#include<bits/stdc++.h> using namespace std; set<pair<int, int>> secik; set<pair<int, int>> dousun; set<pair<int, int>> oryg; vector<pair<int, int>> kompas = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; bool check(pair<int, int> ele){ int x = ele.first; int y = ele.second; if(secik.find({x-1, y}) == secik.end() && secik.find({x+1, y}) == secik.end()){ return 1; } if(secik.find({x, y-1}) == secik.end() && secik.find({x, y+1}) == secik.end()){ return 1; } return 0; } int policz(){ int proz = oryg.size(); secik = oryg; for(pair<int, int> ele : secik){ if(check(ele)){ dousun.insert(ele); } } while(!dousun.empty()){ pair<int, int> ele = *(dousun.begin()); dousun.erase(dousun.begin()); secik.erase(ele); for(pair<int, int> d : kompas){ int nx = ele.first+d.first; int ny = ele.second+d.second; if((secik.find({nx, ny}) != secik.end()) && check({nx, ny})){ //cerr << ele.first << " " << ele.second << " : " << nx << " " << ny << "\n"; dousun.insert({nx, ny}); } } } return proz - secik.size(); } int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(0); int n, m, k, q; cin >> n >> m >> k >> q; for(int i = 1; i <= k ; i++){ int x, y; cin >> x >> y; oryg.insert({x, y}); } int wynik = policz(); cout << wynik << "\n"; while(q--){ int x, y; cin >> x >> y; if(oryg.find({x, y}) != oryg.end()){ oryg.erase({x, y}); } else{ oryg.insert({x, y}); } wynik = policz(); cout << wynik << "\n"; } 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 | #include<bits/stdc++.h> using namespace std; set<pair<int, int>> secik; set<pair<int, int>> dousun; set<pair<int, int>> oryg; vector<pair<int, int>> kompas = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; bool check(pair<int, int> ele){ int x = ele.first; int y = ele.second; if(secik.find({x-1, y}) == secik.end() && secik.find({x+1, y}) == secik.end()){ return 1; } if(secik.find({x, y-1}) == secik.end() && secik.find({x, y+1}) == secik.end()){ return 1; } return 0; } int policz(){ int proz = oryg.size(); secik = oryg; for(pair<int, int> ele : secik){ if(check(ele)){ dousun.insert(ele); } } while(!dousun.empty()){ pair<int, int> ele = *(dousun.begin()); dousun.erase(dousun.begin()); secik.erase(ele); for(pair<int, int> d : kompas){ int nx = ele.first+d.first; int ny = ele.second+d.second; if((secik.find({nx, ny}) != secik.end()) && check({nx, ny})){ //cerr << ele.first << " " << ele.second << " : " << nx << " " << ny << "\n"; dousun.insert({nx, ny}); } } } return proz - secik.size(); } int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(0); int n, m, k, q; cin >> n >> m >> k >> q; for(int i = 1; i <= k ; i++){ int x, y; cin >> x >> y; oryg.insert({x, y}); } int wynik = policz(); cout << wynik << "\n"; while(q--){ int x, y; cin >> x >> y; if(oryg.find({x, y}) != oryg.end()){ oryg.erase({x, y}); } else{ oryg.insert({x, y}); } wynik = policz(); cout << wynik << "\n"; } return 0; } |