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 "seats.h"
#include <bits/stdc++.h>
using namespace std;
constexpr int N = 1 << 20, dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1}; //Up, Right, Down, Left
int tag[N << 1], mn[N << 1], cnt[N << 1], sz = 1;
void apply(int x, int v) {
tag[x] += v;
mn[x] += v;
}
void pull(int x) {
mn[x] = min(mn[x << 1], mn[x << 1 | 1]);
cnt[x] = 0;
if (mn[x] == mn[x << 1]) {
cnt[x] += cnt[x << 1];
}
if (mn[x] == mn[x << 1 | 1]) {
cnt[x] += cnt[x << 1 | 1];
}
mn[x] += tag[x];
}
void modify(int l, int r, int val, int x = 1, int lx = 0, int rx = sz) {
if (l >= rx || lx >= r) {
return;
}
if (l <= lx && rx <= r) {
apply(x, val);
return;
}
int mid = (lx + rx) >> 1;
modify(l, r, val, x << 1, lx, mid), modify(l, r, val, x << 1 | 1, mid, rx);
pull(x);
}
void build(int a[], int x = 1, int lx = 0, int rx = sz) {
if (lx + 1 == rx) {
mn[x] = a[lx];
cnt[x] = 1;
return;
}
int mid = (lx + rx) >> 1;
build(a, x << 1, lx, mid), build(a, x << 1 | 1, mid, rx);
pull(x);
}
int n, m, S;
int R[N], C[N];
int **p;
bool inside(int a, int b) {
return a >= 0 && a < n && b >= 0 && b < m;
}
int prefix[N];
void update(int i, int val, bool O1 = false) {
int x = R[i], y = C[i];
int nx[4], ny[4], time[4];
for (int j = 0; j < 4; ++j) {
nx[j] = x + dx[j], ny[j] = y + dy[j];
time[j] = inside(nx[j], ny[j]) ? p[nx[j]][ny[j]] : S;
}
for (int j = 0; j < 4; ++j) {
int r = min(time[j], time[j == 3 ? 0 : j + 1]);
if (!O1) {
modify(i, r, val);
} else {
prefix[i] += 1;
prefix[max(i, r)] -= 1;
}
r = max(time[j], time[j == 3 ? 0 : j + 1]);
if (r < i) {
if (!O1) {
modify(min(i, r), max(i, r), val);
} else {
prefix[min(i, r)] += 1;
prefix[max(i, r)] -= 1;
}
}
}
}
void give_initial_chart(int H, int W, vector<int> _R, vector<int> _C) {
memcpy(R, _R.data(), sizeof(int) * H * W);
memcpy(C, _C.data(), sizeof(int) * H * W);
n = H, m = W;
S = n * m;
p = new int*[n];
for (int i = 0; i < n; ++i) {
p[i] = new int[m];
}
for (int i = 0; i < S; ++i) {
p[R[i]][C[i]] = i;
}
sz = S;
for (int i = 0; i < S; ++i) {
update(i, 1, true);
}
for (int i = 0; i < S; ++i) {
if (i > 0) {
prefix[i] += prefix[i - 1];
}
}
build(prefix);
}
int swap_seats(int a, int b) {
if (a > b) {
swap(a, b);
}
vector<int> yy = {a, b};
for (int _ = 0; _ < 2; ++_) {
for (int i = 0; i < 4; ++i) {
int nx = R[a] + dx[i], ny = C[a] + dy[i];
if (inside(nx, ny)) {
yy.push_back(p[nx][ny]);
}
}
swap(a, b);
}
sort(yy.begin(), yy.end());
yy.resize(unique(yy.begin(), yy.end()) - yy.begin());
for (int i: yy) {
update(i, -1);
}
swap(R[a], R[b]), swap(C[a], C[b]);
p[R[a]][C[a]] = a, p[R[b]][C[b]] = b;
for (int i: yy) {
update(i, 1);
}
return cnt[1];
}
# | 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... |