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 Seg {
int n;
vii seg;
Seg() {}
Seg(int m) {
for (n = 1; n < m; n <<= 1);
seg.resize(2 * n);
}
void update(int i, int x) {
seg[i += n] = {x, x};
for (i >>= 1; i; i >>= 1) {
seg[i].x = min(seg[i << 1].x, seg[i << 1 | 1].x);
seg[i].y = max(seg[i << 1].y, seg[i << 1 | 1].y);
}
}
pii query(int l, int r) {
pii ans = {MAX_N, 0};
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1) {
chkmin(ans.x, seg[l].x);
chkmax(ans.y, seg[l].y);
l++;
}
if (r & 1) {
--r;
chkmin(ans.x, seg[r].x);
chkmax(ans.y, seg[r].y);
}
}
return ans;
}
};
int r[MAX_N], c[MAX_N];
Seg r_seg, c_seg;
int n;
int h, w;
inline void update(int i) {
r_seg.update(i, r[i]);
c_seg.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;
r_seg = c_seg = Seg(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++) {
auto [x1, x2] = r_seg.query(0, i + 1);
auto [y1, y2] = c_seg.query(0, i + 1);
if ((x2 - x1 + 1) * (y2 - y1 + 1) == i + 1) {
ans++;
} else {
i = (x2 - x1 + 1) * (y2 - y1 + 1) - 2;
}
}
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... |