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;
const int maxn = 1e6 + 10;
vector <vector <int>> a;
vector <vector <bool>> can;
vector <int> row, col;
int h, w;
int arr[maxn];
bool inside(int x, int y) {
return (0 <= x && x < h) && (0 <= y && y < w);
}
struct node {
int val, cnt;
int lazy;
void add(int x) {
val += x;
lazy += x;
}
} t[maxn * 4];
void pushdown(int c) {
if(!t[c].lazy) return ;
int l = c << 1;
int r = l + 1;
t[l].add(t[c].lazy);
t[r].add(t[c].lazy);
t[c].lazy = 0;
}
void build(int c = 1, int b = 0, int e = h * w - 1) {
t[c].val = t[c].lazy = 0;
t[c].cnt = e - b + 1;
if(b == e) return ;
int m = (b + e) >> 1;
int l = c << 1;
int r = l + 1;
build(l, b, m);
build(r, m + 1, e);
}
void add(int x, int y, int val, int c = 1, int b = 0, int e = h * w - 1) {
if(x > y) return ;
if(x <= b && e <= y) {
t[c].add(val);
return ;
}
if(y < b || e < x) return ;
pushdown(c);
int m = (b + e) >> 1;
int l = c << 1;
int r = l + 1;
add(x, y, val, l, b, m);
add(x, y, val, r, m + 1, e);
t[c].val = min(t[l].val, t[r].val);
t[c].cnt = 0;
if(t[c].val == t[l].val) t[c].cnt += t[l].cnt;
if(t[c].val == t[r].val) t[c].cnt += t[r].cnt;
}
void process(int x, int y, int val) {
for(int dx : {-1, 1}) {
for(int dy : {-1, 1}) {
if(dx == -1 && dy == -1) continue;
if(inside(x + dx, y) && !can[x + dx][y] &&
inside(x, y + dy) && !can[x][y + dy] && !can[x][y]) continue;
int r = h * w;
if(inside(x + dx, y)) r = min(r, a[x + dx][y]);
if(inside(x, y + dy)) r = min(r, a[x][y + dy]);
add(a[x][y], r - 1, val);
if(dx != 1 || dy != 1) continue;
r = 0;
if(inside(x + dx, y)) r = max(r, a[x + dx][y]);
else r = h * w;
if(inside(x, y + dy)) r = max(r, a[x][y + dy]);
else r = h * w;
add(r, a[x][y] - 1, val);
}
}
}
void give_initial_chart(int H, int W, std::vector<int> R, std::vector<int> C) {
h = H;
w = W;
a = vector <vector <int>> (H, vector <int> (W, 0));
can = vector <vector <bool>> (H, vector <bool> (W, true));
row = R;
col = C;
for(int i = 0; i < H * W; i++) {
a[row[i]][col[i]] = i;
}
build();
for(int i = 0; i < h; i++) {
for(int j = 0; j < w; j++) {
process(i, j, 1);
}
}
for(int i = 0; i < h; i++) {
for(int j = 0; j < w; j++) {
can[i][j] = false;
}
}
}
const int dx[] = {0, 0, -1, 1};
const int dy[] = {-1, 1, 0, 0};
int swap_seats(int p, int q) {
int ans = 0;
set <pair <int, int>> change;
change.insert(make_pair(row[p], col[p]));
change.insert(make_pair(row[q], col[q]));
can[row[p]][col[p]] = can[row[q]][col[q]] = true;
for(int i = 0; i < 4; i++) {
int x = row[p] + dx[i];
int y = col[p] + dy[i];
if(inside(x, y)) change.insert(make_pair(x, y));
}
for(int i = 0; i < 4; i++) {
int x = row[q] + dx[i];
int y = col[q] + dy[i];
if(inside(x, y)) change.insert(make_pair(x, y));
}
for(auto i : change) {
process(i.first, i.second, -1);
}
swap(a[row[p]][col[p]], a[row[q]][col[q]]);
swap(row[p], row[q]);
swap(col[p], col[q]);
for(auto i : change) {
process(i.first, i.second, 1);
}
can[row[p]][col[p]] = can[row[q]][col[q]] = false;
return t[1].cnt;
}
Compilation message (stderr)
seats.cpp: In function 'int swap_seats(int, int)':
seats.cpp:104:7: warning: unused variable 'ans' [-Wunused-variable]
104 | int ans = 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... |