#include "bits/stdc++.h" #define int long long #define pi pair<long long, long long> #define a3 array<long long, 3> #define meow main using namespace std; signed meow() { cin.tie((ostream *)!ios::sync_with_stdio(false)); int n, m, k, q; cin >> n >> m >> k >> q; set<pi> plansz; while (k--) { int x, y; cin >> x >> y; plansz.insert({x, y}); } auto solve = [&]() { set<pair<int, pi>> hor; for (auto &[x, y] : plansz) hor.insert( {(int)(plansz.count({x, y - 1}) + plansz.count({x, y + 1})), {x, y}}); set<pair<int, pi>> ver; for (auto &[x, y] : plansz) ver.insert( {(int)(plansz.count({x - 1, y}) + plansz.count({x + 1, y})), {x, y}}); int res = 0; while (true) { if (hor.empty()) break; auto [val, coords] = *hor.lower_bound({-1, {-1, -1}}); bool brk = true; if (val == 0) { brk = false; hor.erase(hor.begin()); ver.erase({0, coords}); ver.erase({1, coords}); ver.erase({2, coords}); res++; if (ver.count({1, {coords.first + 1, coords.second}})) { ver.erase({1, {coords.first + 1, coords.second}}); ver.insert({0, {coords.first + 1, coords.second}}); } if (ver.count({2, {coords.first + 1, coords.second}})) { ver.erase({2, {coords.first + 1, coords.second}}); ver.insert({1, {coords.first + 1, coords.second}}); } if (ver.count({1, {coords.first - 1, coords.second}})) { ver.erase({1, {coords.first - 1, coords.second}}); ver.insert({0, {coords.first - 1, coords.second}}); } if (ver.count({2, {coords.first - 1, coords.second}})) { ver.erase({2, {coords.first - 1, coords.second}}); ver.insert({1, {coords.first - 1, coords.second}}); } continue; } if (hor.empty()) break; auto [_val, _coords] = *ver.lower_bound({-1, {-1, -1}}); if (_val == 0) { brk = false; ver.erase(ver.begin()); hor.erase({0, _coords}); hor.erase({1, _coords}); hor.erase({2, _coords}); res++; if (hor.count({1, {_coords.first, _coords.second + 1}})) { hor.erase({1, {_coords.first, _coords.second + 1}}); hor.insert({0, {_coords.first, _coords.second + 1}}); } if (hor.count({2, {_coords.first, _coords.second + 1}})) { hor.erase({2, {_coords.first, _coords.second + 1}}); hor.insert({1, {_coords.first, _coords.second + 1}}); } if (hor.count({1, {_coords.first, _coords.second - 1}})) { hor.erase({1, {_coords.first, _coords.second - 1}}); hor.insert({0, {_coords.first, _coords.second - 1}}); } if (hor.count({2, {_coords.first, _coords.second - 1}})) { hor.erase({2, {_coords.first, _coords.second - 1}}); hor.insert({1, {_coords.first, _coords.second - 1}}); } } if (brk) break; } return res; }; cout << solve() << '\n'; while (q--) { int x, y; cin >> x >> y; if (plansz.count({x, y})) plansz.erase({x, y}); else plansz.insert({x, y}); cout << solve() << '\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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | #include "bits/stdc++.h" #define int long long #define pi pair<long long, long long> #define a3 array<long long, 3> #define meow main using namespace std; signed meow() { cin.tie((ostream *)!ios::sync_with_stdio(false)); int n, m, k, q; cin >> n >> m >> k >> q; set<pi> plansz; while (k--) { int x, y; cin >> x >> y; plansz.insert({x, y}); } auto solve = [&]() { set<pair<int, pi>> hor; for (auto &[x, y] : plansz) hor.insert( {(int)(plansz.count({x, y - 1}) + plansz.count({x, y + 1})), {x, y}}); set<pair<int, pi>> ver; for (auto &[x, y] : plansz) ver.insert( {(int)(plansz.count({x - 1, y}) + plansz.count({x + 1, y})), {x, y}}); int res = 0; while (true) { if (hor.empty()) break; auto [val, coords] = *hor.lower_bound({-1, {-1, -1}}); bool brk = true; if (val == 0) { brk = false; hor.erase(hor.begin()); ver.erase({0, coords}); ver.erase({1, coords}); ver.erase({2, coords}); res++; if (ver.count({1, {coords.first + 1, coords.second}})) { ver.erase({1, {coords.first + 1, coords.second}}); ver.insert({0, {coords.first + 1, coords.second}}); } if (ver.count({2, {coords.first + 1, coords.second}})) { ver.erase({2, {coords.first + 1, coords.second}}); ver.insert({1, {coords.first + 1, coords.second}}); } if (ver.count({1, {coords.first - 1, coords.second}})) { ver.erase({1, {coords.first - 1, coords.second}}); ver.insert({0, {coords.first - 1, coords.second}}); } if (ver.count({2, {coords.first - 1, coords.second}})) { ver.erase({2, {coords.first - 1, coords.second}}); ver.insert({1, {coords.first - 1, coords.second}}); } continue; } if (hor.empty()) break; auto [_val, _coords] = *ver.lower_bound({-1, {-1, -1}}); if (_val == 0) { brk = false; ver.erase(ver.begin()); hor.erase({0, _coords}); hor.erase({1, _coords}); hor.erase({2, _coords}); res++; if (hor.count({1, {_coords.first, _coords.second + 1}})) { hor.erase({1, {_coords.first, _coords.second + 1}}); hor.insert({0, {_coords.first, _coords.second + 1}}); } if (hor.count({2, {_coords.first, _coords.second + 1}})) { hor.erase({2, {_coords.first, _coords.second + 1}}); hor.insert({1, {_coords.first, _coords.second + 1}}); } if (hor.count({1, {_coords.first, _coords.second - 1}})) { hor.erase({1, {_coords.first, _coords.second - 1}}); hor.insert({0, {_coords.first, _coords.second - 1}}); } if (hor.count({2, {_coords.first, _coords.second - 1}})) { hor.erase({2, {_coords.first, _coords.second - 1}}); hor.insert({1, {_coords.first, _coords.second - 1}}); } } if (brk) break; } return res; }; cout << solve() << '\n'; while (q--) { int x, y; cin >> x >> y; if (plansz.count({x, y})) plansz.erase({x, y}); else plansz.insert({x, y}); cout << solve() << '\n'; } return 0; } |