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;
int nextPower2(int n) {
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return 1+n;
}
struct SegmentTree {
struct node {
int cnt = 1;
int min = 0;
int lz = 0;
};
int n;
vector<node> t;
SegmentTree()= default;
void init(int n) {
this->n = n;
t.assign(this->n*4, {});
build(1, 0, n-1);
}
void build(int v, int tl, int tr) {
if (tl == tr) return void(t[v] = node{.cnt=1, .min=0});
int mid = (tl+tr) / 2;
build(v*2, tl, mid);
build(v*2+1, mid+1, tr);
t[v] = merge(t[v*2], t[v*2+1]);
}
void apply(int v, int x) {
t[v].lz += x;
t[v].min += x;
}
void push(int v) {
if (t[v].lz) {
apply(v*2, t[v].lz);
apply(v*2+1, t[v].lz);
t[v].lz = 0;
}
}
node merge(const node& a, const node&b) {
node res{.min = min(a.min, b.min)};
if (a.min == b.min) res.cnt = a.cnt + b.cnt;
else if (a.min< b.min) res.cnt = a.cnt;
else res.cnt = b.cnt;
return res;
}
void update(int l, int r, int x, int v, int tl, int tr) {
if (tl > tr) return;
if (r < tl || tr < l) return;
if (l <= tl && tr <= r) {
apply(v, x);
return;
}
push(v);
int mid = (tl+tr) / 2;
update(l, r, x, v*2, tl, mid);
update(l, r, x, v*2+1, mid+1, tr);
t[v] = merge(t[v*2], t[v*2+1]);
}
void update(int l, int r, int x) {
update(l, r, x, 1, 0, n-1);
}
int query() {
return t[1].cnt;
}
node query(int l, int r, int v, int tl, int tr) {
if (r < tl || tr < l) return {.cnt=0, .min=int(1e9)};
if (l <= tl && tr <= r) return t[v];
push(v);
int mid = (tl+tr) / 2;
return merge(query(l, r, v*2, tl, mid), query(l, r, v*2+1, mid+1, tr));
}
node query(int l, int r) {
return query(l, r, 1, 0, n-1);
}
};
SegmentTree pot;
vector<int> raw;
int add(int i) {
int cnt_1 = 0, cnt_3 = 0;
int row = r[i], col = c[i];
for (int ir = 0; ir < 2; ir++) {
for (int ic = 0; ic < 2; ic++) {
int in = 0;
for (int iir = 0; iir < 2; iir++) {
for (int iic = 0; iic < 2; iic++) {
int nrow = row - ir + iir;
int ncol = col - ic + iic;
if (clamp(nrow,0, h-1) == nrow && clamp(ncol, 0, w-1) == ncol)
if (arr[nrow][ncol] < i) in++;
}
}
if (in == 0) cnt_1++;
if (in == 1) cnt_1--;
if (in == 2) cnt_3++;
if (in == 3) cnt_3--;
}
}
return cnt_1 + cnt_3;
};
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);
}
raw.assign(w, 0);
pot.init(w);
for (int i = 0; i < w; i++) {
int x = add(i);
pot.update(i, w-1, x - raw[i]);
raw[i] = x;
}
#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]);
auto update = [&](int i, int j) {
if (clamp(i, 0,h-1) != i || clamp(j, 0, w-1) != j)return;
int num = arr[i][j];
int x = add(num);
pot.update(num, w - 1, x - raw[i]);
raw[i] = x;
};
for (int di = -1; di <= 1; di++) {
for (int dj = -1; dj <= 1; dj++) {
update(r[a] + di, c[a] + dj);
update(r[b] + di, c[b] + dj);
}
}
assert(h == 1);
int ans = pot.query();
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... |