#include <iostream>
#include <unordered_set>
#include <unordered_map>
#include <queue>
using namespace std;
typedef pair<int, int> i2;
struct hash_pair {
size_t operator()(const i2 &p) const {
auto hash1 = hash<int>{}(p.first);
auto hash2 = hash<int>{}(p.second);
return hash1 ^ hash2;
}
};
unordered_set<i2, hash_pair> board;
unordered_set<i2, hash_pair> copy_board;
bool takeable(int x, int y, unordered_set<i2, hash_pair>& B) {
return (B.count({x, y}) & (!B.count({x, y-1}) & !B.count({x, y+1}) | !B.count({x-1, y}) & !B.count({x+1, y})));
}
int solve() {
queue<i2> q;
copy_board = board;
for(auto [x, y] : copy_board) {
if(takeable(x, y, copy_board)) {
q.push({x, y});
}
}
int ans = 0;
while(!q.empty()) {
auto [x, y] = q.front();
q.pop();
if(copy_board.count({x, y})) {
copy_board.erase({x, y});
ans++;
if(takeable(x-1, y, copy_board))
q.push({x-1, y});
if(takeable(x+1, y, copy_board))
q.push({x+1, y});
if(takeable(x, y-1, copy_board))
q.push({x, y-1});
if(takeable(x, y+1, copy_board))
q.push({x, y+1});
}
}
return ans;
}
int 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;
board.insert({x, y});
}
cout << solve() << "\n";
while(q--) {
int x, y; cin >> x >> y;
if(board.count({x, y})) {
board.erase({x, y});
}
else {
board.insert({x, y});
}
cout << solve() << "\n";
}
}
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 | #include <iostream> #include <unordered_set> #include <unordered_map> #include <queue> using namespace std; typedef pair<int, int> i2; struct hash_pair { size_t operator()(const i2 &p) const { auto hash1 = hash<int>{}(p.first); auto hash2 = hash<int>{}(p.second); return hash1 ^ hash2; } }; unordered_set<i2, hash_pair> board; unordered_set<i2, hash_pair> copy_board; bool takeable(int x, int y, unordered_set<i2, hash_pair>& B) { return (B.count({x, y}) & (!B.count({x, y-1}) & !B.count({x, y+1}) | !B.count({x-1, y}) & !B.count({x+1, y}))); } int solve() { queue<i2> q; copy_board = board; for(auto [x, y] : copy_board) { if(takeable(x, y, copy_board)) { q.push({x, y}); } } int ans = 0; while(!q.empty()) { auto [x, y] = q.front(); q.pop(); if(copy_board.count({x, y})) { copy_board.erase({x, y}); ans++; if(takeable(x-1, y, copy_board)) q.push({x-1, y}); if(takeable(x+1, y, copy_board)) q.push({x+1, y}); if(takeable(x, y-1, copy_board)) q.push({x, y-1}); if(takeable(x, y+1, copy_board)) q.push({x, y+1}); } } return ans; } int 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; board.insert({x, y}); } cout << solve() << "\n"; while(q--) { int x, y; cin >> x >> y; if(board.count({x, y})) { board.erase({x, y}); } else { board.insert({x, y}); } cout << solve() << "\n"; } } |
English