【刷题】BZOJ 1453 [Wc]Dface双面棋盘
Description
Input
Output
Sample Input
Sample Output
HINT
Solution
不强制在线的动态图问题,那就LCT了
类似二分图那道题目
对于四个方向,颜色相同的连边;那么每次翻转就变成了几次删边和几次加边(注意加边在删边之后);联通块数量就变成了LCT维护的森林的数量
先把所有动态的边存下来(离线),然后找到他们被删掉的时间,以时间为权值用LCT维护最大生成树,然后就保证了不会出现非树边代替树边的情况,所以只要删掉了一条树边,就一定会把一棵树变成两棵树;而对于连边,如果这条边的两端还没联通,那这条边就一定会把两棵树变成一棵树
这题初始化比较麻烦,LCT倒是很正常(我数组不知道要开多大,试了几个数,最后变成了程序里的那样,不会RE,也不会MLE)
#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=300+10,MAXM=30000+10,MAXS=MAXN*MAXN,inf=0x3f3f3f3f;
int n,m,color[MAXN][MAXN],ans[2],dr[4][2]={{1,0},{-1,0},{0,1},{0,-1}},cnt,tmp[MAXN][MAXN],in[MAXS];
struct edge{
int u,v;
};
edge side[MAXS+MAXM];
struct data{
int id,opt,val,t,c;
inline bool operator < (const data &A) const {
return side[id].u<side[A.id].u||side[id].u==side[A.id].u&&side[id].v<side[A.id].v;
};
inline bool operator > (const data &A) const {
return t<A.t||t==A.t&&opt<A.opt;
};
};
data p[MAXS+MAXM<<1];
struct question{
int x,y;
};
question query[MAXM];
#define lc(x) ch[(x)][0]
#define rc(x) ch[(x)][1]
struct LCT{
int ch[MAXS+MAXM][2],fa[MAXS+MAXM],id[MAXS+MAXM],Mn[MAXS+MAXM],rev[MAXS+MAXM],stack[MAXS+MAXM],cnt,val[MAXS+MAXM];
inline void init()
{
memset(Mn,inf,sizeof(Mn));
memset(val,inf,sizeof(val));
}
inline bool nroot(int x)
{
return lc(fa[x])==x||rc(fa[x])==x;
}
inline void reverse(int x)
{
std::swap(lc(x),rc(x));
rev[x]^=1;
}
inline void pushup(int x)
{
Mn[x]=val[x],id[x]=x;
if(Mn[lc(x)]<Mn[x])Mn[x]=Mn[lc(x)],id[x]=id[lc(x)];
if(Mn[rc(x)]<Mn[x])Mn[x]=Mn[rc(x)],id[x]=id[rc(x)];
}
inline void pushdown(int x)
{
if(rev[x])
{
if(lc(x))reverse(lc(x));
if(rc(x))reverse(rc(x));
rev[x]=0;
}
}
inline void rotate(int x)
{
int f=fa[x],p=fa[f],c=(rc(f)==x);
if(nroot(f))ch[p][rc(p)==f]=x;
fa[ch[f][c]=ch[x][c^1]]=f;
fa[ch[x][c^1]=f]=x;
fa[x]=p;
pushup(f);
pushup(x);
}
inline void splay(int x)
{
cnt=0;
stack[++cnt]=x;
for(register int i=x;nroot(i);i=fa[i])stack[++cnt]=fa[i];
while(cnt)pushdown(stack[cnt--]);
for(register int y=fa[x];nroot(x);rotate(x),y=fa[x])
if(nroot(y))rotate((lc(y)==x)==(lc(fa[y])==y)?y:x);
pushup(x);
}
inline void access(int x)
{
for(register int y=0;x;x=fa[y=x])splay(x),rc(x)=y,pushup(x);
}
inline int findroot(int x)
{
access(x);splay(x);
while(lc(x))pushdown(x),x=lc(x);
splay(x);
return x;
}
inline void makeroot(int x)
{
access(x);splay(x);reverse(x);
}
inline void split(int x,int y)
{
makeroot(x);access(y);splay(y);
}
inline void link(int x,int y)
{
makeroot(x);fa[x]=y;
}
inline void cut(int x,int y)
{
split(x,y);fa[x]=lc(y)=0;pushup(y);
}
};
LCT T;
#undef lc
#undef rc
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char c='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(c!='\0')putchar(c);
}
inline bool cmp(data a,data b)
{
return a>b;
}
struct chess{
std::map<data,int> M;
std::map<int,int> Mp[MAXS];
inline int id(int x,int y)
{
return (x-1)*n+y;
}
inline void init()
{
int snt=0;
for(register int i=1;i<=n;++i)
for(register int j=1;j<=n;++j)
{
if(j!=n)side[++snt].u=id(i,j),side[snt].v=id(i,j+1),Mp[side[snt].u][side[snt].v]=snt;
if(i!=n)side[++snt].u=id(i,j),side[snt].v=id(i+1,j),Mp[side[snt].u][side[snt].v]=snt;
}
for(register int i=1;i<=n;++i)
for(register int j=1;j<=n;++j)
{
if(j!=n&&color[i][j]==color[i][j+1])p[++cnt].id=Mp[id(i,j)][id(i,j+1)],p[cnt].t=0,p[cnt].opt=1,p[cnt].c=color[i][j];
if(i!=n&&color[i][j]==color[i+1][j])p[++cnt].id=Mp[id(i,j)][id(i+1,j)],p[cnt].t=0,p[cnt].opt=1,p[cnt].c=color[i][j];
}
read(m);
for(register int i=1;i<=m;++i)
{
int x,y;
read(x);read(y);
query[i].x=x;query[i].y=y;
for(register int k=0;k<4;++k)
{
int dx=x+dr[k][0],dy=y+dr[k][1],u=id(x,y),v=id(dx,dy);
if(dx<1||dy<1||dx>n||dy>n)continue;
if(u>v)std::swap(u,v);
if(tmp[x][y]==tmp[dx][dy])p[++cnt].id=Mp[u][v],p[cnt].t=i,p[cnt].opt=-1;
else p[++cnt].id=Mp[u][v],p[cnt].t=i,p[cnt].opt=1;
}
tmp[x][y]^=1;
}
std::stable_sort(p+1,p+cnt+1,cmp);
for(register int i=1;i<=cnt;++i)p[i].val=m+1;
for(register int i=cnt;i>=1;--i)
{
if(M[p[i]])p[i].val=M[p[i]];
M[p[i]]=p[i].t;
}
}
inline void add(int now,int col)
{
int u=side[p[now].id].u,v=side[p[now].id].v,sn=p[now].id+n*n;
if(T.findroot(u)!=T.findroot(v))
{
ans[col]--;
T.access(sn);T.splay(sn);
T.val[sn]=p[now].val;
T.link(sn,u);T.link(sn,v);
in[sn-n*n]=1;
}
else
{
T.split(u,v);
if(p[now].val>T.Mn[v])
{
int so=T.id[v];
T.cut(so,side[so-n*n].u);T.cut(so,side[so-n*n].v);
T.val[sn]=p[now].val;
T.link(sn,u);T.link(sn,v);
in[so-n*n]=0;in[sn-n*n]=1;
}
}
}
inline void del(int now,int col)
{
if(!in[p[now].id])return ;
int u=side[p[now].id].u,v=side[p[now].id].v,sn=p[now].id+n*n;
T.cut(sn,u);T.cut(sn,v);
in[sn-n*n]=0;
ans[col]++;
}
};
chess G;
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
int main()
{
read(n);
for(register int i=1;i<=n;++i)
for(register int j=1;j<=n;++j)
{
read(color[i][j]),ans[color[i][j]]++;
tmp[i][j]=color[i][j];
}
G.init();
T.init();
int j=1;
for(;j<=cnt&&p[j].t==0;++j)G.add(j,p[j].c);
for(register int i=1;i<=m;++i)
{
int x=query[i].x,y=query[i].y,pcol=color[x][y];
for(;j<=cnt&&p[j].t<=i;++j)
if(p[j].opt==-1)G.del(j,pcol);
else G.add(j,pcol^1);
ans[pcol]--;ans[pcol^1]++;
color[x][y]^=1;
write(ans[1],' ');write(ans[0],'\n');
}
return 0;
}