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>
#define x first
#define y second
#define ff first
#define ss second
using namespace std;
const int NMAX = 4000005;
const int dxa[] = {1, 0, -1, 0};
const int dya[] = {0, 1, 0, -1};
typedef pair<int, int> pii;
pii t[4*NMAX];
int lz[4*NMAX];
pii seats[4000555];
int tsz, H, W;
void updateA(pii pos);
void deleteA(pii pos);
void updateB(pii pos);
void deleteB(pii pos);
int secondSmallestNeighbour(pii pos);
pii getB(pii pos);
bool ok(pii pos);
void addInterval(int l, int r);
void subInterval(int l, int r);
void rewrite(pii pos, int val);
vector<vector<int>> arr;
int ln(int n){
return 2*n;
}
int rn(int n){
return 2*n + 1;
}
pii merge(pii a, pii b){
if(a.ff < b.ff)return a;
if(a.ff > b.ff)return b;
return {a.ff, a.ss + b.ss};
}
void push(int n){
if(lz[n]){
t[n].first += lz[n];
lz[ln(n)] += lz[n];
lz[rn(n)] += lz[n];
lz[n] = 0;
}
}
void build(int n, int l, int r){
if(l == r){
t[n] = {0, 1};
return;
}
int pivot = (l+r)>>1;
build(ln(n), l, pivot);
build(rn(n), pivot + 1, r);
t[n] = merge(t[ln(n)], t[rn(n)]);
}
void update(int n, int l, int r, int ql, int qr, int add){
push(n);
if(ql <= l && r <= qr){
lz[n] += add;
push(n);
return;
}
int pivot = (l+r)>>1;
if(ql <= pivot)
update(ln(n), l, pivot, ql, qr, add);
if(qr > pivot)
update(rn(n), pivot+1, r, ql, qr, add);
push(ln(n));
push(rn(n));
t[n] = merge(t[ln(n)], t[rn(n)]);
}
void give_initial_chart(int _H, int _W, vector<int> R, vector<int> C) {
W = _W;
H = _H;
tsz = H*W;
build(1, 1, tsz);
arr = vector<vector<int>>(H+1, vector<int>(W+1, 0));
for(int i=0; i<H*W; ++i){
arr[R[i] + 1][C[i] + 1] = i + 1;
seats[i] = {R[i] + 1, C[i] + 1};
}
for(int i = 1; i<=H; ++i){
for(int j = 1; j<=W; ++j){
updateA({i, j});
updateB({i, j});
}
}
}
int swap_seats(int a, int b) {
pii pa = seats[a];
pii pb = seats[b];
rewrite(pa, b + 1);
rewrite(pb, a + 1);
swap(seats[a], seats[b]);
return (t[1].first == 1 ? t[1].second : 0);
}
void rewrite(pii pos, int val){
// delete previous connections
deleteA(pos);
for(int i=0; i<4; ++i){
pii aux = pos;
aux.x += dxa[i];
aux.y += dya[i];
if(ok(aux)){
deleteA(aux);
}
}
deleteB(pos);
for(int i=2; i<4; ++i){
pii aux = pos;
aux.x += dxa[i];
aux.y += dya[i];
if(ok(aux)){
deleteB(aux);
}
}
arr[pos.x][pos.y] = val;
updateA(pos);
for(int i=0; i<4; ++i){
pii aux = pos;
aux.x += dxa[i];
aux.y += dya[i];
if(ok(aux)){
updateA(aux);
}
}
updateB(pos);
for(int i=2; i<4; ++i){
pii aux = pos;
aux.x += dxa[i];
aux.y += dya[i];
if(ok(aux)){
updateB(aux);
}
}
}
void deleteA(pii pos){
int l = secondSmallestNeighbour(pos);
int r = arr[pos.x][pos.y];
if(r > l){
subInterval(l, r - 1);
}
}
void updateA(pii pos){
int l = secondSmallestNeighbour(pos);
int r = arr[pos.x][pos.y];
if(r > l){
addInterval(l, r - 1);
}
}
void deleteB(pii pos){
pii p = getB(pos);
if(p.y > p.x){
subInterval(p.x, p.y - 1);
}
}
void updateB(pii pos){
pii p = getB(pos);
if(p.y > p.x){
addInterval(p.x, p.y - 1);
}
}
int secondSmallestNeighbour(pii pos){
set<int> countSet;
for(int i=0; i<4; ++i){
pii aux = pos;
aux.x += dxa[i];
aux.y += dya[i];
if(ok(aux)){
countSet.insert(arr[aux.x][aux.y]);
}
}
if(countSet.size() > 1){
return *(++countSet.begin());
} else {
return tsz+1;
}
}
pii getB(pii pos){
int l = arr[pos.x][pos.y];
int r = tsz + 1;
for(int i=0; i<4; ++i){
pii aux = pos;
aux.x += dxa[i];
aux.y += dya[i];
if(ok(aux)){
if(i < 2)
r = min(r, arr[aux.x][aux.y]);
}
}
return {l, r};
}
bool ok(pii pos){
if(pos.x > 0 && pos.x <= H){
if(pos.y > 0 && pos.y <= W){
return 1;
}
}
return 0;
}
void addInterval(int l, int r){
update(1, 1, tsz, l, r, +1);
}
void subInterval(int l, int r){
update(1, 1, tsz, 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... |