#pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define pb push_back #define ff first #define ss second #define MOD 1000000009 #define INF 1000000019 #define INFL 1000000000000000099LL #define erase(x, y) x.erase(find(x.begin(),x.end(),y)) ll n,q,a,b; //int ile[150007][2]; vector<ll>sas[150007]; pair<ll,ll>co[150007]; bitset<150007> odw; map<pair<ll,ll>,ll>mp; stack<int>st; ll policz(ll k){ ll wyn=0; odw.reset(); odw[0]=1; for(int i=1;i<=n+k+1;i++) if(co[i].ff||co[i].ss) st.push(i); //return 0; //cout<<st.size()<<flush;//return 0; while(!st.empty()){ if(odw[st.top()]){ st.pop(); continue; } //cout<<odw[22]<<" "; //cout<<st.top()<<" "<<flush ; bool bl1=1,bl2=1; for(int i : sas[st.top()]){ if(co[i].ff==co[st.top()].ff && !odw[i]) bl1=0; else if(!odw[i]) bl2=0; } int pom=st.top(); st.pop(); if(bl1 || bl2){ //cout<<pom<<" "; wyn++; odw[pom]=1; for( ll i : sas[pom]) st.push(i); } } return wyn; } int main() { ios_base::sync_with_stdio(0);cin.tie(0); cin>>n>>n>>n>>q; for(ll i=0;i<n;i++){ cin>>a>>b; co[i+1]={a,b}; mp[{a,b}]=i+1; if(mp[{a+1,b}]){ sas[mp[{a+1,b}]].pb(i+1); sas[i+1].pb(mp[{a+1,b}]); } if(mp[{a-1,b}]){ sas[mp[{a-1,b}]].pb(i+1); sas[i+1].pb(mp[{a-1,b}]); } if(mp[{a,b+1}]){ sas[mp[{a,b+1}]].pb(i+1); sas[i+1].pb(mp[{a,b+1}]); } if(mp[{a,b-1}]){ sas[mp[{a,b-1}]].pb(i+1); sas[i+1].pb(mp[{a,b-1}]); } //for(ll j : sas[1])cout<<j<<" "; // cout<<"\n"; } cout<<policz(-1)<<"\n";//<<flush; for(ll i=0;i<q;i++){ cin>>a>>b; if(!mp[{a,b}]){ co[n+i+1]={a,b}; mp[{a,b}]=n+i+1; if(mp[{a+1,b}]){ sas[mp[{a+1,b}]].pb(i+n+1); sas[i+n+1].pb(mp[{a+1,b}]); } if(mp[{a-1,b}]){ sas[mp[{a-1,b}]].pb(i+n+1); sas[i+n+1].pb(mp[{a-1,b}]); } if(mp[{a,b+1}]){ sas[mp[{a,b+1}]].pb(i+n+1); sas[i+n+1].pb(mp[{a,b+1}]); } if(mp[{a,b-1}]){ sas[mp[{a,b-1}]].pb(i+n+1); sas[i+n+1].pb(mp[{a,b-1}]); } } else{ co[mp[{a,b}]]={0,0}; if(mp[{a+1,b}]){ erase(sas[(mp[{a+1,b}])],(mp[{a,b}])); erase(sas[(mp[{a,b}])],(mp[{a+1,b}])); } if(mp[{a-1,b}]){ erase(sas[(mp[{a-1,b}])],(mp[{a,b}])); erase(sas[(mp[{a,b}])],(mp[{a-1,b}])); } if(mp[{a,b+1}]){ erase(sas[(mp[{a,b+1}])],(mp[{a,b}])); erase(sas[(mp[{a,b}])],(mp[{a,b+1}])); } if(mp[{a,b-1}]){ erase(sas[(mp[{a,b-1}])],(mp[{a,b}])); erase(sas[(mp[{a,b}])],(mp[{a,b-1}])); } mp[{a,b}]=0; } //for(ll j : sas[1])cout<<j<<" "; cout<<policz(i)<<"\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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | #pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define pb push_back #define ff first #define ss second #define MOD 1000000009 #define INF 1000000019 #define INFL 1000000000000000099LL #define erase(x, y) x.erase(find(x.begin(),x.end(),y)) ll n,q,a,b; //int ile[150007][2]; vector<ll>sas[150007]; pair<ll,ll>co[150007]; bitset<150007> odw; map<pair<ll,ll>,ll>mp; stack<int>st; ll policz(ll k){ ll wyn=0; odw.reset(); odw[0]=1; for(int i=1;i<=n+k+1;i++) if(co[i].ff||co[i].ss) st.push(i); //return 0; //cout<<st.size()<<flush;//return 0; while(!st.empty()){ if(odw[st.top()]){ st.pop(); continue; } //cout<<odw[22]<<" "; //cout<<st.top()<<" "<<flush ; bool bl1=1,bl2=1; for(int i : sas[st.top()]){ if(co[i].ff==co[st.top()].ff && !odw[i]) bl1=0; else if(!odw[i]) bl2=0; } int pom=st.top(); st.pop(); if(bl1 || bl2){ //cout<<pom<<" "; wyn++; odw[pom]=1; for( ll i : sas[pom]) st.push(i); } } return wyn; } int main() { ios_base::sync_with_stdio(0);cin.tie(0); cin>>n>>n>>n>>q; for(ll i=0;i<n;i++){ cin>>a>>b; co[i+1]={a,b}; mp[{a,b}]=i+1; if(mp[{a+1,b}]){ sas[mp[{a+1,b}]].pb(i+1); sas[i+1].pb(mp[{a+1,b}]); } if(mp[{a-1,b}]){ sas[mp[{a-1,b}]].pb(i+1); sas[i+1].pb(mp[{a-1,b}]); } if(mp[{a,b+1}]){ sas[mp[{a,b+1}]].pb(i+1); sas[i+1].pb(mp[{a,b+1}]); } if(mp[{a,b-1}]){ sas[mp[{a,b-1}]].pb(i+1); sas[i+1].pb(mp[{a,b-1}]); } //for(ll j : sas[1])cout<<j<<" "; // cout<<"\n"; } cout<<policz(-1)<<"\n";//<<flush; for(ll i=0;i<q;i++){ cin>>a>>b; if(!mp[{a,b}]){ co[n+i+1]={a,b}; mp[{a,b}]=n+i+1; if(mp[{a+1,b}]){ sas[mp[{a+1,b}]].pb(i+n+1); sas[i+n+1].pb(mp[{a+1,b}]); } if(mp[{a-1,b}]){ sas[mp[{a-1,b}]].pb(i+n+1); sas[i+n+1].pb(mp[{a-1,b}]); } if(mp[{a,b+1}]){ sas[mp[{a,b+1}]].pb(i+n+1); sas[i+n+1].pb(mp[{a,b+1}]); } if(mp[{a,b-1}]){ sas[mp[{a,b-1}]].pb(i+n+1); sas[i+n+1].pb(mp[{a,b-1}]); } } else{ co[mp[{a,b}]]={0,0}; if(mp[{a+1,b}]){ erase(sas[(mp[{a+1,b}])],(mp[{a,b}])); erase(sas[(mp[{a,b}])],(mp[{a+1,b}])); } if(mp[{a-1,b}]){ erase(sas[(mp[{a-1,b}])],(mp[{a,b}])); erase(sas[(mp[{a,b}])],(mp[{a-1,b}])); } if(mp[{a,b+1}]){ erase(sas[(mp[{a,b+1}])],(mp[{a,b}])); erase(sas[(mp[{a,b}])],(mp[{a,b+1}])); } if(mp[{a,b-1}]){ erase(sas[(mp[{a,b-1}])],(mp[{a,b}])); erase(sas[(mp[{a,b}])],(mp[{a,b-1}])); } mp[{a,b}]=0; } //for(ll j : sas[1])cout<<j<<" "; cout<<policz(i)<<"\n"; } return 0; } |