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<bits/stdc++.h>
using namespace std;
typedef long long llint;
typedef pair <int, int> pi;
const int MAXN = 1000005;
const llint INF = 1000000000000000000LL;
const int BIG = 1000000000;
int n, m, ofs = 1;
vector <int> r, c;
vector < vector <int> > v;
llint a[MAXN], b[MAXN];
int dx[4] = {0, 0, 1, 1};
int dy[4] = {0, 1, 0, 1};
struct node {
llint mn, prop;
int br;
node () {
mn = 0; br = 1; prop = 0;
}
node (llint _mn, int _br) {
mn = _mn; br = _br; prop = 0;
}
} t[MAXN * 4];
node spoji (node a, node b) {
if (a.mn < b.mn) return a;
if (a.mn > b.mn) return b;
return node(a.mn, a.br + b.br);
}
void tour_init () {
while (ofs < n*m) ofs *= 2;
for (int i = n*m; i < ofs; i++) {
t[i + ofs] = node(INF, 1);
}
for (int i = ofs - 1; i > 0; i--) {
t[i] = spoji(t[2 * i], t[2 * i + 1]);
}
}
void propagate (int x) {
if (t[x].prop == 0) return;
if (x < ofs) {
t[2*x].prop += t[x].prop;
t[2*x+1].prop += t[x].prop;
}
t[x].mn += t[x].prop;
t[x].prop = 0;
}
int from, to, val;
void update (int x, int lo, int hi) {
propagate(x);
if (from <= lo && hi <= to) {
t[x].prop += val;
propagate(x);
return;
}
if (to < lo || hi < from) return;
update(2*x, lo, (lo + hi) / 2);
update(2*x+1, (lo + hi) / 2 + 1, hi);
t[x] = spoji(t[2*x], t[2*x+1]);
}
node upit (int x, int lo, int hi) {
propagate(x);
if (from <= lo && hi <= to) return t[x];
if (to < lo || hi < from) return node(INF, 0);
return spoji(upit(2*x, lo, (lo + hi) / 2), upit(2*x+1, (lo + hi) / 2 + 1, hi));
}
void two_by_two (int x, int y, int d) {
int w[4] = {v[x][y], v[x][y + 1], v[x + 1][y], v[x + 1][y + 1]};
sort(w, w + 4);
if (w[0] < w[1]) {from = w[0]; to = w[1] - 1; val = d; update(1, 0, ofs - 1);}
if (w[2] < w[3]) {from = w[2]; to = w[3] - 1; val = BIG * d; update(1, 0, ofs - 1);}
}
void give_initial_chart (int N, int M, vector <int> R, vector <int> C) {
n = N; m = M; r = R; c = C;
vector <int> e(m + 2, BIG);
for (int i = 0; i < n + 2; i++) v.push_back(e);
for (int i = 0; i < n * m; i++) {
v[R[i] + 1][C[i] + 1] = i;
}
tour_init();
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++) {
two_by_two(i, j, 1);
}
}
}
int swap_seats (int a, int b) {
vector <pi> u;
for (int i = 0; i < 4; i++) u.push_back({r[a] + dx[i], c[a] + dy[i]});
for (int i = 0; i < 4; i++) u.push_back({r[b] + dx[i], c[b] + dy[i]});
sort(u.begin(), u.end());
u.erase(unique(u.begin(), u.end()), u.end());
for (auto p : u) two_by_two(p.first, p.second, -1);
swap(v[r[a] + 1][c[a] + 1], v[r[b] + 1][c[b] + 1]);
swap(r[a], r[b]);
swap(c[a], c[b]);
for (auto p : u) two_by_two(p.first, p.second, 1);
from = 0; to = ofs - 1;
node res = upit(1, 0, ofs - 1);
if (res.mn > 4) return 0;
return res.br;
}
# | 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... |