#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m, k, q;
map<pair<int,int>, int> mapa;
map<pair<int,int>, int> odw;
set<pair<int,int>> punkty;
vector<pair<int,int>> roza = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
bool check(pair<int,int> u){
if(u.first <= 0 or u.first > n or u.second <= 0 or u.second > m) return false;
int l = mapa[{u.first, u.second - 1}];
int lodw = odw[{u.first, u.second - 1}];
int g = mapa[{u.first - 1, u.second}];
int godw = odw[{u.first - 1, u.second}];
int p = mapa[{u.first, u.second + 1}];
int podw = odw[{u.first, u.second + 1}];
int d = mapa[{u.first + 1, u.second}];
int dodw = odw[{u.first + 1, u.second}];
if(((l == 0 or lodw == 1) and (p == 0 or podw == 1)) or ((g == 0 or godw == 1) and (d == 0 or dodw == 1))) return true;
return false;
}
void rozwiaz(){
queue<pair<int,int>> q;
ll w = 0;
for(auto u : punkty){
if(check(u)) {
q.push(u);
//cout << u.first << " " << u.second << " u\n";
}
}
while(!q.empty()){
auto e = q.front();
q.pop();
if(odw[e] == 1) continue;
odw[e] = 1;
w++;
for(auto u : roza){
pair<int,int> tmp = {e.first + u.first, e.second + u.second};
if(odw[tmp] == 0 and mapa[tmp] == 1 and check(tmp)) q.push(tmp);
}
}
odw.clear();
cout << w << "\n";
return;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> k >> q;
int a, b;
for(int i = 0; i < k; ++i){
cin >> a >> b;
mapa[{a,b}] = 1;
punkty.insert({a,b});
}
rozwiaz();
for(int i = 0; i < q; ++i){
cin >> a >> b;
mapa[{a,b}] = mapa[{a,b}] ^ 1;
if(mapa[{a,b}] == 1){
punkty.insert({a,b});
}
else{
punkty.erase({a,b});
}
rozwiaz();
}
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; int n, m, k, q; map<pair<int,int>, int> mapa; map<pair<int,int>, int> odw; set<pair<int,int>> punkty; vector<pair<int,int>> roza = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; bool check(pair<int,int> u){ if(u.first <= 0 or u.first > n or u.second <= 0 or u.second > m) return false; int l = mapa[{u.first, u.second - 1}]; int lodw = odw[{u.first, u.second - 1}]; int g = mapa[{u.first - 1, u.second}]; int godw = odw[{u.first - 1, u.second}]; int p = mapa[{u.first, u.second + 1}]; int podw = odw[{u.first, u.second + 1}]; int d = mapa[{u.first + 1, u.second}]; int dodw = odw[{u.first + 1, u.second}]; if(((l == 0 or lodw == 1) and (p == 0 or podw == 1)) or ((g == 0 or godw == 1) and (d == 0 or dodw == 1))) return true; return false; } void rozwiaz(){ queue<pair<int,int>> q; ll w = 0; for(auto u : punkty){ if(check(u)) { q.push(u); //cout << u.first << " " << u.second << " u\n"; } } while(!q.empty()){ auto e = q.front(); q.pop(); if(odw[e] == 1) continue; odw[e] = 1; w++; for(auto u : roza){ pair<int,int> tmp = {e.first + u.first, e.second + u.second}; if(odw[tmp] == 0 and mapa[tmp] == 1 and check(tmp)) q.push(tmp); } } odw.clear(); cout << w << "\n"; return; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> k >> q; int a, b; for(int i = 0; i < k; ++i){ cin >> a >> b; mapa[{a,b}] = 1; punkty.insert({a,b}); } rozwiaz(); for(int i = 0; i < q; ++i){ cin >> a >> b; mapa[{a,b}] = mapa[{a,b}] ^ 1; if(mapa[{a,b}] == 1){ punkty.insert({a,b}); } else{ punkty.erase({a,b}); } rozwiaz(); } return 0; } |
English