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 {
using S = int;
struct T {
int sum=0;
int min=0;
int minc=0;
};
const T id = {};
inline T single(S v) { return {v, v, 1}; }
static T merge(const T& l, const T& r) {
if (l.min == l.sum + r.min) {
return {.sum = l.sum + r.sum, .min = l.min, .minc = l.minc + r.minc};
}
else if (l.min < l.sum + r.min) {
return {.sum = l.sum + r.sum, .min = l.min, .minc = l.minc};
} else {
return {.sum = l.sum + r.sum, .min = l.sum + r.min, .minc = r.minc};
}
}
int n;
vector<T> tree;
SegmentTree() {}
void init(int n) {
this->n = n = nextPower2(n);
tree.assign(n * 2, id);
build();
}
void build() {
for (int i = n-1; i >= 1; i--) {
tree[i] = merge(tree[i*2], tree[i*2 + 1]);
}
}
void update(int i, S v) {
tree[i+=n] = single(v);
for (i /= 2; i >= 1; i/= 2)
tree[i] = merge(tree[i*2], tree[i*2+1]);
}
int query() {
return tree[1].minc;
}
};
SegmentTree pot;
int add(int i) {
if (i == 0) return 0;
auto& brr = arr[0];
int j = c[i];
int cnt = 0;
if (j == 0 || brr[j-1] > i) cnt++;
else cnt--;
if (j == w -1 || brr[j+1] > i) cnt++;
else cnt--;
return cnt;
};
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);
}
pot.init(w);
for (int i = 0; i < w; i++) {
pot.update(i, add(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]);
for (int i : {a-1, a, a+1, b-1, b, b+1}) {
if (0 <= i && i <= w-1)
pot.update(i, add(i));
}
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... |