#include <bits/stdc++.h>
using namespace std;
#define ll long long
bool czy (int x, int y, map<pair<int, int>, bool> &mp) {
if ((!mp[{x-1, y}] && !mp[{x+1, y}]) || (!mp[{x, y-1}] && !mp[{x, y+1}])) {
return 1;
}
return 0;
}
void licz (map<pair<int, int>, bool> mp) {
queue<pair<int, int>> q;
int l = 0;
for (auto i : mp) {
if (i.second) {
if (czy(i.first.first, i.first.second, mp)) {
q.push({i.first.first, i.first.second});
}
}
}
while (!q.empty()) {
auto x = q.front();
q.pop();
if (!mp[{x.first, x.second}]) {
continue;
}
l++;
mp[{x.first, x.second}] = 0;
if (mp[{x.first-1, x.second}] && czy(x.first-1, x.second, mp)) {
q.push({x.first-1, x.second});
}
if (mp[{x.first+1, x.second}] && czy(x.first+1, x.second, mp)) {
q.push({x.first+1, x.second});
}
if (mp[{x.first, x.second-1}] && czy(x.first, x.second-1, mp)) {
q.push({x.first, x.second-1});
}
if (mp[{x.first, x.second+1}] && czy(x.first, x.second+1, mp)) {
q.push({x.first, x.second+1});
}
}
cout << l << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, k, q;
cin >> n >> m >> k >> q;
vector<pair<int, int>> w;
map<pair<int, int>, bool> mp;
for (int i = 0; i < k; i++) {
int x, y;
cin >> x >> y;
w.push_back({x, y});
mp[{x, y}] = 1;
}
licz(mp);
for (int i = 0; i < q; i++) {
int x, y;
cin >> x >> y;
mp[{x, y}] = !mp[{x, y}];
licz(mp);
}
}
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 | #include <bits/stdc++.h> using namespace std; #define ll long long bool czy (int x, int y, map<pair<int, int>, bool> &mp) { if ((!mp[{x-1, y}] && !mp[{x+1, y}]) || (!mp[{x, y-1}] && !mp[{x, y+1}])) { return 1; } return 0; } void licz (map<pair<int, int>, bool> mp) { queue<pair<int, int>> q; int l = 0; for (auto i : mp) { if (i.second) { if (czy(i.first.first, i.first.second, mp)) { q.push({i.first.first, i.first.second}); } } } while (!q.empty()) { auto x = q.front(); q.pop(); if (!mp[{x.first, x.second}]) { continue; } l++; mp[{x.first, x.second}] = 0; if (mp[{x.first-1, x.second}] && czy(x.first-1, x.second, mp)) { q.push({x.first-1, x.second}); } if (mp[{x.first+1, x.second}] && czy(x.first+1, x.second, mp)) { q.push({x.first+1, x.second}); } if (mp[{x.first, x.second-1}] && czy(x.first, x.second-1, mp)) { q.push({x.first, x.second-1}); } if (mp[{x.first, x.second+1}] && czy(x.first, x.second+1, mp)) { q.push({x.first, x.second+1}); } } cout << l << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m, k, q; cin >> n >> m >> k >> q; vector<pair<int, int>> w; map<pair<int, int>, bool> mp; for (int i = 0; i < k; i++) { int x, y; cin >> x >> y; w.push_back({x, y}); mp[{x, y}] = 1; } licz(mp); for (int i = 0; i < q; i++) { int x, y; cin >> x >> y; mp[{x, y}] = !mp[{x, y}]; licz(mp); } } |
English