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
| #include<bits/stdc++.h> #define ll long long #define mp make_pair #define int short using namespace std; const int mxn=808; int gird[mxn][mxn],n,m,q; struct dsu{ int fa[mxn<<1]; inline int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);} inline void uni(int x,int y){ x=find(x),y=find(y); fa[x]=y; } inline void init(){for(int i=1;i<(mxn<<1);++i)fa[i]=i;} }f; int stand[mxn]; struct node{ dsu d; int son[2],ans[2]; inline void init(){ ans[0]=0,ans[1]=0;d.init(); son[0]=0,son[1]=0; } }t[mxn<<1]; inline void ForceUpdate(int id,int x){ t[id].init(); t[id].ans[gird[x][1]]=1; t[id].ans[1-gird[x][1]]=0; int pre=1; for(int i=1;i<=m;++i){ if(gird[x][i]!=gird[x][pre]){ ++t[id].ans[gird[x][i]]; pre=i; } t[id].d.uni(i,pre),t[id].d.uni(i+n,pre); } } inline void pushup(int id,int md,int l,int r){ f.init(); for(int i=0;i<2;++i)t[id].ans[i]=t[l].ans[i]+t[r].ans[i]; for(int i=1;i<=m*2;++i)f.fa[i]=t[l].d.fa[i],f.fa[i+m*2]=t[r].d.fa[i]+m*2; for(int i=1;i<=m;++i){ int fx=f.find(i+m),fy=f.find(i+2*m); if(gird[md][i]==gird[md+1][i] and fx!=fy){ --t[id].ans[gird[md][i]]; f.uni(fx,fy); } } memset(stand,0,sizeof(stand)); for(int i=m;i;--i){ f.fa[i]=f.find(i); stand[f.fa[i]]=i; } for(int i=m*2;i>m;--i){ f.fa[i+m*2]=f.find(i+m*2); stand[f.fa[i+m*2]]=i; } for(int i=1;i<=m;++i){ t[id].d.fa[i]=stand[f.fa[i]]; t[id].d.fa[i+m]=stand[f.fa[i+m*3]]; } } int cntnode=0,root; inline void build(int&id,int l,int r){ id=++cntnode; if(l==r){ ForceUpdate(id,l); return; } int md=l+r>>1; build(t[id].son[0],l,md); build(t[id].son[1],md+1,r); pushup(id,md,t[id].son[0],t[id].son[1]); } inline void upd(int id,int l,int r,int x){ if(l==r){ ForceUpdate(id,x); return; } int md=l+r>>1; if(x<=md)upd(t[id].son[0],l,md,x); else upd(t[id].son[1],md+1,r,x); pushup(id,md,t[id].son[0],t[id].son[1]); } inline void glhf(){ build(root,1,n); for(;q--;){ int x,y; cin>>x>>y; gird[x][y]^=1; upd(root,1,n,x); cout<<t[root].ans[1]<<' '<<t[root].ans[0]<<'\n'; } } inline void solve(){ cin>>n;m=n; for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)cin>>gird[i][j]; cin>>q; glhf(); } signed main(){ ios_base::sync_with_stdio(false); cin.tie(0),cout.tie(0); int T;T=1;
for(;T--;)solve(); return 0; }
|