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>
#pragma GCC optimize("Ofast")
using namespace std;
const int N = 1e6 + 5, INF = 1e9, G = 5;
int n, m, len;
vector<int> x, y;
vector<int> num[N];
int dx[4] = { 0, 0, 1, 1 }, dy[4] = { 1, 0, 0, 1 };
int mn[N * 4], ct[N * 4], up[N * 4];
struct cmp {
bool operator()(const pair<int, int> &a, const pair<int, int> &b) const {
if (num[a.first][a.second] == num[b.first][b.second])
return a < b;
return num[a.first][a.second] < num[b.first][b.second];
}
};
vector<set<pair<int, int>, cmp>> st[N];
void push(int v, int L, int R) {
if (up[v]) {
if (L != R) {
up[v * 2] += up[v];
up[v * 2 + 1] += up[v];
}
mn[v] += up[v];
up[v] = 0;
}
}
void upd(int l, int r, int x, int v, int L, int R) {
push(v, L, R);
if (l > r)
return;
if (l == L && r == R) {
up[v] += x;
push(v, L, R);
}
else {
int m = (L + R) >> 1;
upd(l, min(m, r), x, v * 2, L, m);
upd(max(m + 1, l), r, x, v * 2 + 1, m + 1, R);
if (mn[v * 2] == mn[v * 2 + 1]) {
mn[v] = mn[v * 2];
ct[v] = ct[v * 2] + ct[v * 2 + 1];
}
else if (mn[v * 2] < mn[v * 2 + 1]) {
mn[v] = mn[v * 2];
ct[v] = ct[v * 2];
}
else {
mn[v] = mn[v * 2 + 1];
ct[v] = ct[v * 2 + 1];
}
}
}
pair<int, int> v[4];
void upd2times2(int a, int b, int d) {
auto x = st[a][b].begin(),
y = next(x);
upd(num[x->first][x->second], num[y->first][y->second] - 1, d, 1, 0, len - 1);
x = next(y),
y = next(x);
upd(num[x->first][x->second], num[y->first][y->second] - 1, d * G, 1, 0, len - 1);
}
void change(int a, int b, int d) {
if (d == 1) {
for (int i = 0; i < 4; i++)
st[a - dx[i]][b - dy[i]].insert({ a, b });
}
for (int i = 0; i < 4; i++) {
upd2times2(a - dx[i], b - dy[i], d);
}
if (d == -1) {
for (int i = 0; i < 4; i++)
st[a - dx[i]][b - dy[i]].erase({ a, b });
}
}
// void que(int v, int L, int R) {
// push(v, L, R);
// if (L == R) {
// cout << t[v].mn << " ";
// }
// else {
// int m = (L + R) >> 1;
// que(v * 2, L, m);
// que(v * 2 + 1, m + 1, R);
// }
// }
void give_initial_chart(int H, int W, std::vector<int> R, std::vector<int> C) {
n = H;
m = W;
x = R;
y = C;
len = n * m;
set<pair<int, int>, cmp> pattern;
for (int i = 0; i < n + 2; i++) {
num[i].resize(m + 2, len);
st[i].resize(m + 2, pattern);
}
fill(ct, ct + len * 4, 1);
for (int i = 0; i < len; i++) {
x[i]++;
y[i]++;
num[x[i]][y[i]] = i;
}
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++) {
for (int k = 0; k < 4; k++)
st[i][j].insert({ i + dx[k], j + dy[k] });
upd2times2(i, j, 1);
}
}
}
int swap_seats(int a, int b) {
change(x[a], y[a], -1);
num[x[a]][y[a]] = b;
change(x[a], y[a], 1);
change(x[b], y[b], -1);
num[x[b]][y[b]] = a;
change(x[b], y[b], 1);
swap(x[a], x[b]);
swap(y[a], y[b]);
push(1, 0, len - 1);
return mn[1] == 4 ? ct[1] : 0;
}
# | 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... |