#include <bits/stdc++.h>
using namespace std;
#define loop(i, a, b) for(int i = a; i <= b; i++)
#define loop_rev(i, a, b) for(int i = a; i >= b; i--)
#define all(x) x.begin(), x.end()
#define sz(x) int(x.size())
#define eb emplace_back
#define pb push_back
using ui = uint32_t;
using ll = int64_t;
set<pair<int, int>> blocks, blocks_orig;
vector<pair<int, int>> directions = {
{ -1, 0 },
{ 0, 1 },
{ 1, 0 },
{ 0, -1 }
};
bool is_removable(int x, int y) { // assumes block { x, y } exists
if (!blocks.count({ x, y })) return false;
return (!blocks.count({ x - 1, y }) && !blocks.count({ x + 1, y }))
|| (!blocks.count({ x, y - 1 }) && !blocks.count({ x, y + 1 }));
}
int solve() {
queue<pair<int, int>> q;
blocks = blocks_orig;
for(auto [ x, y ] : blocks) {
if(is_removable(x, y)) {
q.push({ x, y });
}
}
int cnt = 0;
while(!q.empty()) {
auto [ x, y ] = q.front(); q.pop();
if(!blocks.count({ x, y })) continue;
blocks.erase({ x, y });
for(auto [ dx, dy ] : directions) {
int nx = x + dx, ny = y + dy;
if(is_removable(nx, ny)) {
q.push({ nx, ny });
}
}
++cnt;
}
return cnt;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int h, w, n, q; cin >> h >> w >> n >> q;
loop(i, 1, n) {
int x, y; cin >> x >> y;
blocks_orig.insert({ x, y });
}
cout << solve() << '\n';
loop(i, 1, q) {
int x, y; cin >> x >> y;
if(blocks_orig.count({ x, y })) {
blocks_orig.erase({ x, y });
}
else {
blocks_orig.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 75 | #include <bits/stdc++.h> using namespace std; #define loop(i, a, b) for(int i = a; i <= b; i++) #define loop_rev(i, a, b) for(int i = a; i >= b; i--) #define all(x) x.begin(), x.end() #define sz(x) int(x.size()) #define eb emplace_back #define pb push_back using ui = uint32_t; using ll = int64_t; set<pair<int, int>> blocks, blocks_orig; vector<pair<int, int>> directions = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } }; bool is_removable(int x, int y) { // assumes block { x, y } exists if (!blocks.count({ x, y })) return false; return (!blocks.count({ x - 1, y }) && !blocks.count({ x + 1, y })) || (!blocks.count({ x, y - 1 }) && !blocks.count({ x, y + 1 })); } int solve() { queue<pair<int, int>> q; blocks = blocks_orig; for(auto [ x, y ] : blocks) { if(is_removable(x, y)) { q.push({ x, y }); } } int cnt = 0; while(!q.empty()) { auto [ x, y ] = q.front(); q.pop(); if(!blocks.count({ x, y })) continue; blocks.erase({ x, y }); for(auto [ dx, dy ] : directions) { int nx = x + dx, ny = y + dy; if(is_removable(nx, ny)) { q.push({ nx, ny }); } } ++cnt; } return cnt; } int main() { cin.tie(0)->sync_with_stdio(0); int h, w, n, q; cin >> h >> w >> n >> q; loop(i, 1, n) { int x, y; cin >> x >> y; blocks_orig.insert({ x, y }); } cout << solve() << '\n'; loop(i, 1, q) { int x, y; cin >> x >> y; if(blocks_orig.count({ x, y })) { blocks_orig.erase({ x, y }); } else { blocks_orig.insert({ x, y }); } cout << solve() << '\n'; } } |
English