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>
#include "seats.h"
using namespace std;
using ll = long long;
const int inf = 1e9;
int h, w;
std::vector<int> r, c;
vector<vector<int>> arr;
struct Fenwick {
int n, m;
vector<vector<ll>> t;
Fenwick() = default;
Fenwick(int n, int m) : n(n), m(m), t(n, vector<ll>(m)) {}
ll sum(int r, int c) {
ll s = 0;
for (int i = r; i >= 0; i = (i&(i+1))-1)
for (int j = c; j >= 0; j = (j&(j+1))-1)
s += t[i][j];
return s;
}
ll sum(int r1, int c1, int r2, int c2) {
return sum(r2, c2) + sum(r1-1, c1-1) - sum(r1-1, c2) - sum(r2, c1-1);
}
void inc(int r, int c, int d) {
for (; r < n; r |= r+1)
for (int i = c; i < m; i |= i+1)
t[r][i] += d;
}
};
Fenwick ft;
struct SegmentTree {
static int nextPower2(int n) {
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return 1+n;
}
int n;
vector<pair<int,int>> tree;
static pair<int,int> merge(pair<int,int>& a, pair<int,int>& b) {
return {min(a.first, b.first), max(a.second, b.second)};
}
SegmentTree() = default;
SegmentTree(int m, vector<int>& arr) : n(nextPower2(m)), tree(n*2) {
for (int i = 0; i < m; i++)
tree[n+i] = {arr[i], arr[i]};
for (int i = n-1; i >= 1; i--)
tree[i] = merge(tree[i*2], tree[i*2+1]);
}
void update(int i, int x) {
tree[i+=n] = {x, x};
for (i /= 2; i >= 1; i /= 2)
tree[i] = merge(tree[i*2], tree[i*2+1]);
}
pair<int,int> query(int l, int r) {
pair<int,int> ans{inf, -inf};
l += n; r += n;
while (l <= r) {
if (l % 2 == 1) ans = merge(ans, tree[l++]);
if (r % 2 == 0) ans = merge(ans, tree[r--]);
l /= 2;
r /= 2;
}
return ans;
}
};
SegmentTree rs, cs;
void give_initial_chart(int H, int W, std::vector<int> R, std::vector<int> C) {
h = H;
w = W;
r = R;
c = C;
arr = vector(H, vector<int>(W, 0));
ft = Fenwick(H, W);
for (int i = 0; i < H * W; i++) {
arr[r[i]][c[i]] = i;
ft.inc(r[i], c[i], i);
}
rs = SegmentTree(H*W, R);
cs = SegmentTree(H*W, C);
#ifdef ONPC
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
cout << arr[i][j] << " \n"[j == W-1];
#endif
}
int swap_seats(int a, int b) {
ft.inc(r[a], c[a], -a +b);
ft.inc(r[b], c[b], -b +a);
rs.update(a, r[b]);
rs.update(b, r[a]);
cs.update(a, c[b]);
cs.update(b, c[a]);
swap(arr[r[a]][c[a]], arr[r[b]][c[b]]);
swap(r[a], r[b]);
swap(c[a], c[b]);
int ans = 1;
int k = 1;
int steps = 0;
while (k < h*w) {
auto [r1, r2] = rs.query(0, k);
auto [c1, c2] = cs.query(0, k);
int nk = (r2 - r1 +1) * (c2 - c1 +1);
if (k +1 == nk)
ans++, nk++;
k = nk-1;
steps++;
assert(steps <= 2* (h + w));
}
return ans;
}
# | 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... |