This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("O3")
#include "seats.h"
#include <bits/stdc++.h>
#define vi vector<int>
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
const int INF = 1e9 + 7;
const int MAXN = 1e6 + 5;
int seg[MAXN*4][2], lazy[MAXN*4];
int h, w, dx[4] = {0, 0, -1, -1}, dy[4] = {0, -1, 0, -1};
vi arr[MAXN];
pii pos[MAXN];
int get(int i, int j) {
if (0 > i || i >= h || 0 > j || j >= w)
return h * w;
return arr[i][j];
}
void build(int l, int r, int k) {
lazy[k] = 0;
if (l == r) {
seg[k][0] = 1;
seg[k][1] = 0;
return;
}
int m = (l + r) / 2;
build(l, m, k * 2);
build(m + 1, r, k*2+1);
seg[k][0] = seg[k*2][0] + seg[k*2+1][0];
seg[k][1] = 0;
}
void upd(int l, int r, int k, int a, int b, int v) {
if (b < l || r < a) return;
if (a <= l && r <= b) {
seg[k][1] += v;
lazy[k] += v;
return;
}
int m = (l + r) / 2;
upd(l, m, k*2, a, b, v);
upd(m+1, r, k*2+1, a, b, v);
if (seg[k*2][1] < seg[k*2+1][1])
seg[k][0] = seg[k*2][0], seg[k][1] = seg[k*2][1];
else if (seg[k*2][1] > seg[k*2+1][1])
seg[k][0] = seg[k*2+1][0], seg[k][1] = seg[k*2+1][1];
else
seg[k][0] = seg[k*2][0]+seg[k*2+1][0], seg[k][1] = seg[k*2][1];
seg[k][1] += lazy[k];
}
void calc(int x, int y, int v) {
vi tmp = {0, 0, 0, 0};
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
tmp[i] = get(nx, ny);
}
sort(tmp.begin(), tmp.end());
if (tmp[0] != tmp[1])
upd(0, h*w - 1, 1, tmp[0], tmp[1] - 1, v);
if (tmp[2] != tmp[3])
upd(0, h*w - 1, 1, tmp[2], tmp[3] - 1, v);
}
void give_initial_chart(int H, int W, vector<int> r, vector<int> c) {
h = H, w = W;
for (int i = 0; i < h; i++)
arr[i].resize(w);
for (int i = 0; i < h * w; i++) {
arr[r[i]][c[i]] = i;
pos[i] = {r[i], c[i]};
}
build(0, h*w - 1, 1);
for (int i = 0; i <= h; i++)
for (int j = 0; j <= w; j++)
calc(i, j, 1);
}
int swap_seats(int a, int b) {
int x1 = pos[a].fi, y1 = pos[a].se;
int x2 = pos[b].fi, y2 = pos[b].se;
for (int i = 0; i < 4; i++) {
int nx = x1 - dx[i], ny = y1 - dy[i];
calc(nx, ny, -1);
nx = x2 - dx[i], ny = y2 - dy[i];
calc(nx, ny, -1);
}
arr[x2][y2] = a;
arr[x1][y1] = b;
for (int i = 0; i < 4; i++) {
int nx = x1 - dx[i], ny = y1 - dy[i];
calc(nx, ny, 1);
nx = x2 - dx[i], ny = y2 - dy[i];
calc(nx, ny, 1);
}
swap(pos[a], pos[b]);
return seg[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... |