#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define pb push_back
#define rep(i,a,b) for(int i = a; i <= b; i++)
#define irep(i,a,b) for(int i = a; i >= b; i--)
typedef long long ll;
typedef long double ld;
//typedef __int128 int128;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef pair<double,double> pd;
typedef pair<ll,ll> pl;
set<pi> s;
set<pi> odw;
queue<pi> q;
void zero_deg(pi x){
bool up = (!s.count({x.st-1,x.nd}) || odw.count({x.st-1,x.nd}));
bool down = (!s.count({x.st+1,x.nd}) || odw.count({x.st+1,x.nd}));
bool left = (!s.count({x.st,x.nd-1}) || odw.count({x.st,x.nd-1}));
bool right = (!s.count({x.st,x.nd+1}) || odw.count({x.st,x.nd+1}));
if((up&down) || (left&right))
if(s.count(x) && !odw.count(x)){
odw.insert(x);
q.push(x);
}
}
int get_ans(){
//cout << "\n\nget_ans\n";
for(auto x:s) zero_deg(x);
int ans = 0;
while(!q.empty()){
pi x = q.front(); q.pop();
//cout << "x: " << x.st << ' ' << x.nd << '\n';
odw.insert(x); ans++;
zero_deg({x.st-1,x.nd});
zero_deg({x.st+1,x.nd});
zero_deg({x.st,x.nd-1});
zero_deg({x.st,x.nd+1});
}
return ans;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n,m,k,t; cin >> n >> m >> k >> t;
rep(i,1,k){
int x,y; cin >> x >> y; s.insert({x,y});
}
cout << get_ans() << '\n';
rep(i,1,t){
int x,y; cin >> x >> y;
if(s.count({x,y})) s.erase({x,y});
else s.insert({x,y});
odw.clear(); while(!q.empty()) q.pop();
cout << get_ans() << '\n';
}
return 0;
}
//g++ -O3 -static -Wall .cpp -std=c++17
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 | #include <bits/stdc++.h> using namespace std; #define st first #define nd second #define pb push_back #define rep(i,a,b) for(int i = a; i <= b; i++) #define irep(i,a,b) for(int i = a; i >= b; i--) typedef long long ll; typedef long double ld; //typedef __int128 int128; typedef vector<int> vi; typedef pair<int,int> pi; typedef pair<double,double> pd; typedef pair<ll,ll> pl; set<pi> s; set<pi> odw; queue<pi> q; void zero_deg(pi x){ bool up = (!s.count({x.st-1,x.nd}) || odw.count({x.st-1,x.nd})); bool down = (!s.count({x.st+1,x.nd}) || odw.count({x.st+1,x.nd})); bool left = (!s.count({x.st,x.nd-1}) || odw.count({x.st,x.nd-1})); bool right = (!s.count({x.st,x.nd+1}) || odw.count({x.st,x.nd+1})); if((up&down) || (left&right)) if(s.count(x) && !odw.count(x)){ odw.insert(x); q.push(x); } } int get_ans(){ //cout << "\n\nget_ans\n"; for(auto x:s) zero_deg(x); int ans = 0; while(!q.empty()){ pi x = q.front(); q.pop(); //cout << "x: " << x.st << ' ' << x.nd << '\n'; odw.insert(x); ans++; zero_deg({x.st-1,x.nd}); zero_deg({x.st+1,x.nd}); zero_deg({x.st,x.nd-1}); zero_deg({x.st,x.nd+1}); } return ans; } int main(){ ios::sync_with_stdio(0); cin.tie(0); int n,m,k,t; cin >> n >> m >> k >> t; rep(i,1,k){ int x,y; cin >> x >> y; s.insert({x,y}); } cout << get_ans() << '\n'; rep(i,1,t){ int x,y; cin >> x >> y; if(s.count({x,y})) s.erase({x,y}); else s.insert({x,y}); odw.clear(); while(!q.empty()) q.pop(); cout << get_ans() << '\n'; } return 0; } //g++ -O3 -static -Wall .cpp -std=c++17 |
English