This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "seats.h"
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;
std::vector<int> r;
std :: vector <int> c;
const int NMAX = 1e6 + 5;
void add(int val);
void rem(int val);
int lazy[4 * NMAX];
pii t[4 * NMAX];
int n, m;
void push(int v){
if(lazy[v] == 0) return;
t[2 * v].f += lazy[v];
t[2 * v + 1].f += lazy[v];
lazy[2 * v] += lazy[v];
lazy[2 * v + 1] += lazy[v];
lazy[v] = 0;
return;
}
pii merg(pii a, pii b){
if(a.f < b.f)return a;
if(a.f > b.f)return b;
return {a.f, b.s + a.s};
}
void update(int v, int l, int r, int st, int fin, int val){
if(st > fin) return;
if(l > fin || r < st) return;
if(l >= st && r <= fin) {
t[v].f += val;
lazy[v] += val;
return;
}
int m = (l + r) / 2;
push(v);
update(2 * v, l, m, st, fin, val);
update(2 * v +1, m + 1, r, st, fin, val);
t[v] = merg(t[2 * v], t[2 * v + 1]);
}
void go(int v, int l, int r){
if(l == r){
t[v] = {0, 1};
return;
}
int m = (l + r) / 2;
go(2 * v, l, m);
go(2 * v + 1, m + 1, r);
t[v]= {0, t[2 * v].s + t[2 * v + 1].s};
}
vector <vector <int> > a;
vector <vector <bool> > used;
set <pii> st;
void give_initial_chart(int H, int W, std::vector<int> R, std::vector<int> C) {
r = R;
c = C;
n = H, m = W;
a.resize(n + 2);
used.resize(n + 2);
for(int i = 0; i <= n + 1; i++) a[i].resize(m + 2), used[i].resize(m + 2);
go(1, 1, n * m);
for(int i = 0; i <= n + 1; i++){
for(int j = 0; j <= m + 1; j++) a[i][j] = n * m;
}
for(int i = 0; i < n * m; i++){
int x = ++r[i], y= ++c[i];
a[x][y] = i;
}
for(int i = 0; i < n * m; i++) add(i);
for(int i = 0; i <= n + 1; i++) {
for(int j = 0; j <= m + 1; j ++) used[i][j] = false;
}
}
void add(int val){
int x = r[val];
int y = c[val];
for(int i = x - 1; i <= x; i++){
for(int j = y - 1; j <= y; j++){
if(used[i][j]) continue;
used[i][j] = true;
vector <int> vec;
vec.pb(a[i][j]);
vec.pb(a[i][j + 1]);
vec.pb(a[i + 1][j]);
vec.pb(a[i + 1][j + 1]);
sort(vec.begin(), vec.end());
update(1, 1, n * m, vec[0] + 1, vec[1], 1);
// update(1, 1, n * m, vec[1] + 1, vec[2], -1);
update(1, 1, n * m, vec[2] + 1, vec[3], 1);
}
}
}
void rem(int val){
int x = r[val];
int y = c[val];
for(int i = x - 1; i <= x; i++){
for(int j = y - 1; j <= y; j++){
if(used[i][j]) continue;
used[i][j] = true;
vector <int> vec;
vec.pb(a[i][j]);
vec.pb(a[i][j + 1]);
vec.pb(a[i + 1][j]);
vec.pb(a[i + 1][j + 1]);
sort(vec.begin(), vec.end());
update(1, 1, n * m, vec[0] + 1, vec[1], -1);
// update(1, 1, n * m, vec[1] + 1, vec[2], 1);
update(1, 1, n * m, vec[2] + 1, vec[3], -1);
}
}
}
void cl(int val){
int x = r[val];
int y = c[val];
for(int i = x - 1; i <= x; i++){
for(int j = y - 1; j <= y; j++){
used[i][j] = false;
}
}
}
int swap_seats(int A, int B) {
//cout << t[1].s << "\n";
rem(A);
rem(B);
cl(A);
cl(B);
swap(r[A], r[B]);
swap(c[A], c[B]);
a[r[A]][c[A]] = A;
a[r[B]][c[B]] = B;
add(A);
add(B);
cl(A);
cl(B);
if(t[1].f == 4) return t[1].s;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |