#include <iostream>
#include <vector>
#include <map>
#include <set>
using namespace std;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,m,k,q;
cin>>n>>m>>k>>q;
set<pair<int,int>>secik;
map<pair<int,int>,bool>mapa;
for(int i=0; k>i; i++){
int a,b; cin>>a>>b;
secik.insert({a,b});
mapa[{a,b}] = true;
}
set<pair<int,int>>kol = secik;
map<pair<int,int>,bool>mapka = mapa;
int res = 0;
while(true){
bool czy_koniec = true;
vector<pair<int,int>>usun;
usun.clear();
for(auto x : kol){
int wsp_x = x.first;
int wsp_y = x.second;
if(mapka[{wsp_x-1,wsp_y}] == false and mapka[{wsp_x+1,wsp_y}] == false){
res++;
czy_koniec = false;
mapka[{wsp_x,wsp_y}] = false;
usun.push_back({wsp_x,wsp_y});
}else if(mapka[{wsp_x,wsp_y+1}] == false and mapka[{wsp_x,wsp_y-1}] == false){
res++;
czy_koniec = false;
mapka[{wsp_x,wsp_y}] = false;
usun.push_back({wsp_x,wsp_y});
}
}
for(auto x : usun){
kol.erase({x});
}
if(czy_koniec == true){
break;
}
}
cout<<res<<endl;
for(int i=0; q>i; i++){
int a,b; cin>>a>>b;
if(mapa[{a,b}] == true){
secik.erase({a,b});
k--;
mapa[{a,b}] = false;
}else{
secik.insert({a,b});
k++;
mapa[{a,b}] = true;
}
res = 0;
kol = secik;
mapka = mapa;
while(true){
bool czy_koniec = true;
vector<pair<int,int>>usun;
usun.clear();
for(auto x : kol){
int wsp_x = x.first;
int wsp_y = x.second;
if(mapka[{wsp_x-1,wsp_y}] == false and mapka[{wsp_x+1,wsp_y}] == false){
res++;
czy_koniec = false;
mapka[{wsp_x,wsp_y}] = false;
usun.push_back({wsp_x,wsp_y});
}else if(mapka[{wsp_x,wsp_y+1}] == false and mapka[{wsp_x,wsp_y-1}] == false){
res++;
czy_koniec = false;
mapka[{wsp_x,wsp_y}] = false;
usun.push_back({wsp_x,wsp_y});
}
}
for(auto x : usun){
kol.erase({x});
}
if(czy_koniec == true){
break;
}
}
cout<<res<<endl;
}
}
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 <vector> #include <map> #include <set> using namespace std; signed main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n,m,k,q; cin>>n>>m>>k>>q; set<pair<int,int>>secik; map<pair<int,int>,bool>mapa; for(int i=0; k>i; i++){ int a,b; cin>>a>>b; secik.insert({a,b}); mapa[{a,b}] = true; } set<pair<int,int>>kol = secik; map<pair<int,int>,bool>mapka = mapa; int res = 0; while(true){ bool czy_koniec = true; vector<pair<int,int>>usun; usun.clear(); for(auto x : kol){ int wsp_x = x.first; int wsp_y = x.second; if(mapka[{wsp_x-1,wsp_y}] == false and mapka[{wsp_x+1,wsp_y}] == false){ res++; czy_koniec = false; mapka[{wsp_x,wsp_y}] = false; usun.push_back({wsp_x,wsp_y}); }else if(mapka[{wsp_x,wsp_y+1}] == false and mapka[{wsp_x,wsp_y-1}] == false){ res++; czy_koniec = false; mapka[{wsp_x,wsp_y}] = false; usun.push_back({wsp_x,wsp_y}); } } for(auto x : usun){ kol.erase({x}); } if(czy_koniec == true){ break; } } cout<<res<<endl; for(int i=0; q>i; i++){ int a,b; cin>>a>>b; if(mapa[{a,b}] == true){ secik.erase({a,b}); k--; mapa[{a,b}] = false; }else{ secik.insert({a,b}); k++; mapa[{a,b}] = true; } res = 0; kol = secik; mapka = mapa; while(true){ bool czy_koniec = true; vector<pair<int,int>>usun; usun.clear(); for(auto x : kol){ int wsp_x = x.first; int wsp_y = x.second; if(mapka[{wsp_x-1,wsp_y}] == false and mapka[{wsp_x+1,wsp_y}] == false){ res++; czy_koniec = false; mapka[{wsp_x,wsp_y}] = false; usun.push_back({wsp_x,wsp_y}); }else if(mapka[{wsp_x,wsp_y+1}] == false and mapka[{wsp_x,wsp_y-1}] == false){ res++; czy_koniec = false; mapka[{wsp_x,wsp_y}] = false; usun.push_back({wsp_x,wsp_y}); } } for(auto x : usun){ kol.erase({x}); } if(czy_koniec == true){ break; } } cout<<res<<endl; } } |
English