LCT
不知道为啥,我做题老是碰到LCT的题,而且一眼能看出来,苦于不会写LCT,只能拿到60的部分分,所以LittlePrincess来学一发LCT~
这东西干啥?
最常见的应用(或许)——可删边并查集
这玩意好写吗
还可以。
为什么这篇博客没有有趣的LittlePrincess历险记
因为我决定我讲的LCT太烂于是我交给:
1. http://www.cnblogs.com/flashhu/p/8324551.html %Candy% dalao
2.https://www.zybuluo.com/xzyxzy/note/1027479 %XZY% dalao
3.http://www.cnblogs.com/flashhu/p/8324551.html %flashhu% dalao
排名不分先后
在此之前,提醒诸位一点:
代码中所有f[],ch[][]数组储存的均为splay中的信息,在学习的时候尽量从”splay如何找到原树 “的方向考虑。
最后 斗胆分类一下:
篇1:讲到了很多细节(如pushdown的影响与一下微操作)
篇2:适合初学者看,语言易懂,例子很好,有图!(这里好像有点小失误,在我的代码中有标识)
篇3:julao版本不解释
最后上代码(储存)
#include<bits/stdc++.h>
using namespace std;
#define so(i,j,k,l) for(int i=j;i<=k;i+=l)
#define MN 300030
#define lc ch[x][0]
#define rc ch[x][1]
#define I void
#define R register int
int ch[MN][2],n,m,v[MN],s[MN],st[MN],f[MN],opt,x,y;
bool tag[MN];
bool nroot(int x)
{return ch[f[x]][0]==x||ch[f[x]][1]==x;}
void pushup(int x)
{
s[x]=s[ch[x][0]]^s[ch[x][1]]^v[x];
}
void pushr(int x)
{
swap(ch[x][0],ch[x][1]);
tag[x]^=1;
}
void pushdown(int x)
{
if(tag[x])
{
if(ch[x][0]) pushr(ch[x][0]);
if(ch[x][1]) pushr(ch[x][1]);
tag[x]=0;
}
}
void rotate(int x)
{
int old=f[x],oldf=f[old],which=ch[old][1]==x,w=ch[x][!which];
if(nroot(old)) ch[oldf][ch[oldf][1]==old]=x;
ch[x][which^1]=old;ch[old][which]=w;
if(w) f[w]=old;
f[old]=x;f[x]=oldf;
pushup(old);
}
void splay(int x)
{
int y=x,z=0;
st[++z]=y;
while(nroot(y)) st[++z]=y=f[y];
while(z) pushdown(st[z--]);
while(nroot(x))
{
int fa=f[x],ffa=f[fa];
if(nroot(fa))
rotate((ch[fa][0]==x)^(ch[ffa][0]==fa)?x:fa);
rotate(x);
}
pushup(x);
}
void access(int x)
{
for(int y=0;x;x=f[y=x])
{
splay(x),ch[x][1]=y,pushup(x);
}
}
void mkrt(int x)
{
access(x);splay(x);
pushr(x);
}
int findrt(int x)
{
access(x),splay(x);
while(ch[x][0]) pushdown(x),x=ch[x][0];
return x;
}
void split(int x,int y)
{
mkrt(x),access(y),splay(y);
}
void link(int x,int y)
{
mkrt(x);
if(findrt(x)!=findrt(y)) f[x]=y;
}
void cut(int x,int y)
{
split(x,y);//刚刚说的失误是这里 原作写错为mkrt(x)?(雾
if(findrt(x)==findrt(y)&&f[x]==y&&!ch[x][1])
{
f[x]=ch[y][0]=0;pushup(y);
}
}
int main()
{
scanf("%d%d",&n,&m);
so(i,1,n,1) scanf("%d",&v[i]);
while(m--)
{
scanf("%d%d%d",&opt,&x,&y);
switch(opt)
{
case 0:split(x,y);printf("%d\n",s[y]);break;
case 1:link(x,y);break;
case 2:cut(x,y);break;
case 4:splay(x);v[x]=y;
}
}
return 0;
}