#include <iostream> #include <set> #include <vector> using namespace std; struct Board { int n, m; set<pair<int, int>> blocks; Board(int n, int m) : n(n), m(m) {} bool isRemovable(int x, int y, const set<pair<int, int>>& currentBlocks) { bool left = (currentBlocks.count({x, y - 1}) == 0); bool right = (currentBlocks.count({x, y + 1}) == 0); bool up = (currentBlocks.count({x - 1, y}) == 0); bool down = (currentBlocks.count({x + 1, y}) == 0); return (left && right) || (up && down); } int countMaxRemovable() { set<pair<int, int>> currentBlocks = blocks; set<pair<int, int>> removable; for (auto [x, y] : currentBlocks) { if (isRemovable(x, y, currentBlocks)) { removable.insert({x, y}); } } int count = 0; while (!removable.empty()) { auto it = removable.begin(); int x = it->first, y = it->second; removable.erase(it); currentBlocks.erase({x, y}); count++; for (int dx = -1; dx <= 1; dx++) { for (int dy = -1; dy <= 1; dy++) { if (dx * dy == 0 && dx != dy) { int nx = x + dx, ny = y + dy; if (currentBlocks.count({nx, ny}) && isRemovable(nx, ny, currentBlocks)) { removable.insert({nx, ny}); } } } } } return count; } void add(int x, int y) { blocks.insert({x, y}); } void remove(int x, int y) { blocks.erase({x, y}); } }; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m, k, q; cin >> n >> m >> k >> q; Board board(n, m); for (int i = 0; i < k; i++) { int x, y; cin >> x >> y; board.add(x, y); } cout << board.countMaxRemovable() << "\n"; for (int i = 0; i < q; i++) { int x, y; cin >> x >> y; if (board.blocks.count({x, y})) { board.remove(x, y); } else { board.add(x, y); } cout << board.countMaxRemovable() << "\n"; } 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 <set> #include <vector> using namespace std; struct Board { int n, m; set<pair<int, int>> blocks; Board(int n, int m) : n(n), m(m) {} bool isRemovable(int x, int y, const set<pair<int, int>>& currentBlocks) { bool left = (currentBlocks.count({x, y - 1}) == 0); bool right = (currentBlocks.count({x, y + 1}) == 0); bool up = (currentBlocks.count({x - 1, y}) == 0); bool down = (currentBlocks.count({x + 1, y}) == 0); return (left && right) || (up && down); } int countMaxRemovable() { set<pair<int, int>> currentBlocks = blocks; set<pair<int, int>> removable; for (auto [x, y] : currentBlocks) { if (isRemovable(x, y, currentBlocks)) { removable.insert({x, y}); } } int count = 0; while (!removable.empty()) { auto it = removable.begin(); int x = it->first, y = it->second; removable.erase(it); currentBlocks.erase({x, y}); count++; for (int dx = -1; dx <= 1; dx++) { for (int dy = -1; dy <= 1; dy++) { if (dx * dy == 0 && dx != dy) { int nx = x + dx, ny = y + dy; if (currentBlocks.count({nx, ny}) && isRemovable(nx, ny, currentBlocks)) { removable.insert({nx, ny}); } } } } } return count; } void add(int x, int y) { blocks.insert({x, y}); } void remove(int x, int y) { blocks.erase({x, y}); } }; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m, k, q; cin >> n >> m >> k >> q; Board board(n, m); for (int i = 0; i < k; i++) { int x, y; cin >> x >> y; board.add(x, y); } cout << board.countMaxRemovable() << "\n"; for (int i = 0; i < q; i++) { int x, y; cin >> x >> y; if (board.blocks.count({x, y})) { board.remove(x, y); } else { board.add(x, y); } cout << board.countMaxRemovable() << "\n"; } return 0; } |