#include<bits/stdc++.h> using namespace std; set<pair<int, int>> board; set<pair<int, int>> boardcopy; vector<int> dx = {1, 0, -1, 0}; vector<int> dy = {0, -1, 0, 1}; bool empty(int x, int y){ auto it = board.find({x, y}); if(it==board.end()) return 1; return 0; } bool dasie(int x, int y){ if(empty(x+1, y)&&empty(x-1, y)) return 1; if(empty(x, y+1) && empty(x, y-1)) return 1; return 0; } void solve(){ int k = board.size(); queue<pair<int, int>> bfs; for(auto p : board){ if(dasie(p.first, p.second)){ bfs.push(p); } } while(!bfs.empty()){ auto p = bfs.front(); if(!empty(p.first, p.second)) board.erase(p); //cout << p.first << " "<<p.second<<"\n"; cout.flush(); bfs.pop(); for(int i=0; i<4; i++){ int x = p.first + dx[i]; int y = p.second + dy[i]; if(empty(x, y)) continue; if(dasie(x, y)){ board.erase({x, y}); bfs.push({x, y}); } } } cout << k - board.size() << "\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n, m, k, q; cin >> n >> m >> k >> q; for(int i=0; i<k; i++){ int x, y; cin >> x >> y; board.insert({x, y}); boardcopy.insert({x, y}); } solve(); for(int i=0; i<q; i++){ int x, y; cin >> x >> y; if(boardcopy.count({x, y})){ boardcopy.erase({x, y}); } else{ boardcopy.insert({x, y}); } board.clear(); for(auto p : boardcopy){ board.insert(p); } 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 | #include<bits/stdc++.h> using namespace std; set<pair<int, int>> board; set<pair<int, int>> boardcopy; vector<int> dx = {1, 0, -1, 0}; vector<int> dy = {0, -1, 0, 1}; bool empty(int x, int y){ auto it = board.find({x, y}); if(it==board.end()) return 1; return 0; } bool dasie(int x, int y){ if(empty(x+1, y)&&empty(x-1, y)) return 1; if(empty(x, y+1) && empty(x, y-1)) return 1; return 0; } void solve(){ int k = board.size(); queue<pair<int, int>> bfs; for(auto p : board){ if(dasie(p.first, p.second)){ bfs.push(p); } } while(!bfs.empty()){ auto p = bfs.front(); if(!empty(p.first, p.second)) board.erase(p); //cout << p.first << " "<<p.second<<"\n"; cout.flush(); bfs.pop(); for(int i=0; i<4; i++){ int x = p.first + dx[i]; int y = p.second + dy[i]; if(empty(x, y)) continue; if(dasie(x, y)){ board.erase({x, y}); bfs.push({x, y}); } } } cout << k - board.size() << "\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n, m, k, q; cin >> n >> m >> k >> q; for(int i=0; i<k; i++){ int x, y; cin >> x >> y; board.insert({x, y}); boardcopy.insert({x, y}); } solve(); for(int i=0; i<q; i++){ int x, y; cin >> x >> y; if(boardcopy.count({x, y})){ boardcopy.erase({x, y}); } else{ boardcopy.insert({x, y}); } board.clear(); for(auto p : boardcopy){ board.insert(p); } solve(); } } |