#include<bits/stdc++.h> using namespace std; #define fi first #define se second bool debug=false; const int Q=2e5+5; int n,m,k,q,z; map<pair<int,int>,int>mp; int a[Q],b[Q]; int x[Q],y[Q]; int on[Q]; int col[Q]; int dir[Q][4]; int dx[4]={-1,0,1,0}; int dy[4]={0,-1,0,1}; int sq[Q];//number of 2x2 squares formed around vector<pair<int,int> >mem[Q];//each guy is membership of at most 4 segments int rt[4*Q],len[4*Q]; typedef array<int,4> arin;//0 = bonus, 1 = prefix 11112, 2 = suffix 211111 ,3 = len if all 1 or 0 o/w arin wjsn[3]={{0,0,0,0},{0,0,0,1},{0,1,1,0}}; arin merge(arin &x,arin &y){ arin z={0,0,0,0}; if(x[3] && y[3]){ z[3]=x[3]+y[3]; return z; } z[0]=x[0]+y[0]; if(x[2]>0 && y[1]>0) z[0]+=x[2]+y[1]-2; if(x[3]){ if(y[1]>0) z[1]=x[3]+y[1]; else z[1]=0; } else z[1]=x[1]; if(y[3]){ if(x[2]>0) z[2]=x[2]+y[3]; else z[2]=0; } else z[2]=y[2]; return z; } const int ts=5e6+1; int lc[ts],rc[ts]; arin info[ts]; int sz; int build(int l,int r){ int id=++sz; if(l==r) return id; int mid=(l+r)/2; lc[id]=build(l,mid); rc[id]=build(mid+1,r); return id; } void upd(int id,int l,int r,int p,int v){ if(l==r){ info[id]=wjsn[v]; return; } int mid=(l+r)/2; if(p<=mid) upd(lc[id],l,mid,p,v); else upd(rc[id],mid+1,r,p,v); info[id]=merge(info[lc[id]],info[rc[id]]); } void check_sq(vector<int>v,int w){ for(auto c:v){ if(!on[c]) return; } /*cout << "SQuare found: "; for(int i=0; i<4 ;i++) cout << v[i] << ' '; cout << endl;*/ for(auto c:v){ sq[c]+=w; } } int tot_c2=0,tot_on=0; int ans=0; int ask(){ //cout << "ASK: " << tot_on << ' ' << tot_c2 << ' ' << ans << endl; return tot_on-(tot_c2+ans); } void upd_col(int i,int nc){ //cout << "upd_col " << i << ' ' << nc << endl; for(auto c:mem[i]){ ans-=info[rt[c.fi]][0]; upd(rt[c.fi],1,len[c.fi],c.se,nc); ans+=info[rt[c.fi]][0]; //cout << "HI " << c.fi << ' ' << info[rt[c.fi]][0] << endl; } if(col[i]==2) tot_c2--; if(col[i]!=0) tot_on--; col[i]=nc; if(col[i]==2) tot_c2++; if(col[i]!=0) tot_on++; } int g[3][3]; void tog(int i){ //cout << "tog " << a[i] << ' ' << b[i] << endl; g[1][1]=i; g[0][1]=dir[i][0]; g[0][0]=dir[dir[i][0]][1]; g[1][0]=dir[i][1]; g[2][0]=dir[dir[i][1]][2]; g[2][1]=dir[i][2]; g[2][2]=dir[dir[i][2]][3]; g[1][2]=dir[i][3]; g[0][2]=dir[dir[i][3]][0]; for(int i=0; i<2 ;i++){ for(int j=0; j<2 ;j++){ vector<int>v; for(int k=0; k<2 ;k++){ for(int l=0; l<2 ;l++){ v.push_back(g[i+k][j+l]); } } check_sq(v,-1); } } on[i]^=1; for(int i=0; i<2 ;i++){ for(int j=0; j<2 ;j++){ vector<int>v; for(int k=0; k<2 ;k++){ for(int l=0; l<2 ;l++){ v.push_back(g[i+k][j+l]); } } check_sq(v,1); } } for(int i=0; i<3 ;i++){ for(int j=0; j<3 ;j++){ if(g[i][j]==0) continue; int new_col=0; if(on[g[i][j]]) new_col=1+(sq[g[i][j]]>0); if(new_col!=col[g[i][j]]) upd_col(g[i][j],new_col); } } } bool vis[Q]; pair<int,int>val[Q]; int nxt(int id,int mode){ if((a[id]+b[id])%2) mode^=1; int step; if(mode==0) step=3; if(mode==1) step=2; if(mode==2) step=1; if(mode==3) step=2; return dir[id][step]; } vector<int>segs[4*Q];//for debug void build_segs(){ int seg_cnt=0; for(int mode=0; mode<4 ;mode++){ for(int i=1; i<=z ;i++) vis[i]=false; if(mode==0){ for(int i=1; i<=z ;i++){ val[i]={a[i]+b[i],i}; } sort(val+1,val+z+1); } if(mode==2){ for(int i=1; i<=z ;i++){ val[i]={a[i]-b[i],i}; } sort(val+1,val+z+1); } for(int j=1; j<=z ;j++){ int i=val[j].se; if(vis[i]) continue; int cur=++seg_cnt; int frog=i; for(int j=1; frog ;j++){ len[cur]=j; mem[frog].push_back({cur,j}); vis[frog]=true; //segs[cur].push_back(frog); frog=nxt(frog,mode); } rt[cur]=build(1,len[cur]); } }/* for(int i=1; i<=seg_cnt ;i++){ cout << "seg " << i << ": "; for(auto c:segs[i]){ cout << c << ' '; } cout << endl; }*/ } int main(){ ios::sync_with_stdio(false);cin.tie(0); cin >> n >> m >> k >> q; for(int i=1; i<=k+q ;i++){ cin >> x[i] >> y[i]; if(mp[{x[i],y[i]}]==0){ mp[{x[i],y[i]}]=++z; a[z]=x[i]; b[z]=y[i]; } } for(int i=1; i<=z ;i++){ for(int j=0; j<4 ;j++){ int nx=a[i]+dx[j]; int ny=b[i]+dy[j]; if(mp[{nx,ny}]==0) mp.erase({nx,ny}); else dir[i][j]=mp[{nx,ny}]; } } /*for(int i=1; i<=z ;i++){ cout << i << ' ' << a[i] << ' ' << b[i] << endl; cout << "DIR: "; for(int j=0; j<4 ;j++) cout << dir[i][j] << ' '; cout << endl; }*/ build_segs(); for(int i=1; i<=k ;i++) tog(mp[{x[i],y[i]}]); cout << ask() << '\n'; for(int i=k+1; i<=k+q ;i++){ tog(mp[{x[i],y[i]}]); cout << ask() << '\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 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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | #include<bits/stdc++.h> using namespace std; #define fi first #define se second bool debug=false; const int Q=2e5+5; int n,m,k,q,z; map<pair<int,int>,int>mp; int a[Q],b[Q]; int x[Q],y[Q]; int on[Q]; int col[Q]; int dir[Q][4]; int dx[4]={-1,0,1,0}; int dy[4]={0,-1,0,1}; int sq[Q];//number of 2x2 squares formed around vector<pair<int,int> >mem[Q];//each guy is membership of at most 4 segments int rt[4*Q],len[4*Q]; typedef array<int,4> arin;//0 = bonus, 1 = prefix 11112, 2 = suffix 211111 ,3 = len if all 1 or 0 o/w arin wjsn[3]={{0,0,0,0},{0,0,0,1},{0,1,1,0}}; arin merge(arin &x,arin &y){ arin z={0,0,0,0}; if(x[3] && y[3]){ z[3]=x[3]+y[3]; return z; } z[0]=x[0]+y[0]; if(x[2]>0 && y[1]>0) z[0]+=x[2]+y[1]-2; if(x[3]){ if(y[1]>0) z[1]=x[3]+y[1]; else z[1]=0; } else z[1]=x[1]; if(y[3]){ if(x[2]>0) z[2]=x[2]+y[3]; else z[2]=0; } else z[2]=y[2]; return z; } const int ts=5e6+1; int lc[ts],rc[ts]; arin info[ts]; int sz; int build(int l,int r){ int id=++sz; if(l==r) return id; int mid=(l+r)/2; lc[id]=build(l,mid); rc[id]=build(mid+1,r); return id; } void upd(int id,int l,int r,int p,int v){ if(l==r){ info[id]=wjsn[v]; return; } int mid=(l+r)/2; if(p<=mid) upd(lc[id],l,mid,p,v); else upd(rc[id],mid+1,r,p,v); info[id]=merge(info[lc[id]],info[rc[id]]); } void check_sq(vector<int>v,int w){ for(auto c:v){ if(!on[c]) return; } /*cout << "SQuare found: "; for(int i=0; i<4 ;i++) cout << v[i] << ' '; cout << endl;*/ for(auto c:v){ sq[c]+=w; } } int tot_c2=0,tot_on=0; int ans=0; int ask(){ //cout << "ASK: " << tot_on << ' ' << tot_c2 << ' ' << ans << endl; return tot_on-(tot_c2+ans); } void upd_col(int i,int nc){ //cout << "upd_col " << i << ' ' << nc << endl; for(auto c:mem[i]){ ans-=info[rt[c.fi]][0]; upd(rt[c.fi],1,len[c.fi],c.se,nc); ans+=info[rt[c.fi]][0]; //cout << "HI " << c.fi << ' ' << info[rt[c.fi]][0] << endl; } if(col[i]==2) tot_c2--; if(col[i]!=0) tot_on--; col[i]=nc; if(col[i]==2) tot_c2++; if(col[i]!=0) tot_on++; } int g[3][3]; void tog(int i){ //cout << "tog " << a[i] << ' ' << b[i] << endl; g[1][1]=i; g[0][1]=dir[i][0]; g[0][0]=dir[dir[i][0]][1]; g[1][0]=dir[i][1]; g[2][0]=dir[dir[i][1]][2]; g[2][1]=dir[i][2]; g[2][2]=dir[dir[i][2]][3]; g[1][2]=dir[i][3]; g[0][2]=dir[dir[i][3]][0]; for(int i=0; i<2 ;i++){ for(int j=0; j<2 ;j++){ vector<int>v; for(int k=0; k<2 ;k++){ for(int l=0; l<2 ;l++){ v.push_back(g[i+k][j+l]); } } check_sq(v,-1); } } on[i]^=1; for(int i=0; i<2 ;i++){ for(int j=0; j<2 ;j++){ vector<int>v; for(int k=0; k<2 ;k++){ for(int l=0; l<2 ;l++){ v.push_back(g[i+k][j+l]); } } check_sq(v,1); } } for(int i=0; i<3 ;i++){ for(int j=0; j<3 ;j++){ if(g[i][j]==0) continue; int new_col=0; if(on[g[i][j]]) new_col=1+(sq[g[i][j]]>0); if(new_col!=col[g[i][j]]) upd_col(g[i][j],new_col); } } } bool vis[Q]; pair<int,int>val[Q]; int nxt(int id,int mode){ if((a[id]+b[id])%2) mode^=1; int step; if(mode==0) step=3; if(mode==1) step=2; if(mode==2) step=1; if(mode==3) step=2; return dir[id][step]; } vector<int>segs[4*Q];//for debug void build_segs(){ int seg_cnt=0; for(int mode=0; mode<4 ;mode++){ for(int i=1; i<=z ;i++) vis[i]=false; if(mode==0){ for(int i=1; i<=z ;i++){ val[i]={a[i]+b[i],i}; } sort(val+1,val+z+1); } if(mode==2){ for(int i=1; i<=z ;i++){ val[i]={a[i]-b[i],i}; } sort(val+1,val+z+1); } for(int j=1; j<=z ;j++){ int i=val[j].se; if(vis[i]) continue; int cur=++seg_cnt; int frog=i; for(int j=1; frog ;j++){ len[cur]=j; mem[frog].push_back({cur,j}); vis[frog]=true; //segs[cur].push_back(frog); frog=nxt(frog,mode); } rt[cur]=build(1,len[cur]); } }/* for(int i=1; i<=seg_cnt ;i++){ cout << "seg " << i << ": "; for(auto c:segs[i]){ cout << c << ' '; } cout << endl; }*/ } int main(){ ios::sync_with_stdio(false);cin.tie(0); cin >> n >> m >> k >> q; for(int i=1; i<=k+q ;i++){ cin >> x[i] >> y[i]; if(mp[{x[i],y[i]}]==0){ mp[{x[i],y[i]}]=++z; a[z]=x[i]; b[z]=y[i]; } } for(int i=1; i<=z ;i++){ for(int j=0; j<4 ;j++){ int nx=a[i]+dx[j]; int ny=b[i]+dy[j]; if(mp[{nx,ny}]==0) mp.erase({nx,ny}); else dir[i][j]=mp[{nx,ny}]; } } /*for(int i=1; i<=z ;i++){ cout << i << ' ' << a[i] << ' ' << b[i] << endl; cout << "DIR: "; for(int j=0; j<4 ;j++) cout << dir[i][j] << ' '; cout << endl; }*/ build_segs(); for(int i=1; i<=k ;i++) tog(mp[{x[i],y[i]}]); cout << ask() << '\n'; for(int i=k+1; i<=k+q ;i++){ tog(mp[{x[i],y[i]}]); cout << ask() << '\n'; } } |