#include <bits/stdc++.h> #define nl '\n' using namespace std; using ll = long long; using pll = pair<ll, ll>; ll encode(pll p){ return (p.first<<30) + p.second; } pll decode(ll h){ return {h>>30, h&((1<<30)-1)}; } unordered_map<ll, int> id_map; vector<pll> coords; int n, m, k, q; void check(){ ll cnt = 0; queue<pll> q; vector<int> vis(coords.size(), 0); vis[0] = 1; for(auto [x, y]: coords){ if(!x || !y) continue; if((!id_map[encode({x+1, y})] && !id_map[encode({x-1, y})]) || (!id_map[encode({x, y+1})] && !id_map[encode({x, y-1})])){ q.push({x, y}); } } //cerr<<nl; while(q.size()){ auto [x, y] = q.front(); q.pop(); int p_id = id_map[encode({x, y})]; if(x <= 0 || x > n || y <= 0 || y > m || vis[p_id]) continue; //vis[id_map[encode({x, y})]] = 1; int status = (vis[id_map[encode({x+1, y})]] && vis[id_map[encode({x-1, y})]]) || (vis[id_map[encode({x, y+1})]] && vis[id_map[encode({x, y-1})]]); if(status){ cnt++; //cerr<<x<<' '<<y<<nl; vis[p_id] = 1; q.push({x+1, y}); q.push({x-1, y}); q.push({x, y+1}); q.push({x, y-1}); } } cout<<cnt<<nl; } int main() { cin.tie(0)->sync_with_stdio(0); cin>>n>>m>>k>>q; coords.push_back({0, 0}); for(int i=1; i<=k; i++){ int x, y; cin>>x>>y; coords.push_back({x, y}); id_map[encode({x, y})] = i; } check(); for(int j=k+1; j<=k+q; j++){ int x, y; cin>>x>>y; if(id_map[encode({x, y})]){ coords[id_map[encode({x, y})]] = {0, 0}; id_map[encode({x, y})] = 0; }else{ coords.push_back({x, y}); id_map[encode({x, y})] = coords.size()-1; } check(); } 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 | #include <bits/stdc++.h> #define nl '\n' using namespace std; using ll = long long; using pll = pair<ll, ll>; ll encode(pll p){ return (p.first<<30) + p.second; } pll decode(ll h){ return {h>>30, h&((1<<30)-1)}; } unordered_map<ll, int> id_map; vector<pll> coords; int n, m, k, q; void check(){ ll cnt = 0; queue<pll> q; vector<int> vis(coords.size(), 0); vis[0] = 1; for(auto [x, y]: coords){ if(!x || !y) continue; if((!id_map[encode({x+1, y})] && !id_map[encode({x-1, y})]) || (!id_map[encode({x, y+1})] && !id_map[encode({x, y-1})])){ q.push({x, y}); } } //cerr<<nl; while(q.size()){ auto [x, y] = q.front(); q.pop(); int p_id = id_map[encode({x, y})]; if(x <= 0 || x > n || y <= 0 || y > m || vis[p_id]) continue; //vis[id_map[encode({x, y})]] = 1; int status = (vis[id_map[encode({x+1, y})]] && vis[id_map[encode({x-1, y})]]) || (vis[id_map[encode({x, y+1})]] && vis[id_map[encode({x, y-1})]]); if(status){ cnt++; //cerr<<x<<' '<<y<<nl; vis[p_id] = 1; q.push({x+1, y}); q.push({x-1, y}); q.push({x, y+1}); q.push({x, y-1}); } } cout<<cnt<<nl; } int main() { cin.tie(0)->sync_with_stdio(0); cin>>n>>m>>k>>q; coords.push_back({0, 0}); for(int i=1; i<=k; i++){ int x, y; cin>>x>>y; coords.push_back({x, y}); id_map[encode({x, y})] = i; } check(); for(int j=k+1; j<=k+q; j++){ int x, y; cin>>x>>y; if(id_map[encode({x, y})]){ coords[id_map[encode({x, y})]] = {0, 0}; id_map[encode({x, y})] = 0; }else{ coords.push_back({x, y}); id_map[encode({x, y})] = coords.size()-1; } check(); } return 0; } |