#include <iostream> #include <vector> #include <utility> #include <unordered_set> #include <queue> using namespace std; auto pHash = [](const pair<int, int> & p) { return (size_t)p.first<<32 + (size_t)p.second ^ (size_t)p.first; }; void input(int & n, int & m, int & k, int & q, unordered_set<pair<int, int>, decltype(pHash)> & positions, vector<pair<int, int>> & mods) { cin >> n >> m >> k >> q; mods.resize(q); for(int i = 0; i < k; i++) { pair<int, int> cur_pos; cin >> cur_pos.first >> cur_pos.second; positions.insert(cur_pos); } for(int i = 0; i < q; i++) { cin >> mods[i].first >> mods[i].second; } } bool can_remove(pair<int, int> const & pos, unordered_set<pair<int, int>, decltype(pHash)> const & positions) { return (positions.count({pos.first-1, pos.second}) + positions.count({pos.first+1, pos.second}) == 0) || (positions.count({pos.first, pos.second-1}) + positions.count({pos.first, pos.second+1}) == 0); } int get_single_result(unordered_set<pair<int, int>, decltype(pHash)> positions) { int result = 0; queue<pair<int, int>> que; vector<pair<int, int>> cand_delta{{1,0},{-1,0},{0,1},{0,-1}}; for(auto const & pos : positions) { if(can_remove(pos, positions)) { que.push(pos); } } while(!que.empty()) { pair<int, int> cur_pos = que.front(); que.pop(); if(!positions.count(cur_pos)) { continue; } positions.erase(cur_pos); result++; for(auto const & delta : cand_delta) { pair<int, int> candidate{cur_pos.first + delta.first, cur_pos.second + delta.second}; if(positions.count(candidate)) { if(can_remove(candidate, positions)) { que.push(candidate); } } } } return result; } void solve() { int n, m, k, q; unordered_set<pair<int, int>, decltype(pHash)> positions(1000, pHash); vector<pair<int, int>> mods; input(n, m, k, q, positions, mods); cout << get_single_result(positions) << "\n"; for(auto const & mod : mods) { if(positions.count(mod)) { positions.erase(mod); } else { positions.insert(mod); } cout << get_single_result(positions) << "\n"; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); solve(); 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 82 83 84 85 86 87 88 89 | #include <iostream> #include <vector> #include <utility> #include <unordered_set> #include <queue> using namespace std; auto pHash = [](const pair<int, int> & p) { return (size_t)p.first<<32 + (size_t)p.second ^ (size_t)p.first; }; void input(int & n, int & m, int & k, int & q, unordered_set<pair<int, int>, decltype(pHash)> & positions, vector<pair<int, int>> & mods) { cin >> n >> m >> k >> q; mods.resize(q); for(int i = 0; i < k; i++) { pair<int, int> cur_pos; cin >> cur_pos.first >> cur_pos.second; positions.insert(cur_pos); } for(int i = 0; i < q; i++) { cin >> mods[i].first >> mods[i].second; } } bool can_remove(pair<int, int> const & pos, unordered_set<pair<int, int>, decltype(pHash)> const & positions) { return (positions.count({pos.first-1, pos.second}) + positions.count({pos.first+1, pos.second}) == 0) || (positions.count({pos.first, pos.second-1}) + positions.count({pos.first, pos.second+1}) == 0); } int get_single_result(unordered_set<pair<int, int>, decltype(pHash)> positions) { int result = 0; queue<pair<int, int>> que; vector<pair<int, int>> cand_delta{{1,0},{-1,0},{0,1},{0,-1}}; for(auto const & pos : positions) { if(can_remove(pos, positions)) { que.push(pos); } } while(!que.empty()) { pair<int, int> cur_pos = que.front(); que.pop(); if(!positions.count(cur_pos)) { continue; } positions.erase(cur_pos); result++; for(auto const & delta : cand_delta) { pair<int, int> candidate{cur_pos.first + delta.first, cur_pos.second + delta.second}; if(positions.count(candidate)) { if(can_remove(candidate, positions)) { que.push(candidate); } } } } return result; } void solve() { int n, m, k, q; unordered_set<pair<int, int>, decltype(pHash)> positions(1000, pHash); vector<pair<int, int>> mods; input(n, m, k, q, positions, mods); cout << get_single_result(positions) << "\n"; for(auto const & mod : mods) { if(positions.count(mod)) { positions.erase(mod); } else { positions.insert(mod); } cout << get_single_result(positions) << "\n"; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); solve(); return 0; } |