#include <bits/stdc++.h>
#define int long long
#define pii pair<int, int>
#define piii pair<pair<int,int>, int>
#define st first.first
#define nd first.second
#define rd second
#define For(i, l, r) for (int i = l; i <= r; i++)
#define Forcin(l, r, a) \
for (int i = l; i <= r; i++) \
cin >> a[i];
#define Ford(i, l, r) for (int i = l; i >= r; i--)
#define ben(v) v.begin(), v.end()
#define LOCAL 0
#define LOCAL2 0
using namespace std;
const int M = 400005, inf=1e9;
int n,m,k,q, cur_ind, g[M][4], curk;
pii klocki[M], nei[4]={{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
map <pii, int> pola;
bool deleted[M];
void solve(){
queue <int> q;
For(i, 1, cur_ind-1){
auto [x, y]=klocki[i];
if (x<=0)
continue;
For(j, 0, 3){
int nx=x+nei[j].first, ny=y+nei[j].second;
g[i][j]=pola[{nx, ny}];
}
deleted[i]=0;
if(g[i][0]==g[i][2]||g[i][1]==g[i][3]){
q.push(i);
deleted[i]=1;
}
}
int del=0;
while(!q.empty()){
int v=q.front();
q.pop();
auto [x, y]=klocki[v];
del++;
For(i, 0, 3){
int nx=x+nei[i].first, ny=y+nei[i].second;
int j=(i+2)%4, ind=pola[{nx, ny}];
if (ind>0 && !deleted[ind]){
g[ind][j]=0;
if (g[ind][i]==0){
q.push(ind);
deleted[ind]=1;
}
}
}
}
cout << del<<'\n';
}
signed main()
{
cin.tie(0)->sync_with_stdio();
if (LOCAL)
freopen("gray/8.in", "r", stdin);
if (LOCAL2)
freopen("local_out.txt", "w", stdout);
cin>>n>>m>>k>>q;
For(i, 1, k){
int x, y;
cin>>x>>y;
klocki[i]={x, y};
pola[{x, y}]=i;
}
cur_ind=k+1;
curk=k;
solve();
For(i, 1, q){
int x, y;
cin>>x>>y;
int ind=pola[{x, y}];
if (ind){
pola.erase({x, y});
klocki[ind].first=-1;
curk--;
}
else {
pola[{x, y}]=cur_ind;
klocki[cur_ind]={x, y};
curk++;
cur_ind++;
}
solve();
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | #include <bits/stdc++.h> #define int long long #define pii pair<int, int> #define piii pair<pair<int,int>, int> #define st first.first #define nd first.second #define rd second #define For(i, l, r) for (int i = l; i <= r; i++) #define Forcin(l, r, a) \ for (int i = l; i <= r; i++) \ cin >> a[i]; #define Ford(i, l, r) for (int i = l; i >= r; i--) #define ben(v) v.begin(), v.end() #define LOCAL 0 #define LOCAL2 0 using namespace std; const int M = 400005, inf=1e9; int n,m,k,q, cur_ind, g[M][4], curk; pii klocki[M], nei[4]={{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; map <pii, int> pola; bool deleted[M]; void solve(){ queue <int> q; For(i, 1, cur_ind-1){ auto [x, y]=klocki[i]; if (x<=0) continue; For(j, 0, 3){ int nx=x+nei[j].first, ny=y+nei[j].second; g[i][j]=pola[{nx, ny}]; } deleted[i]=0; if(g[i][0]==g[i][2]||g[i][1]==g[i][3]){ q.push(i); deleted[i]=1; } } int del=0; while(!q.empty()){ int v=q.front(); q.pop(); auto [x, y]=klocki[v]; del++; For(i, 0, 3){ int nx=x+nei[i].first, ny=y+nei[i].second; int j=(i+2)%4, ind=pola[{nx, ny}]; if (ind>0 && !deleted[ind]){ g[ind][j]=0; if (g[ind][i]==0){ q.push(ind); deleted[ind]=1; } } } } cout << del<<'\n'; } signed main() { cin.tie(0)->sync_with_stdio(); if (LOCAL) freopen("gray/8.in", "r", stdin); if (LOCAL2) freopen("local_out.txt", "w", stdout); cin>>n>>m>>k>>q; For(i, 1, k){ int x, y; cin>>x>>y; klocki[i]={x, y}; pola[{x, y}]=i; } cur_ind=k+1; curk=k; solve(); For(i, 1, q){ int x, y; cin>>x>>y; int ind=pola[{x, y}]; if (ind){ pola.erase({x, y}); klocki[ind].first=-1; curk--; } else { pola[{x, y}]=cur_ind; klocki[cur_ind]={x, y}; curk++; cur_ind++; } solve(); } } |
English