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;
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;
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);
}
#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);
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 r1 = r[0], r2 = r[0];
int c1 = c[0], c2 = c[0];
int k = 1;
int steps = 0;
while (true) {
r1 = min(r1, r[k]);
r2 = max(r2, r[k]);
c1 = min(c1, c[k]);
c2 = max(c2, c[k]);
int nk = (r2 - r1 +1) * (c2 - c1 +1);
if (ft.sum(r1, c1, r2, c2) == (ll)nk * (nk-1) / 2)
ans++;
if (nk == h * w) break;
for (; k < nk; k++) {
r1 = min(r1, r[k]);
r2 = max(r2, r[k]);
c1 = min(c1, c[k]);
c2 = max(c2, c[k]);
}
steps++;
if (k >= h * w) break;
}
assert(steps <= 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... |