#include <bits/stdc++.h>
using namespace std;
map<pair<int,int>,pair<int,int>>mp;// sec 1-da sie
vector<pair<int,int>>v,v1;
int res=0;
void dfs(pair<int,int>p)
{
if(mp[p].second==1||mp[p].first==0)
return;
//cout<<p.first<<" "<<p.second<<"\n";
auto x10=mp[{p.first-1,p.second}];
auto x20=mp[{p.first+1,p.second}];
auto x01=mp[{p.first,p.second-1}];
auto x02=mp[{p.first,p.second+1}];
if(((x10.first==0||x10.second==1)&&(x20.first==0||x20.second==1))||((x01.first==0||x01.second==1)&&(x02.first==0||x02.second==1)))
{
//cout<<"----->"<<mp[{2,1}].first<<" "<<mp[{2,1}].second<<"\n";
res++;
mp[p].second=1;
//cout<<"????\n";
if(x10.first==1&&x10.second==0)
dfs({p.first-1,p.second});
if(x20.first==1&&x20.second==0)
dfs({p.first+1,p.second});
if(x01.first==1&&x01.second==0)
dfs({p.first,p.second-1});
if(x02.first==1&&x02.second==0)
dfs({p.first,p.second+1});
}
}
int main()
{
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
cout.tie(NULL);
int n,m,k,q;
cin>>n>>m>>k>>q;
for(int i=0 ;i<k; i++)
{
int x,y;
cin>>x>>y;
v.push_back({x,y});
mp[{x,y}]={1,0};
}
for(int i=0; i<k; i++)
{
//cout<<"dupa\n";
if(mp[v[i]].second==0)
dfs(v[i]);
}
cout<<res<<"\n";
for(int j=0; j<q; j++)
{
int x,y;
cin>>x>>y;
res=0;
if(mp[{x,y}].first==1)
mp[{x,y}]={0,0};
else
{
mp[{x,y}]={1,0};
v.push_back({x,y});
}
v1.clear();
for(int i=0; i<v.size(); i++)
{
mp[v[i]].second=0;
if(mp[v[i]].first!=0)
v1.push_back(v[i]);
}
v=v1;
for(int i=0; i<v.size(); i++)
{
//cout<<"dupa\n";
if(mp[v[i]].second==0)
dfs(v[i]);
}
cout<<res<<"\n";
}
}
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 | #include <bits/stdc++.h> using namespace std; map<pair<int,int>,pair<int,int>>mp;// sec 1-da sie vector<pair<int,int>>v,v1; int res=0; void dfs(pair<int,int>p) { if(mp[p].second==1||mp[p].first==0) return; //cout<<p.first<<" "<<p.second<<"\n"; auto x10=mp[{p.first-1,p.second}]; auto x20=mp[{p.first+1,p.second}]; auto x01=mp[{p.first,p.second-1}]; auto x02=mp[{p.first,p.second+1}]; if(((x10.first==0||x10.second==1)&&(x20.first==0||x20.second==1))||((x01.first==0||x01.second==1)&&(x02.first==0||x02.second==1))) { //cout<<"----->"<<mp[{2,1}].first<<" "<<mp[{2,1}].second<<"\n"; res++; mp[p].second=1; //cout<<"????\n"; if(x10.first==1&&x10.second==0) dfs({p.first-1,p.second}); if(x20.first==1&&x20.second==0) dfs({p.first+1,p.second}); if(x01.first==1&&x01.second==0) dfs({p.first,p.second-1}); if(x02.first==1&&x02.second==0) dfs({p.first,p.second+1}); } } int main() { ios_base::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL); int n,m,k,q; cin>>n>>m>>k>>q; for(int i=0 ;i<k; i++) { int x,y; cin>>x>>y; v.push_back({x,y}); mp[{x,y}]={1,0}; } for(int i=0; i<k; i++) { //cout<<"dupa\n"; if(mp[v[i]].second==0) dfs(v[i]); } cout<<res<<"\n"; for(int j=0; j<q; j++) { int x,y; cin>>x>>y; res=0; if(mp[{x,y}].first==1) mp[{x,y}]={0,0}; else { mp[{x,y}]={1,0}; v.push_back({x,y}); } v1.clear(); for(int i=0; i<v.size(); i++) { mp[v[i]].second=0; if(mp[v[i]].first!=0) v1.push_back(v[i]); } v=v1; for(int i=0; i<v.size(); i++) { //cout<<"dupa\n"; if(mp[v[i]].second==0) dfs(v[i]); } cout<<res<<"\n"; } } |
English