#include <bits/stdc++.h> using i64 = long long; using u64 = unsigned long long; using u32 = unsigned; using u128 = unsigned __int128; constexpr int inf = 1E9; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n, m, k, q; std::cin >> n >> m >> k >> q; std::set<std::array<int, 2>> s, t; std::vector<std::set<std::array<int, 3>>> d1(n + m), d2(n + m); int path = 0; auto check = [&](auto &s, auto it) { auto [l, r, _] = *it; if (_ == 0) { return 0; } if (it == s.begin() || std::next(it) == s.end()) { return 0; } if ((*std::prev(it))[1] == l && (*std::next(it))[0] == r) { return r - l; } return 0; }; auto addLineP = [&](auto &s, int x) { int L = x, R = x + 1; auto it = s.lower_bound({L}); if (it != s.end()) { auto [l, r, t] = *it; if (l == R && t == 1) { R = r; it = s.erase(it); } } if (it != s.begin()) { auto prv = std::prev(it); auto [l, r, t] = *prv; if (L == r && t == 1) { L = l; s.erase(prv); } } it = s.insert({L, R, 1}).first; path += check(s, it); }; auto addLineV = [&](auto &s, int x) { auto it = s.insert({x, x + 1, 0}).first; if (it != s.begin()) { path += check(s, std::prev(it)); } if (std::next(it) != s.end()) { path += check(s, std::next(it)); } }; auto delLineP = [&](auto &s, int x) { auto it = std::prev(s.lower_bound({x + 1, -inf, -inf})); auto [L, R, _] = *it; path -= check(s, it); s.erase(it); if (L < x) { s.insert({L, x, 1}); } if (R > x + 1) { s.insert({x + 1, R, 1}); } }; auto delLineV = [&](auto &s, int x) { auto it = s.find({x, x + 1, 0}); if (it != s.begin()) { path -= check(s, std::prev(it)); } if (std::next(it) != s.end()) { path -= check(s, std::next(it)); } s.erase(it); }; auto addP = [&](int x, int y) { addLineP(d1[x - y + m - 1], x + y); addLineP(d1[x - y + m], x + y); addLineP(d2[x + y - 2], x - y); addLineP(d2[x + y - 1], x - y); }; auto addV = [&](int x, int y) { addLineV(d1[x - y + m - 1], x + y); addLineV(d1[x - y + m], x + y); addLineV(d2[x + y - 2], x - y); addLineV(d2[x + y - 1], x - y); }; auto delP = [&](int x, int y) { delLineP(d1[x - y + m - 1], x + y); delLineP(d1[x - y + m], x + y); delLineP(d2[x + y - 2], x - y); delLineP(d2[x + y - 1], x - y); }; auto delV = [&](int x, int y) { delLineV(d1[x - y + m - 1], x + y); delLineV(d1[x - y + m], x + y); delLineV(d2[x + y - 2], x - y); delLineV(d2[x + y - 1], x - y); }; auto check2x2 = [&](int x, int y) { if (!s.contains({x, y})) { return; } bool ok = false; for (int dx = -1; dx <= 0; dx++) { for (int dy = -1; dy <= 0; dy++) { bool f = true; for (int i = 0; i <= 1; i++) { for (int j = 0; j <= 1; j++) { if (!s.contains({x + dx + i, y + dy + j})) { f = false; } } } if (f) { ok = true; } } } if (ok) { if (!t.contains({x, y})) { t.insert({x, y}); delP(x, y); addV(x, y); } } else { if (t.contains({x, y})) { t.erase({x, y}); delV(x, y); addP(x, y); } } }; auto add = [&](int x, int y) { addP(x, y); s.insert({x, y}); for (int dx = -1; dx <= 1; dx++) { for (int dy = -1; dy <= 1; dy++) { check2x2(x + dx, y + dy); } } }; auto del = [&](int x, int y) { if (t.contains({x, y})) { delV(x, y); t.erase({x, y}); } else { delP(x, y); } s.erase({x, y}); for (int dx = -1; dx <= 1; dx++) { for (int dy = -1; dy <= 1; dy++) { check2x2(x + dx, y + dy); } } }; for (int i = 0; i < k; i++) { int x, y; std::cin >> x >> y; add(x, y); } std::cout << s.size() - (t.size() + path) << "\n"; for (int i = 0; i < q; i++) { int x, y; std::cin >> x >> y; if (s.contains({x, y})) { del(x, y); } else { add(x, y); } std::cout << s.size() - (t.size() + path) << "\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 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | #include <bits/stdc++.h> using i64 = long long; using u64 = unsigned long long; using u32 = unsigned; using u128 = unsigned __int128; constexpr int inf = 1E9; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n, m, k, q; std::cin >> n >> m >> k >> q; std::set<std::array<int, 2>> s, t; std::vector<std::set<std::array<int, 3>>> d1(n + m), d2(n + m); int path = 0; auto check = [&](auto &s, auto it) { auto [l, r, _] = *it; if (_ == 0) { return 0; } if (it == s.begin() || std::next(it) == s.end()) { return 0; } if ((*std::prev(it))[1] == l && (*std::next(it))[0] == r) { return r - l; } return 0; }; auto addLineP = [&](auto &s, int x) { int L = x, R = x + 1; auto it = s.lower_bound({L}); if (it != s.end()) { auto [l, r, t] = *it; if (l == R && t == 1) { R = r; it = s.erase(it); } } if (it != s.begin()) { auto prv = std::prev(it); auto [l, r, t] = *prv; if (L == r && t == 1) { L = l; s.erase(prv); } } it = s.insert({L, R, 1}).first; path += check(s, it); }; auto addLineV = [&](auto &s, int x) { auto it = s.insert({x, x + 1, 0}).first; if (it != s.begin()) { path += check(s, std::prev(it)); } if (std::next(it) != s.end()) { path += check(s, std::next(it)); } }; auto delLineP = [&](auto &s, int x) { auto it = std::prev(s.lower_bound({x + 1, -inf, -inf})); auto [L, R, _] = *it; path -= check(s, it); s.erase(it); if (L < x) { s.insert({L, x, 1}); } if (R > x + 1) { s.insert({x + 1, R, 1}); } }; auto delLineV = [&](auto &s, int x) { auto it = s.find({x, x + 1, 0}); if (it != s.begin()) { path -= check(s, std::prev(it)); } if (std::next(it) != s.end()) { path -= check(s, std::next(it)); } s.erase(it); }; auto addP = [&](int x, int y) { addLineP(d1[x - y + m - 1], x + y); addLineP(d1[x - y + m], x + y); addLineP(d2[x + y - 2], x - y); addLineP(d2[x + y - 1], x - y); }; auto addV = [&](int x, int y) { addLineV(d1[x - y + m - 1], x + y); addLineV(d1[x - y + m], x + y); addLineV(d2[x + y - 2], x - y); addLineV(d2[x + y - 1], x - y); }; auto delP = [&](int x, int y) { delLineP(d1[x - y + m - 1], x + y); delLineP(d1[x - y + m], x + y); delLineP(d2[x + y - 2], x - y); delLineP(d2[x + y - 1], x - y); }; auto delV = [&](int x, int y) { delLineV(d1[x - y + m - 1], x + y); delLineV(d1[x - y + m], x + y); delLineV(d2[x + y - 2], x - y); delLineV(d2[x + y - 1], x - y); }; auto check2x2 = [&](int x, int y) { if (!s.contains({x, y})) { return; } bool ok = false; for (int dx = -1; dx <= 0; dx++) { for (int dy = -1; dy <= 0; dy++) { bool f = true; for (int i = 0; i <= 1; i++) { for (int j = 0; j <= 1; j++) { if (!s.contains({x + dx + i, y + dy + j})) { f = false; } } } if (f) { ok = true; } } } if (ok) { if (!t.contains({x, y})) { t.insert({x, y}); delP(x, y); addV(x, y); } } else { if (t.contains({x, y})) { t.erase({x, y}); delV(x, y); addP(x, y); } } }; auto add = [&](int x, int y) { addP(x, y); s.insert({x, y}); for (int dx = -1; dx <= 1; dx++) { for (int dy = -1; dy <= 1; dy++) { check2x2(x + dx, y + dy); } } }; auto del = [&](int x, int y) { if (t.contains({x, y})) { delV(x, y); t.erase({x, y}); } else { delP(x, y); } s.erase({x, y}); for (int dx = -1; dx <= 1; dx++) { for (int dy = -1; dy <= 1; dy++) { check2x2(x + dx, y + dy); } } }; for (int i = 0; i < k; i++) { int x, y; std::cin >> x >> y; add(x, y); } std::cout << s.size() - (t.size() + path) << "\n"; for (int i = 0; i < q; i++) { int x, y; std::cin >> x >> y; if (s.contains({x, y})) { del(x, y); } else { add(x, y); } std::cout << s.size() - (t.size() + path) << "\n"; } return 0; } |