이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 v){
if(!lz[v])return;
t[2*v].ff += lz[v];
t[2*v + 1].ff += lz[v];
lz[2*v] += lz[v];
lz[2*v + 1] += lz[v];
lz[v] = 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;
t[n].ff += 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... |