Descriptioninputoutputsample Input
Sample Output
HINT
The line tree sets and the check set should be a better writing practice, the time complexity of O (N^3+m*nlogn).
#include <cstdio> #include <cctype> #include <queue> #include <cstring> #include <algorithm > #define REP (i,s,t) for (int. i=s;i<=t;i++) #define DWN (I,S,T) for (int. i=s;i>=t;i--) #define REN for (int i=first[x ];i;i=next[i]) using namespace Std;inline int read () {int X=0,f=1;char c=getchar (); for (;! IsDigit (c); C=getchar ()) if (c== '-') f=-1; for (; IsDigit (c); C=getchar ()) x=x*10+c-' 0 '; return x*f;} const int Maxn=210;int n,a[maxn][maxn];struct Node {int res[2],pa[maxn*2]; int findset (int x) {return pa[x]==x?x:findset (pa[x]);} void init (int x) {res[0]=res[1]=0; Rep (i,1,n) pa[i]=i; Rep (i,1,n) {res[! a[x][i]]++; if (I>1&&a[x][i-1]==a[x][i]) res[! A[x][i]]--, pa[findset (i-1)]=findset (i); } Rep (i,1,n) pa[i+n]=pa[i]; }}t[maxn<<2];int pa[maxn*4],tmp[maxn*4];int findset (int x) {return x==pa[x]?x:pa[x]=findset (pa[x]);} void Maintain (int o,int mid) {int lc=o<<1,rc=lc|1; T[o].rEs[0]=t[lc].res[0]+t[rc].res[0]; T[O].RES[1]=T[LC].RES[1]+T[RC].RES[1]; Rep (i,1,n*2) pa[i]=t[lc].pa[i]; Rep (i,1,n*2) pa[i+2*n]=t[rc].pa[i]+2*n; Rep (i,1,n) if (A[mid][i]==a[mid+1][i]) {int X=findset (i+n), Y=findset (i+2*n); if (x!=y) {t[o].res[! a[mid][i]]--; Pa[x]=y; }} Rep (i,1,n*4) {if (i<=n) tmp[findset (i)]=i; if (i>3*n) tmp[findset (i)]=i-2*n; } Rep (I,1,n) t[o].pa[i]=tmp[pa[i]]; Rep (I,1,n) t[o].pa[i+n]=tmp[pa[i+3*n]];} void build (int o,int l,int R) {if (l==r) T[o].init (L); else {int mid=l+r>>1,lc=o<<1,rc=lc|1; Build (Lc,l,mid); build (Rc,mid+1,r); Maintain (O,MID); }}void Update (int o,int l,int R,int p) {if (l==r) T[o].init (L); else {int mid=l+r>>1,lc=o<<1,rc=lc|1; if (p<=mid) update (LC,L,MID,P); else update (RC,MID+1,R,P); Maintain (O,MID); }}int Main () {n=read (); Rep (I,1,n) Rep (j,1,n) A[i][j]=read (); Build (1,1,n); Dwn (I,read (), 1) {int x=read (), Y=read (); A[x][y]^=1; Update (1,1,N,X); printf ("%d%d\n", t[1].res[0],t[1].res[1]); } return 0;}
BZOJ1453: [Wc]dface double-sided chessboard