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;
const int MAX = 1000005;
long long st[4 * MAX][2], lzy[4 * MAX];
void build(int node, int l, int r) {
st[node][0] = 0;
st[node][1] = r - l + 1;
lzy[node] = 0;
if (l == r) {
return;
}
int mid = l + r >> 1;
build(node + node, l, mid);
build(node + node + 1, mid + 1, r);
}
void pull(int node, int l, int r) {
st[node][0] = min(st[node + node][0], st[node + node + 1][0]);
st[node][1] = 0;
if (st[node][0] == st[node + node][0]) {
st[node][1] += st[node + node][1];
}
if (st[node][0] == st[node + node + 1][0]) {
st[node][1] += st[node + node + 1][1];
}
}
void push(int node, int l, int r) {
if (l == r) {
return;
}
st[node + node][0] += lzy[node];
st[node + node + 1][0] += lzy[node];
lzy[node + node] += lzy[node];
lzy[node + node + 1] += lzy[node];
lzy[node] = 0;
}
void modify(int node, int l, int r, int ll, int rr, int v) {
if (l > rr || r < ll) return;
if (ll <= l && r <= rr) {
st[node][0] += v;
lzy[node] += v;
push(node, l, r);
return;
}
int mid = l + r >> 1;
push(node, l, r);
modify(node + node, l, mid, ll, rr, v);
modify(node + node + 1, mid + 1, r, ll, rr, v);
pull(node, l, r);
}
vector<vector<int>> a;
int h, w;
int x[MAX];
int y[MAX];
vector<int> dx = {1, 0, 1};
vector<int> dy = {0, 1, 1};
void add(int p, int i, int j, int v) {
vector<int> vals(1, p);
for (int d = 0; d < 3; d++) {
int ni = i + dx[d];
int nj = j + dy[d];
if (ni >= 0 && ni < h && nj >= 0 && nj < w) {
vals.push_back(a[ni][nj]);
} else {
vals.push_back(h * w);
}
}
sort(vals.begin(), vals.end());
if (vals[0] < vals[1]) {
modify(1, 0, h * w - 1, vals[0], vals[1] - 1, v);
}
if (vals[2] < vals[3]) {
modify(1, 0, h * w - 1, vals[2], vals[3] - 1, v * MAX);
}
}
void give_initial_chart(int H, int W, vector<int> R, vector<int> C) {
h = H;
w = W;
a.resize(H);
for (int i = 0; i < H; i++) {
a[i].resize(W);
}
for (int i = 0; i < H * W; i++) {
x[i] = R[i];
y[i] = C[i];
a[x[i]][y[i]] = i;
}
build(1, 0, h * w - 1);
for (int i = 0; i < H * W; i++) {
add(i, x[i], y[i], 1);
}
for (int i = 0; i < H; i++) {
add(h * w, i, -1, 1);
}
for (int j = -1; j < W; j++) {
add(h * w, -1, j, 1);
}
}
vector<int> di = {0, 0, -1, -1};
vector<int> dj = {0, -1, 0, -1};
bool Valid(int i, int j) {
return i >= 0 && i < h && j >= 0 && j < w;
}
vector<pair<int, int>> aux;
int swap_seats(int i, int j) {
for (int d = 0; d < 4; d++) {
{
int ni = x[i] + di[d];
int nj = y[i] + dj[d];
aux.emplace_back(ni, nj);
}
{
int ni = x[j] + di[d];
int nj = y[j] + dj[d];
aux.emplace_back(ni, nj);
}
}
sort(aux.begin(), aux.end());
aux.erase(unique(aux.begin(), aux.end()), aux.end());
for (auto& c : aux) {
if (Valid(c.first, c.second)) {
add(a[c.first][c.second], c.first, c.second, -1);
} else {
add(h * w, c.first, c.second, -1);
}
}
swap(a[x[i]][y[i]], a[x[j]][y[j]]);
swap(x[i], x[j]);
swap(y[i], y[j]);
for (auto& c : aux) {
if (Valid(c.first, c.second)) {
add(a[c.first][c.second], c.first, c.second, 1);
} else {
add(h * w, c.first, c.second, 1);
}
}
aux.clear();
return st[1][1];
}
Compilation message (stderr)
seats.cpp: In function 'void build(int, int, int)':
seats.cpp:17:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
17 | int mid = l + r >> 1;
| ~~^~~
seats.cpp: In function 'void modify(int, int, int, int, int, int)':
seats.cpp:52:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
52 | int mid = l + r >> 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... |