#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pi; #define sz(x) (int)(x).size() struct pair_hash { inline std::size_t operator()(const std::pair<int, int>& v) const { return v.first * 1000000007LL + v.second; } }; typedef unordered_set<pi, pair_hash> setpi; inline bool is_ok(const setpi& a, int x, int y) { return a.count({x, y}) && ((!a.count({x - 1, y}) && !a.count({x + 1, y})) || (!a.count({x, y - 1}) && !a.count({x, y + 1}))); } int solve(setpi a) { queue<pi> q; for (auto [x, y] : a) { if (is_ok(a, x, y)) q.push({x, y}); } int res = 0; while (!q.empty()) { auto [x, y] = q.front(); q.pop(); if (!a.count({x, y})) continue; a.erase({x, y}); res++; if (is_ok(a, x - 1, y)) q.push({x - 1, y}); if (is_ok(a, x + 1, y)) q.push({x + 1, y}); if (is_ok(a, x, y - 1)) q.push({x, y - 1}); if (is_ok(a, x, y + 1)) q.push({x, y + 1}); } return res; } int main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); int n, m, k, q; cin >> n >> m >> k >> q; setpi a; for (int i = 0; i < k; i++) { int x, y; cin >> x >> y; a.insert({x, y}); } cout << solve(a) << '\n'; while (q--) { int x, y; cin >> x >> y; if (a.count({x, y})) a.erase({x, y}); else a.insert({x, y}); cout << solve(a) << '\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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pi; #define sz(x) (int)(x).size() struct pair_hash { inline std::size_t operator()(const std::pair<int, int>& v) const { return v.first * 1000000007LL + v.second; } }; typedef unordered_set<pi, pair_hash> setpi; inline bool is_ok(const setpi& a, int x, int y) { return a.count({x, y}) && ((!a.count({x - 1, y}) && !a.count({x + 1, y})) || (!a.count({x, y - 1}) && !a.count({x, y + 1}))); } int solve(setpi a) { queue<pi> q; for (auto [x, y] : a) { if (is_ok(a, x, y)) q.push({x, y}); } int res = 0; while (!q.empty()) { auto [x, y] = q.front(); q.pop(); if (!a.count({x, y})) continue; a.erase({x, y}); res++; if (is_ok(a, x - 1, y)) q.push({x - 1, y}); if (is_ok(a, x + 1, y)) q.push({x + 1, y}); if (is_ok(a, x, y - 1)) q.push({x, y - 1}); if (is_ok(a, x, y + 1)) q.push({x, y + 1}); } return res; } int main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); int n, m, k, q; cin >> n >> m >> k >> q; setpi a; for (int i = 0; i < k; i++) { int x, y; cin >> x >> y; a.insert({x, y}); } cout << solve(a) << '\n'; while (q--) { int x, y; cin >> x >> y; if (a.count({x, y})) a.erase({x, y}); else a.insert({x, y}); cout << solve(a) << '\n'; } } |