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 fr first
#define sc second
using namespace std;
const int INF = 1e9 + 7;
const int MAXN = 1e6 + 5;
int lazy[MAXN*4];
int h, w, dx[4] = {0, 0, -1, -1}, dy[4] = {0, -1, 0, -1};
vi arr[MAXN];
pii pos[MAXN],seg[4*MAXN];
int get(int i, int j) {
if (0 > i || i >= h || 0 > j || j >= w)
return h * w;
return arr[i][j];
}
pair<int,int> merge(pair<int,int> x,pair<int,int> y)
{
if(x.fr<y.fr)return x;
if(x.fr>y.fr)return y;
return make_pair(x.fr,x.sc+y.sc);
}
void build(int l, int r, int k) {
lazy[k] = 0;
if (l == r) {
seg[k]=make_pair(0,1);
return;
}
int m = (l + r) / 2;
build(l, m, k * 2);
build(m + 1, r, k*2+1);
seg[k]=merge(seg[2*k],seg[2*k+1]);
}
void push(int nod,int l,int r)
{
if(!lazy[nod])return;
seg[nod].fr+=lazy[nod];
if(l!=r)
{
lazy[nod*2]+=lazy[nod];
lazy[nod*2+1]+=lazy[nod];
}
lazy[nod]=0;
}
void upd(int l, int r, int k, int a, int b, int v) {
if (b < l || r < a) return;
push(k,l,r);
if (a <= l && r <= b) {
lazy[k] += v;
push(k,l,r);
return;
}
int m = (l + r) / 2;
upd(l, m, k*2, a, b, v);
upd(m+1, r, k*2+1, a, b, v);
push(k*2,l,m);
push(k*2+1,m+1,r);
seg[k]=merge(seg[k*2],seg[k*2+1]);
}
pair<int,int> qry(int nod,int l,int r,int tl,int tr)
{
push(nod,l,r);
if(l>tr || r<tl)return make_pair(1e9,0);
if(tl<=l && r<=tr)return seg[nod];
int mid=(l+r)/2;
return merge(qry(nod*2,l,mid,tl,tr),qry(nod*2+1,mid+1,r,tl,tr));
}
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].fr, y1 = pos[a].sc;
int x2 = pos[b].fr, y2 = pos[b].sc;
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 qry(1,0,h*w-1,0,h*w-1).sc;
}
# | 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... |