#include <bits/stdc++.h>
#define pb push_back
#define f first
#define s second
#define ll long long
#define dbg(x) cerr << #x << ": " << x << "\n"
#define mp make_pair
using namespace std;
const int N = 500005;
map<pair<int, int>, bool> M;
map<pair<int, int>, bool> vis;
vector<pair<int, int>> neigh = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
bool check(int x, int y)
{
if (!M[{x, y}] || vis[{x, y}])
return false;
bool is_left = (M[{x - 1, y}] && vis[{x - 1, y}] == false);
bool is_right = (M[{x + 1, y}] && vis[{x + 1, y}] == false);
bool is_down = (M[{x, y - 1}] && vis[{x, y - 1}] == false);
bool is_up = (M[{x, y + 1}] && vis[{x, y + 1}] == false);
return ((!is_left && !is_right) || (!is_down && !is_up));
}
int main()
{
cin.tie(0)->sync_with_stdio(0);
int n, m, k, q;
cin >> n >> m >> k >> q;
set<pair<int, int>> blocks;
for (int i = 1; i <= k; i++)
{
int x, y;
cin >> x >> y;
blocks.insert({x, y});
M[{x, y}] = true;
}
queue<pair<int, int>> Q;
for (int i = 1; i <= q + 1; i++)
{
for (auto it = blocks.begin(); it != blocks.end(); it++)
{
int x = (*it).f;
int y = (*it).s;
vis[{x, y}] = false;
if ((!M[{x - 1, y}] && !M[{x + 1, y}]) || (!M[{x, y - 1}] && !M[{x, y + 1}]))
{
vis[{x, y}] = true;
Q.push({x, y});
}
}
int res = 0;
while (!Q.empty())
{
res++;
auto [x, y] = Q.front();
Q.pop();
for (auto [x1, y1] : neigh)
{
if (check(x + x1, y + y1))
{
vis[{x + x1, y + y1}] = true;
Q.push({x + x1, y + y1});
}
}
}
cout << res << "\n";
if (i == q + 1)
continue;
int x, y;
cin >> x >> y;
if (M[{x, y}])
blocks.erase({x, y});
else
blocks.insert({x, y});
M[{x, y}] = !M[{x, y}];
}
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 | #include <bits/stdc++.h> #define pb push_back #define f first #define s second #define ll long long #define dbg(x) cerr << #x << ": " << x << "\n" #define mp make_pair using namespace std; const int N = 500005; map<pair<int, int>, bool> M; map<pair<int, int>, bool> vis; vector<pair<int, int>> neigh = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; bool check(int x, int y) { if (!M[{x, y}] || vis[{x, y}]) return false; bool is_left = (M[{x - 1, y}] && vis[{x - 1, y}] == false); bool is_right = (M[{x + 1, y}] && vis[{x + 1, y}] == false); bool is_down = (M[{x, y - 1}] && vis[{x, y - 1}] == false); bool is_up = (M[{x, y + 1}] && vis[{x, y + 1}] == false); return ((!is_left && !is_right) || (!is_down && !is_up)); } int main() { cin.tie(0)->sync_with_stdio(0); int n, m, k, q; cin >> n >> m >> k >> q; set<pair<int, int>> blocks; for (int i = 1; i <= k; i++) { int x, y; cin >> x >> y; blocks.insert({x, y}); M[{x, y}] = true; } queue<pair<int, int>> Q; for (int i = 1; i <= q + 1; i++) { for (auto it = blocks.begin(); it != blocks.end(); it++) { int x = (*it).f; int y = (*it).s; vis[{x, y}] = false; if ((!M[{x - 1, y}] && !M[{x + 1, y}]) || (!M[{x, y - 1}] && !M[{x, y + 1}])) { vis[{x, y}] = true; Q.push({x, y}); } } int res = 0; while (!Q.empty()) { res++; auto [x, y] = Q.front(); Q.pop(); for (auto [x1, y1] : neigh) { if (check(x + x1, y + y1)) { vis[{x + x1, y + y1}] = true; Q.push({x + x1, y + y1}); } } } cout << res << "\n"; if (i == q + 1) continue; int x, y; cin >> x >> y; if (M[{x, y}]) blocks.erase({x, y}); else blocks.insert({x, y}); M[{x, y}] = !M[{x, y}]; } return 0; } |
English