This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
constexpr const int mxn = 1e6 + 3;
struct Seg {
int size;
int res[mxn << 2], min[mxn << 2], laz[mxn << 2];
Seg() = default;
Seg(int s) {
memset(res, 0, sizeof(res));
memset(min, 0, sizeof(min));
memset(laz, 0, sizeof(laz));
size = s;
build(0, 0, size);
}
void build(int now, int l, int r) {
if (!(r - l - 1)) {
res[now] = 1;
return;
}
int mid = (l + r) >> 1;
build(now * 2 + 1, l, mid);
build(now * 2 + 2, mid, r);
res[now] = res[now * 2 + 1] + res[now * 2 + 2];
}
void push(int now, int l, int r) {
min[now] += laz[now];
if (r - l - 1) {
laz[now * 2 + 1] += laz[now];
laz[now * 2 + 2] += laz[now];
}
laz[now] = 0;
}
void update(int l, int r, int val) {
if (r <= l) {
return;
}
update(l, r, val, 0, 0, size);
}
void update(int tl, int tr, int val, int now, int l, int r) {
if (l >= tr || r <= tl) {
return;
}
if (l >= tl && r <= tr) {
laz[now] += val;
return;
}
push(now, l, r);
int mid = (l + r) >> 1;
update(tl, tr, val, now * 2 + 1, l, mid);
update(tl, tr, val, now * 2 + 2, mid, r);
push(now * 2 + 1, l, mid);
push(now * 2 + 2, mid, r);
min[now] = std::min(min[now * 2 + 1], min[now * 2 + 2]);
res[now] =
(min[now * 2 + 1] == min[now] ? res[now * 2 + 1] : 0) +
(min[now * 2 + 2] == min[now] ? res[now * 2 + 2] : 0);
}
int query() {
push(0, 0, size);
return min[0] == 4 ? res[0] : 0;
}
} seg;
int n, m, k;
std::vector <int> g;
int inv[mxn];
void fix(int i, int j, int val) {
int vals[4] = {
g[i * k + j],
g[(i + 1) * k + j],
g[i * k + j + 1],
g[(i + 1) * k + j + 1]
};
std::sort(vals, vals + 4);
seg.update(vals[0], vals[1], val);
seg.update(vals[2], vals[3], val);
}
void give_initial_chart(int h, int w, std::vector <int> r, std::vector <int> c) {
n = h;
m = w;
k = m + 2;
seg = Seg(n * m);
g.resize((n + 2) * (m + 2), n * m);
for (int i = 0; i < n * m; i++) {
inv[i] = ++r[i] * k + ++c[i];
g[r[i] * k + c[i]] = i;
}
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++) {
fix(i, j, 1);
}
}
}
int swap_seats(int a, int b) {
int av[2] = { inv[a] / k, inv[a] % k };
int bv[2] = { inv[b] / k, inv[b] % k };
for (int di = -1; di <= 0; di++) {
for (int dj = -1; dj <= 0; dj++) {
fix(av[0] + di, av[1] + dj, -1);
fix(bv[0] + di, bv[1] + dj, -1);
}
}
std::swap(g[inv[a]], g[inv[b]]);
std::swap(inv[a], inv[b]);
for (int di = -1; di <= 0; di++) {
for (int dj = -1; dj <= 0; dj++) {
fix(av[0] + di, av[1] + dj, 1);
fix(bv[0] + di, bv[1] + dj, 1);
}
}
return seg.query();
}
# | 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... |