#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define REP(i, n) FOR(i, 0, (n) - 1)
#define ssize(x) int(x.size())
template<class A, class B> auto &operator<<(ostream &o, pair<A, B> p) {
return o << '(' << p.first << ", " << p.second << ')';
}
template<class T> auto operator<<(ostream &o, T x) -> decltype(x.end(), o) {
o << '{'; int i = 0; for (auto e : x) o << (", ")+2*!i++ << e;
return o << '}';
}
#ifdef DEBUG
#define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n';
#else
#define debug(...) {}
#endif
vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {-1, 0, 1, 0};
set<pair<int, int>> present;
set<pair<int, int>> moovable;
vector<pair<int, int>> todo;
bool is_moovable(int x, int y) {
if (present.find({x, y}) == present.end())
return false;
if (moovable.find({x, y}) != moovable.end())
return false;
bool hor = (present.find({x - 1, y}) == present.end() &&
present.find({x + 1, y}) == present.end());
bool ver = (present.find({x, y - 1}) == present.end() &&
present.find({x, y + 1}) == present.end());
return hor || ver;
}
void solve() {
vector<pair<int, int>> removed;
for (auto [x, y] : moovable) {
removed.emplace_back(x, y);
present.erase({x, y});
REP(i, 4) {
int xx = x + dx[i], yy = y + dy[i];
if (is_moovable(xx, yy))
todo.emplace_back(xx, yy);
}
}
while (!todo.empty()) {
auto [x, y] = todo.back();
todo.pop_back();
auto iter = present.find({x, y});
if (iter == present.end())
continue;
removed.emplace_back(x, y);
present.erase(iter);
REP(i, 4) {
int xx = x + dx[i], yy = y + dy[i];
if (is_moovable(xx, yy))
todo.emplace_back(xx, yy);
}
}
cout << ssize(removed) << '\n';
for (auto [x, y] : removed)
present.insert({x, y});
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, k, q;
cin >> n >> m >> k >> q;
REP(i, k) {
int x, y;
cin >> x >> y;
present.insert({x, y});
}
for (auto [x, y] : present) {
if (is_moovable(x, y))
moovable.insert({x, y});
}
solve();
while (q--) {
int x, y, xx, yy;
cin >> x >> y;
auto iter = present.find({x, y});
if (iter == present.end()) {
present.insert({x, y});
if (is_moovable(x, y))
moovable.insert({x, y});
REP(i, 4) {
xx = x + dx[i], yy = y + dy[i];
auto iterr = moovable.find({xx, yy});
if (iterr != moovable.end()) {
moovable.erase(iterr);
if (is_moovable(xx, yy))
moovable.insert({xx, yy});
}
}
}
else {
present.erase({x, y});
auto iterr = moovable.find({x, y});
if (iterr != moovable.end())
moovable.erase({x, y});
REP(i, 4) {
xx = x + dx[i], yy = y + dy[i];
if (is_moovable(xx, yy))
moovable.insert({xx, yy});
}
}
solve();
}
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | #include <bits/stdc++.h> using namespace std; using LL = long long; #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) #define ssize(x) int(x.size()) template<class A, class B> auto &operator<<(ostream &o, pair<A, B> p) { return o << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &o, T x) -> decltype(x.end(), o) { o << '{'; int i = 0; for (auto e : x) o << (", ")+2*!i++ << e; return o << '}'; } #ifdef DEBUG #define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n'; #else #define debug(...) {} #endif vector<int> dx = {0, 1, 0, -1}; vector<int> dy = {-1, 0, 1, 0}; set<pair<int, int>> present; set<pair<int, int>> moovable; vector<pair<int, int>> todo; bool is_moovable(int x, int y) { if (present.find({x, y}) == present.end()) return false; if (moovable.find({x, y}) != moovable.end()) return false; bool hor = (present.find({x - 1, y}) == present.end() && present.find({x + 1, y}) == present.end()); bool ver = (present.find({x, y - 1}) == present.end() && present.find({x, y + 1}) == present.end()); return hor || ver; } void solve() { vector<pair<int, int>> removed; for (auto [x, y] : moovable) { removed.emplace_back(x, y); present.erase({x, y}); REP(i, 4) { int xx = x + dx[i], yy = y + dy[i]; if (is_moovable(xx, yy)) todo.emplace_back(xx, yy); } } while (!todo.empty()) { auto [x, y] = todo.back(); todo.pop_back(); auto iter = present.find({x, y}); if (iter == present.end()) continue; removed.emplace_back(x, y); present.erase(iter); REP(i, 4) { int xx = x + dx[i], yy = y + dy[i]; if (is_moovable(xx, yy)) todo.emplace_back(xx, yy); } } cout << ssize(removed) << '\n'; for (auto [x, y] : removed) present.insert({x, y}); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m, k, q; cin >> n >> m >> k >> q; REP(i, k) { int x, y; cin >> x >> y; present.insert({x, y}); } for (auto [x, y] : present) { if (is_moovable(x, y)) moovable.insert({x, y}); } solve(); while (q--) { int x, y, xx, yy; cin >> x >> y; auto iter = present.find({x, y}); if (iter == present.end()) { present.insert({x, y}); if (is_moovable(x, y)) moovable.insert({x, y}); REP(i, 4) { xx = x + dx[i], yy = y + dy[i]; auto iterr = moovable.find({xx, yy}); if (iterr != moovable.end()) { moovable.erase(iterr); if (is_moovable(xx, yy)) moovable.insert({xx, yy}); } } } else { present.erase({x, y}); auto iterr = moovable.find({x, y}); if (iterr != moovable.end()) moovable.erase({x, y}); REP(i, 4) { xx = x + dx[i], yy = y + dy[i]; if (is_moovable(xx, yy)) moovable.insert({xx, yy}); } } solve(); } return 0; } |
English