#include <iostream>
#include <set>
#include <queue>
#include <map>
#include <array>
using namespace std;
int calc(set<long long>& points, int n) {
queue<long long> q;
set<long long> visited;
map<long long, array<bool,4>> touched;
for (long long p : points) {
bool is_left = p%n==0 || points.find(p-1)==points.end();
bool is_right = (p+1)%n==0 || points.find(p+1)==points.end();
bool is_top = points.find(p-n)==points.end();
bool is_bot = points.find(p+n)==points.end();
touched.insert({p,{is_left,is_right,is_top,is_bot}});
if (is_left && is_right) {
visited.insert(p);
q.push(p);
} else if (is_top && is_bot) {
visited.insert(p);
q.push(p);
}
}
while (!q.empty()) {
long long p = q.front();
q.pop();
if (p%n!=0 && points.find(p-1)!=points.end() && visited.find(p-1)==visited.end()) {
touched[p-1][1] = 1;
if (touched[p-1][0] == 1) {
visited.insert(p-1);
q.push(p-1);
}
}
if ((p+1)%n!=0 && points.find(p+1)!=points.end() && visited.find(p+1)==visited.end()) {
touched[p+1][0] = 1;
if (touched[p+1][1] == 1) {
visited.insert(p+1);
q.push(p+1);
}
}
if (points.find(p-n)!=points.end() && visited.find(p-n)==visited.end()) {
touched[p-n][3] = 1;
if (touched[p-n][2] == 1) {
visited.insert(p-n);
q.push(p-n);
}
}
if (points.find(p+n)!=points.end() && visited.find(p+n)==visited.end()) {
touched[p+n][2] = 1;
if (touched[p+n][3] == 1) {
visited.insert(p+n);
q.push(p+n);
}
}
}
return visited.size();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,m,k,q;
cin>>n>>m>>k>>q;
set<long long> points;
for (int i=0; i<k; i++) {
int x,y;
cin>>x>>y;
points.insert(x-1+(y-1)*n);
}
cout<<calc(points,n)<<'\n';
for (int i=0; i<q; i++) {
int x,y;
cin>>x>>y;
x--;y--;
if (points.find(x+y*n) == points.end()) points.insert(x+y*n);
else points.erase(x+y*n);
cout<<calc(points,n)<<'\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 | #include <iostream> #include <set> #include <queue> #include <map> #include <array> using namespace std; int calc(set<long long>& points, int n) { queue<long long> q; set<long long> visited; map<long long, array<bool,4>> touched; for (long long p : points) { bool is_left = p%n==0 || points.find(p-1)==points.end(); bool is_right = (p+1)%n==0 || points.find(p+1)==points.end(); bool is_top = points.find(p-n)==points.end(); bool is_bot = points.find(p+n)==points.end(); touched.insert({p,{is_left,is_right,is_top,is_bot}}); if (is_left && is_right) { visited.insert(p); q.push(p); } else if (is_top && is_bot) { visited.insert(p); q.push(p); } } while (!q.empty()) { long long p = q.front(); q.pop(); if (p%n!=0 && points.find(p-1)!=points.end() && visited.find(p-1)==visited.end()) { touched[p-1][1] = 1; if (touched[p-1][0] == 1) { visited.insert(p-1); q.push(p-1); } } if ((p+1)%n!=0 && points.find(p+1)!=points.end() && visited.find(p+1)==visited.end()) { touched[p+1][0] = 1; if (touched[p+1][1] == 1) { visited.insert(p+1); q.push(p+1); } } if (points.find(p-n)!=points.end() && visited.find(p-n)==visited.end()) { touched[p-n][3] = 1; if (touched[p-n][2] == 1) { visited.insert(p-n); q.push(p-n); } } if (points.find(p+n)!=points.end() && visited.find(p+n)==visited.end()) { touched[p+n][2] = 1; if (touched[p+n][3] == 1) { visited.insert(p+n); q.push(p+n); } } } return visited.size(); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n,m,k,q; cin>>n>>m>>k>>q; set<long long> points; for (int i=0; i<k; i++) { int x,y; cin>>x>>y; points.insert(x-1+(y-1)*n); } cout<<calc(points,n)<<'\n'; for (int i=0; i<q; i++) { int x,y; cin>>x>>y; x--;y--; if (points.find(x+y*n) == points.end()) points.insert(x+y*n); else points.erase(x+y*n); cout<<calc(points,n)<<'\n'; } return 0; } |
English