#include <bits/stdc++.h> typedef long long ll; using namespace std; vector<vector<bool>> pole, zawiera; int ans = 0; bool spr(int x, int y) { return pole[x][y] && pole[x + 1][y] && pole[x][y + 1] && pole[x + 1][y + 1]; } void uaktualnij(int& x, int& y, bool typ) // 0 - odejmij, 1 - dodaj { bool wyznacznik = spr(x, y) || spr(x - 1, y - 1) || spr(x - 1, y) || spr(x, y - 1); if (typ && !zawiera[x][y] && wyznacznik) { zawiera[x][y] = true; --ans; } if (!typ && zawiera[x][y] && !wyznacznik) { zawiera[x][y] = false; ++ans; } } void modyfikuj(int& x, int & y, bool typ) { for (int i = x - 1; i <= x + 1; ++i) for (int j = y - 1; j <= y + 1; ++j) if (pole[i][j])// && (x != i || y != j)) uaktualnij(i, j, typ); } void dodaj(int& x, int& y) { ++ans; pole[x][y] = true; zawiera[x][y] = false; modyfikuj(x, y, true); } void usun(int& x, int& y) { --ans; pole[x][y] = false; zawiera[x][y] = false; modyfikuj(x, y, false); } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); int n, m, k, q, x, y; cin >> n >> m >> k >> q; pole.resize(n + 2, vector<bool>(m + 2)); zawiera.resize(n + 2, vector<bool>(m + 2)); for (int i = 0; i < k; ++i) { cin >> x >> y; pole[x][y] = true; zawiera[x][y] = false; } ans = k; for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) uaktualnij(i, j, true); cout << ans << '\n'; for (int i = 0; i < q; ++i) { cin >> x >> y; if (pole[x][y]) usun(x, y); else dodaj(x, y); cout << ans << '\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 | #include <bits/stdc++.h> typedef long long ll; using namespace std; vector<vector<bool>> pole, zawiera; int ans = 0; bool spr(int x, int y) { return pole[x][y] && pole[x + 1][y] && pole[x][y + 1] && pole[x + 1][y + 1]; } void uaktualnij(int& x, int& y, bool typ) // 0 - odejmij, 1 - dodaj { bool wyznacznik = spr(x, y) || spr(x - 1, y - 1) || spr(x - 1, y) || spr(x, y - 1); if (typ && !zawiera[x][y] && wyznacznik) { zawiera[x][y] = true; --ans; } if (!typ && zawiera[x][y] && !wyznacznik) { zawiera[x][y] = false; ++ans; } } void modyfikuj(int& x, int & y, bool typ) { for (int i = x - 1; i <= x + 1; ++i) for (int j = y - 1; j <= y + 1; ++j) if (pole[i][j])// && (x != i || y != j)) uaktualnij(i, j, typ); } void dodaj(int& x, int& y) { ++ans; pole[x][y] = true; zawiera[x][y] = false; modyfikuj(x, y, true); } void usun(int& x, int& y) { --ans; pole[x][y] = false; zawiera[x][y] = false; modyfikuj(x, y, false); } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); int n, m, k, q, x, y; cin >> n >> m >> k >> q; pole.resize(n + 2, vector<bool>(m + 2)); zawiera.resize(n + 2, vector<bool>(m + 2)); for (int i = 0; i < k; ++i) { cin >> x >> y; pole[x][y] = true; zawiera[x][y] = false; } ans = k; for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) uaktualnij(i, j, true); cout << ans << '\n'; for (int i = 0; i < q; ++i) { cin >> x >> y; if (pole[x][y]) usun(x, y); else dodaj(x, y); cout << ans << '\n'; } return 0; } |