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>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
const int MAX_N = 1e6 + 5;
struct SegMin {
int n;
vi seg;
SegMin() {}
SegMin(int m) {
for (n = 1; n < m; n <<= 1);
seg.resize(2 * n);
}
void update(int i, int x) {
seg[i += n] = x;
for (i >>= 1; i; i >>= 1) seg[i] = min(seg[i << 1], seg[i << 1 | 1]);
}
int query(int l, int r) {
int ans = MAX_N;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1) chkmin(ans, seg[l++]);
if (r & 1) chkmin(ans, seg[--r]);
}
return ans;
}
};
struct Seg1 {
int n;
vi seg;
Seg1() {}
Seg1(int m) {
for (n = 1; n < m; n <<= 1);
seg.resize(2 * n);
}
void update(int i, int x) {
seg[i += n] = x;
for (i >>= 1; i; i >>= 1) seg[i] = max(seg[i << 1], seg[i << 1 | 1]);
}
int query(int l, int r) {
int ans = 0;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1) chkmax(ans, seg[l++]);
if (r & 1) chkmax(ans, seg[--r]);
}
return ans;
}
};
struct Seg2 {
int n, m;
vector<Seg1> seg;
Seg2() {}
Seg2(int n1, int m1) {
for (n = 1; n < n1; n <<= 1);
seg.resize(2 * n, Seg1(m1));
m = seg[0].n;
}
void update(int i, int j, int x) {
seg[i += n].update(j, x);
for (i >>= 1; i; i >>= 1) {
x = max(seg[i << 1].seg[j + m], seg[i << 1 | 1].seg[j + m]);
seg[i].update(j, x);
}
}
int query(int x1, int y1, int x2, int y2) {
int ans = 0;
for (x1 += n, x2 += n; x1 < x2; x1 >>= 1, x2 >>= 1) {
if (x1 & 1) chkmax(ans, seg[x1++].query(y1, y2));
if (x2 & 1) chkmax(ans, seg[--x2].query(y1, y2));
}
return ans;
}
};
int r[MAX_N], c[MAX_N];
Seg2 seg;
Seg1 r_max, c_max;
SegMin r_min, c_min;
int n;
int h, w;
inline void update(int i) {
seg.update(r[i], c[i], i);
r_max.update(i, r[i]);
c_max.update(i, c[i]);
r_min.update(i, r[i]);
c_min.update(i, c[i]);
}
void give_initial_chart(int H, int W, std::vector<int> R, std::vector<int> C) {
h = H, w = W;
n = h * w;
seg = Seg2(h, w);
r_min = c_min = SegMin(n);
r_max = c_max = Seg1(n);
for (int i = 0; i < n; i++) {
r[i] = R[i], c[i] = C[i];
update(i);
}
}
int swap_seats(int a, int b) {
swap(r[a], r[b]), swap(c[a], c[b]);
update(a), update(b);
int ans = 0;
for (int i = 0; i < n; i++) {
int x1 = r_min.query(0, i + 1), y1 = c_min.query(0, i + 1);
int x2 = r_max.query(0, i + 1), y2 = c_max.query(0, i + 1);
if ((x2 - x1 + 1) * (y2 - y1 + 1) == i + 1) {
ans++;
continue;
}
i = seg.query(x1, y1, x2 + 1, y2 + 1) - 1;
}
return ans;
}
//2 3 2
//0 0
//1 0
//1 1
//0 1
//0 2
//1 2
//0 5
//0 5
# | 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... |