#include <bits/stdc++.h>
#define st first
#define nd second
using namespace std;
set<pair<int, int>> blocks, cp;
bool removable(int a, int b) {
if (cp.find({a, b}) == cp.end()) return false;
if (cp.find({a, b+1}) == cp.end() && cp.find({a, b-1}) == cp.end()) return true;
if (cp.find({a+1, b}) == cp.end() && cp.find({a-1, b}) == cp.end()) return true;
return false;
}
vector<pair<int, int>> v = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
void solve() {
vector<pair<int, int>> to_rem;
cp = blocks;
for (auto p: cp) {
if (removable(p.st, p.nd)) to_rem.push_back(p);
}
int ans = 0;
while (!to_rem.empty()) {
pair<int, int> p = to_rem.back();
to_rem.pop_back();
if (cp.find(p) == cp.end()) continue;
cp.erase(p);
ans++;
for (pair<int, int> d: v) {
if (removable(p.st+d.st, p.nd+d.nd)) to_rem.push_back({p.st+d.st, p.nd+d.nd});
}
}
cout << ans << '\n';
return;
}
int main() {
ios_base::sync_with_stdio(false);
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;
blocks.insert({x, y});
}
solve();
for (int i = 0; i < q; i++) {
int a, b; cin >> a >> b;
if (blocks.find({a, b}) != blocks.end()) {
blocks.erase({a, b});
}
else blocks.insert({a, b});
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 | #include <bits/stdc++.h> #define st first #define nd second using namespace std; set<pair<int, int>> blocks, cp; bool removable(int a, int b) { if (cp.find({a, b}) == cp.end()) return false; if (cp.find({a, b+1}) == cp.end() && cp.find({a, b-1}) == cp.end()) return true; if (cp.find({a+1, b}) == cp.end() && cp.find({a-1, b}) == cp.end()) return true; return false; } vector<pair<int, int>> v = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; void solve() { vector<pair<int, int>> to_rem; cp = blocks; for (auto p: cp) { if (removable(p.st, p.nd)) to_rem.push_back(p); } int ans = 0; while (!to_rem.empty()) { pair<int, int> p = to_rem.back(); to_rem.pop_back(); if (cp.find(p) == cp.end()) continue; cp.erase(p); ans++; for (pair<int, int> d: v) { if (removable(p.st+d.st, p.nd+d.nd)) to_rem.push_back({p.st+d.st, p.nd+d.nd}); } } cout << ans << '\n'; return; } int main() { ios_base::sync_with_stdio(false); 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; blocks.insert({x, y}); } solve(); for (int i = 0; i < q; i++) { int a, b; cin >> a >> b; if (blocks.find({a, b}) != blocks.end()) { blocks.erase({a, b}); } else blocks.insert({a, b}); solve(); } } |
English