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;
#define MP make_pair
#define fi first
#define se second
#define INF 1000000007
#define MAXN 1048577
typedef pair<int, int> pii;
typedef pair<pii, pii> ppp;
typedef pair<pii, int> ppi;
int N, M, K;
pii coor[MAXN];
vector<int> grid[MAXN];
pii lazy[2 * MAXN];
ppi stor[2 * MAXN];
pii pref[2 * MAXN];
ppi comb(ppi a, ppi b)
{
ppi res = {{0, 0}, 0};
if (a.fi <= b.fi)
{
res.fi = a.fi;
res.se += a.se;
}
if (b.fi <= a.fi)
{
res.fi = b.fi;
res.se += b.se;
}
return res;
}
void build(int w, int L, int R)
{
lazy[w] = {0, 0};
if (L == R)
{
stor[w] = {pref[L], 1};
return;
}
int mid = (L + R) >> 1;
build(w << 1, L, mid);
build(w << 1 | 1, mid + 1, R);
stor[w] = comb(stor[w << 1], stor[w << 1 | 1]);
}
void push(int w, int L, int R)
{
stor[w].fi.fi += lazy[w].fi; stor[w].fi.se += lazy[w].se;
if (L != R)
{
lazy[w << 1].fi += lazy[w].fi; lazy[w << 1].se += lazy[w].se;
lazy[w << 1 | 1].fi += lazy[w].fi; lazy[w << 1 | 1].se += lazy[w].se;
}
lazy[w] = {0, 0};
}
void update(int w, int L, int R, int a, int b, pii v)
{
push(w, L, R);
if (b < L || R < a) return;
if (a <= L && R <= b)
{
lazy[w].fi += v.fi; lazy[w].se += v.se;
push(w, L, R);
return;
}
int mid = (L + R) >> 1;
update(w << 1, L, mid, a, b, v);
update(w << 1 | 1, mid + 1, R, a, b, v);
stor[w] = comb(stor[w << 1], stor[w << 1 | 1]);
}
void upd(int l, int r, pii p)
{
r = min(r, K - 1);
l = max(l, 0);
if (l > r) return;
update(1, 0, K - 1, l, r, p);
}
int sorta[4];
ppp sort4(int a, int b, int c, int d)
{
sorta[0] = a; sorta[1] = b; sorta[2] = c; sorta[3] = d;
sort(sorta, sorta + 4);
return {{sorta[0], sorta[1]}, {sorta[2], sorta[3]}};
}
void give_initial_chart(int h, int w, vector<int> R, vector<int> C)
{
N = h + 2;
M = w + 2;
K = w * h;
for (int i = 0; i < N; i++)
{
grid[i].resize(M);
}
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
if (i == 0 || i == N - 1 || j == 0 || j == M - 1)
{
grid[i][j] = K; K++;
}
}
}
K = w * h;
for (int i = 0; i < K; i++)
{
R[i]++; C[i]++;
coor[i] = {R[i], C[i]};
grid[R[i]][C[i]] = i;
}
for (int i = 0; i < N - 1; i++)
{
for (int j = 0; j < M - 1; j++)
{
ppp p = sort4(grid[i][j], grid[i + 1][j], grid[i][j + 1], grid[i + 1][j + 1]);
pref[p.se.fi].fi++;
pref[p.se.se].fi--;
pref[p.fi.fi].se++;
pref[p.fi.se].se--;
// upd(p.se.fi, p.se.se - 1, {1, 0});
// upd(p.fi.fi, p.fi.se - 1, {0, 1});
}
}
for (int i = 0; i < K; i++)
{
if (i)
{
pref[i].fi += pref[i - 1].fi;
pref[i].se += pref[i - 1].se;
}
}
build(1, 0, K - 1);
}
int swap_seats(int a, int b)
{
pii p0 = coor[a], p1 = coor[b];
for (int i = -1; i <= 0; i++)
{
for (int j = -1; j <= 0; j++)
{
int x, y; ppp p2, p3;
x = p0.fi + i; y = p0.se + j;
p2 = sort4(grid[x][y], grid[x + 1][y], grid[x][y + 1], grid[x + 1][y + 1]);
grid[p0.fi][p0.se] = b;
grid[p1.fi][p1.se] = a;
p3 = sort4(grid[x][y], grid[x + 1][y], grid[x][y + 1], grid[x + 1][y + 1]);
if (p2.fi != p3.fi)
{
upd(p2.fi.fi, p2.fi.se - 1, {0, -1});
upd(p3.fi.fi, p3.fi.se - 1, {0, 1});
}
if (p2.se != p3.se)
{
upd(p2.se.fi, p2.se.se - 1, {-1, 0});
upd(p3.se.fi, p3.se.se - 1, {1, 0});
}
grid[p0.fi][p0.se] = a;
grid[p1.fi][p1.se] = b;
}
}
for (int i = -1; i <= 0; i++)
{
for (int j = -1; j <= 0; j++)
{
int x, y; ppp p2, p3;
x = p1.fi + i; y = p1.se + j;
p2 = sort4(grid[x][y], grid[x + 1][y], grid[x][y + 1], grid[x + 1][y + 1]);
grid[p0.fi][p0.se] = b;
grid[p1.fi][p1.se] = a;
p3 = sort4(grid[x][y], grid[x + 1][y], grid[x][y + 1], grid[x + 1][y + 1]);
if (p2.fi != p3.fi)
{
upd(p2.fi.fi, p2.fi.se - 1, {0, -1});
upd(p3.fi.fi, p3.fi.se - 1, {0, 1});
}
if (p2.se != p3.se)
{
upd(p2.se.fi, p2.se.se - 1, {-1, 0});
upd(p3.se.fi, p3.se.se - 1, {1, 0});
}
grid[p0.fi][p0.se] = a;
grid[p1.fi][p1.se] = b;
}
}
grid[p0.fi][p0.se] = b;
grid[p1.fi][p1.se] = a;
swap(coor[a], coor[b]);
push(1, 0, K - 1);
return stor[1].se;
}
# | 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... |