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;
struct node {
int mn, cnt;
node() {
cnt = 0, mn = 0x3f3f3f3f;
}
};
node operator+(node a, node b) {
if (b.cnt != 0) {
if (b.mn < a.mn) {
a.mn = b.mn, a.cnt = b.cnt;
} else if (b.mn == a.mn) {
a.cnt += b.cnt;
}
}
return a;
}
constexpr int N = 1 << 20, dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1}; //Up, Right, Down, Left
int tag[N << 1], sz = 1;
node t[N << 1];
void apply(int x, int v) {
tag[x] += v;
t[x].mn += v;
}
void push(int x) {
if (tag[x] == 0) {
return;
}
apply(x << 1, tag[x]);
apply(x << 1 | 1, tag[x]);
tag[x] = 0;
}
void pull(int x) {
t[x] = t[x << 1] + t[x << 1 | 1];
}
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;
push(x);
modify(l, r, val, x << 1, lx, mid), modify(l, r, val, x << 1 | 1, mid, rx);
pull(x);
}
node get(int l, int r, int x = 1, int lx = 0, int rx = sz) {
if (l >= rx || lx >= r || l >= r) {
return {};
}
if (l <= lx && rx <= r) {
return t[x];
}
int mid = (lx + rx) >> 1;
push(x);
return get(l, r, x << 1, lx, mid) + get(l, r, x << 1 | 1, mid, rx);
}
int n, m, S;
vector<int> R, C;
vector<vector<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 + 1) % 4]);
if (!O1) {
modify(i, r, val);
} else {
prefix[i] += 1;
prefix[max(i, r)] -= 1;
}
r = max(time[j], time[(j + 1) % 4]);
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) {
swap(R, _R), swap(C, _C);
n = H, m = W;
S = n * m;
p.assign(n, vector<int>(m));
for (int i = 0; i < S; ++i) {
p[R[i]][C[i]] = i;
}
sz = 1;
while (sz < S) sz <<= 1;
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];
}
t[i + sz].mn = prefix[i];
t[i + sz].cnt = 1;
}
for (int i = sz - 1; i > 0; --i) {
pull(i);
}
}
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 get(0, S).cnt;
}
# | 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... |