#include <bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned ll #define pb push_back #define pii pair<int,int> #define pl pair<ll,ll> #define F first #define S second #define pq priority_queue #define all(x) x.begin(), x.end() #define deb(x) cout << #x << " = " << x << '\n'; #define deb2(x,y) cout << #x << " = " << x << ", " << #y << " = " << y << '\n'; constexpr int M = 1e9+7; constexpr int N = 1e6+7; constexpr bool debug = 0; map<pii,int> takable; vector<pii> rw = {{0,1},{1,0},{0,-1},{-1,0}}; set<pii> vtx; int n, m, kok, q; void proc(){ int sc = 0; queue<pii> todo; for(auto k: vtx){ takable[k] = -1; int ver = 0, hor = 0; if(takable[{k.F,k.S+1}] || takable[{k.F,k.S-1}]) ver++; if(takable[{k.F+1,k.S}] || takable[{k.F-1,k.S}]) hor++; if(ver == 0 || hor == 0){ takable[k] = 1; todo.push(k); } } while(!todo.empty()){ sc++; pii ob = todo.front(); todo.pop(); for(auto k: rw){ pii totak = {ob.F+k.F, ob.S+k.S}; if(takable[totak] == 0 || takable[totak] == 1) continue; int ver = 0, hor = 0; if(takable[{totak.F,totak.S+1}] == -1 || takable[{totak.F,totak.S-1}] == -1) ver++; if(takable[{totak.F+1,totak.S}] == -1 || takable[{totak.F-1,totak.S}] == -1) hor++; if(ver == 0 || hor == 0){ takable[totak] = 1; todo.push(totak); } } } cout << sc << '\n'; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> kok >> q; for(int i = 0; i < kok; i++){ pii a; cin >> a.F >> a.S; vtx.insert(a); takable[a] = -1; } proc(); while(q--){ pii ok; cin >> ok.F >> ok.S; if(vtx.find(ok) != vtx.end()){ vtx.erase(ok); takable[ok] = 0; } else{ vtx.insert(ok); takable[ok] = -1; } proc(); } return 0; }
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 | #include <bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned ll #define pb push_back #define pii pair<int,int> #define pl pair<ll,ll> #define F first #define S second #define pq priority_queue #define all(x) x.begin(), x.end() #define deb(x) cout << #x << " = " << x << '\n'; #define deb2(x,y) cout << #x << " = " << x << ", " << #y << " = " << y << '\n'; constexpr int M = 1e9+7; constexpr int N = 1e6+7; constexpr bool debug = 0; map<pii,int> takable; vector<pii> rw = {{0,1},{1,0},{0,-1},{-1,0}}; set<pii> vtx; int n, m, kok, q; void proc(){ int sc = 0; queue<pii> todo; for(auto k: vtx){ takable[k] = -1; int ver = 0, hor = 0; if(takable[{k.F,k.S+1}] || takable[{k.F,k.S-1}]) ver++; if(takable[{k.F+1,k.S}] || takable[{k.F-1,k.S}]) hor++; if(ver == 0 || hor == 0){ takable[k] = 1; todo.push(k); } } while(!todo.empty()){ sc++; pii ob = todo.front(); todo.pop(); for(auto k: rw){ pii totak = {ob.F+k.F, ob.S+k.S}; if(takable[totak] == 0 || takable[totak] == 1) continue; int ver = 0, hor = 0; if(takable[{totak.F,totak.S+1}] == -1 || takable[{totak.F,totak.S-1}] == -1) ver++; if(takable[{totak.F+1,totak.S}] == -1 || takable[{totak.F-1,totak.S}] == -1) hor++; if(ver == 0 || hor == 0){ takable[totak] = 1; todo.push(totak); } } } cout << sc << '\n'; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> kok >> q; for(int i = 0; i < kok; i++){ pii a; cin >> a.F >> a.S; vtx.insert(a); takable[a] = -1; } proc(); while(q--){ pii ok; cin >> ok.F >> ok.S; if(vtx.find(ok) != vtx.end()){ vtx.erase(ok); takable[ok] = 0; } else{ vtx.insert(ok); takable[ok] = -1; } proc(); } return 0; } |