#include <bits/stdc++.h>
using namespace std;
int n, m, k, q;
bool is_removable(set<pair<int, int>> &b, int x, int y) {
bool h = ((y == 1) || (b.find({x, y - 1}) == b.end())) &&
((y == m) || (b.find({x, y + 1}) == b.end()));
bool v = ((x == 1) || (b.find({x - 1, y}) == b.end())) &&
((x == n) || (b.find({x + 1, y}) == b.end()));
return h || v;
}
int remove(set<pair<int, int>> &blocks) {
queue<pair<int ,int>> q;
int cnt = 0;
set<pair<int, int>> temp = blocks;
for(const auto &[x, y] : temp) {
if(is_removable(temp, x, y))
q.push({x, y});
}
while(!q.empty()) {
auto b = q.front();
q.pop();
if(temp.find(b) == temp.end()) {
continue;
}
temp.erase(b);
cnt++;
vector<pair<int, int>> neighbours = {
{b.first - 1, b.second}, {b.first + 1, b.second},
{b.first, b.second - 1}, {b.first, b.second + 1}
};
for(const auto &[x, y] : neighbours) {
if(temp.find({x, y}) != temp.end() && is_removable(temp, x, y)) {
q.push({x, y});
}
}
}
return cnt;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> k >> q;
set<pair<int, int>> blocks;
for(int i = 0; i < k; i++) {
int x, y;
cin >> x >> y;
blocks.insert({x, y});
}
int removed = remove(blocks);
cout << removed << '\n';
for(int i = 0; i < q; i++) {
int x, y;
cin >> x >> y;
if(blocks.find({x, y}) == blocks.end()) {
blocks.insert({x, y});
} else {
blocks.erase({x, y});
}
removed = remove(blocks);
cout << removed << '\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 75 76 77 78 79 80 81 82 83 84 85 86 87 | #include <bits/stdc++.h> using namespace std; int n, m, k, q; bool is_removable(set<pair<int, int>> &b, int x, int y) { bool h = ((y == 1) || (b.find({x, y - 1}) == b.end())) && ((y == m) || (b.find({x, y + 1}) == b.end())); bool v = ((x == 1) || (b.find({x - 1, y}) == b.end())) && ((x == n) || (b.find({x + 1, y}) == b.end())); return h || v; } int remove(set<pair<int, int>> &blocks) { queue<pair<int ,int>> q; int cnt = 0; set<pair<int, int>> temp = blocks; for(const auto &[x, y] : temp) { if(is_removable(temp, x, y)) q.push({x, y}); } while(!q.empty()) { auto b = q.front(); q.pop(); if(temp.find(b) == temp.end()) { continue; } temp.erase(b); cnt++; vector<pair<int, int>> neighbours = { {b.first - 1, b.second}, {b.first + 1, b.second}, {b.first, b.second - 1}, {b.first, b.second + 1} }; for(const auto &[x, y] : neighbours) { if(temp.find({x, y}) != temp.end() && is_removable(temp, x, y)) { q.push({x, y}); } } } return cnt; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k >> q; set<pair<int, int>> blocks; for(int i = 0; i < k; i++) { int x, y; cin >> x >> y; blocks.insert({x, y}); } int removed = remove(blocks); cout << removed << '\n'; for(int i = 0; i < q; i++) { int x, y; cin >> x >> y; if(blocks.find({x, y}) == blocks.end()) { blocks.insert({x, y}); } else { blocks.erase({x, y}); } removed = remove(blocks); cout << removed << '\n'; } } |
English