最近访问链剖分

· · 个人记录

引入

如果你需要在一棵树上实现单点修改、路径上信息询问,你可以实现重链剖分实现这个问题。

但是,如果这里不是一棵树呢?如果是多棵树,伴随着边的连接、断开,我们该怎么办呢?

【模板】动态树(LCT),比如这个题。我们发现,在这个时候,如果只用原本预处理链的思路是无法实现的。

我们需要想一个办法动态地处理连边和断边的信息。

连接,还需要保证时间复杂度?那也就是说,我们需要在这上面进行一些操作稳定复杂度?

这很难不让人想起 Splay 大人。

算法思想

我们把每一段实链用 Splay 存起来。

在实现上,我们会往右方向去连接,也就是说,一个 1 -> 2 -> 3 -> 4,建出来是这样的:

那么这个实链的中序遍历顺序是 4,3,2,1。

我们引入一个操作 access(x),用于打通一个点到其根节点的路径,并将 x 换到中序遍历的最后一个。

比如我们在这里 access(3),然后这棵树就会变成:

我们再引入一个“虚链”的概念,这个就是不是实链的连接。

比如这个:

假如实链是 6 -> 5,3 -> 4 和 2 -> 1。

那么虚链就是 1 -> 4 和 4 -> 5,这两条边只标记了 fa 的信息。

那么这个时候,我们将 6 -> 5 断开,然后将 4 接到右儿子上,那么就变成了这样:

这个时候,6 -> 5 和 1 -> 4 是虚链。

而 3 变成了 3 -> 4 -> 5 这条实链中序遍历最右侧的点。

但是,如果我们对 3 连边,显然不能对叶子节点操作,所以我们还需要用一次 splay(3) 将 3 转到顶上。

然后这条实链就变成了:

但是,这个是向左的一条链,有问题,我们需要将左右儿子交换,变成这样。

随后我们才可以进行操作。刚才这个操作叫做 makeroot(x),将 x 设置为根节点。

其分为三步:access(x),splay(x),reverse(x)。

:::success[makeroot 函数]

inline void makeroot(int x){
  access(x);splay(x);reverse(x);
}

:::

那么,access 怎么写呢?

首先,我们从 x 开始,一直向上走,直到根节点。

对于每一个点,我们都进行 splay(x),找到 x 原本的右儿子,将其断开,连接上。

此处是借助 fa 来跳的,因为我们的虚链上都只有 fa 信息,而没有对应的儿子信息,最后,更改了别忘记 pushup。

具体实现大概长这样。

:::success[access 函数]

inline void access(int x){
    for(int son=0;x;son=x,x=tr[x].fa){
        splay(x);tr[x].ch[1]=son;pushup(x);
    }
}

:::

至于 reverse,我们延续数据结构的懒标记思想,打一个标记 tag 去实现。

具体实现大概长这样。

:::success[reverse 函数]

inline void reverse(int x){//翻转左右子树 
    if(!x) return;//注意空白节点不能有信息
    swap(tr[x].ch[0],tr[x].ch[1]);
    tr[x].tag^=1; 
}

:::

那么,这里就有一个关键的点,我们在 splay(x) 的时候,需要先下放这里的懒标记,也就是说,我们还需要一个 push(x) 下放懒标记。

push 函数的具体实现大概长这样。

:::success[push 函数]

inline void push(int x){//将整棵树的懒标记信息全部下放 
    if(!check(x)) push(tr[x].fa);
    pushdown(x);
}

:::

至于 pushdown,这是和数据结构类似的,具体实现大概长这样。

:::success[pushdown 函数]

inline void pushdown(int x){
    if(tr[x].tag){
        tr[x].tag=0;
        reverse(tr[x].ch[0]);
        reverse(tr[x].ch[1]);
    }
}

:::

随后就是 splay 函数了,具体实现大概长这样。

:::success[splay 函数]

inline void splay(int x){//LCT 中只需要转到根节点 
    push(x);//下放全部懒标记 
    while(!check(x)){
        int y=tr[x].fa,z=tr[y].fa;
        if(!check(y)){//有祖父 
            if(son(x) xor son(y)) rotate(x);//不是一字型,直接转 
            else rotate(y);//否则先把一字转开 
        }
        rotate(x);//保证 x 上升至少一步 
    }
    pushup(x);
}

:::

你可能会想这里怎么去判 root,因为现在 root 不唯一了。

我们先前不是有刻意防止空白节点被覆盖信息吗,在这里就能派上用场。

如果一个点是根节点,其父亲就是空的,两个儿子都不是他。

即我们可以直接宏定义出来这个函数。

:::success[check 函数宏定义]

#define check(x) ((tr[tr[x].fa].ch[0]!=x) && (tr[tr[x].fa].ch[1]!=x))

:::

另外,这里的 son(x) 是用来判断 x 是其父亲的哪一个儿子的,这个也很经典了,同样宏定义出来。

:::success[son 函数宏定义]

#define son(x) (tr[tr[x].fa].ch[1]==x)

:::

至于 splay 的 rotate,这里是类似的,唯一需要注意的是不要给空白节点上信息,所以需要加两个判断。

:::success[rotate 函数]

inline void rotate(int x){
    int y=tr[x].fa,z=tr[y].fa;int k=son(x);
    tr[x].fa=z;if(!check(y)) tr[z].ch[son(y)]=x;//注意,因为我们必须借助根节点父亲为空的性质,所以要保证根节点的父亲的信息不会被影响 
    tr[y].ch[k]=tr[x].ch[k^1];if(tr[x].ch[k^1]) tr[tr[x].ch[k^1]].fa=y;//y 继承 x 的一个子树,但是我们要保证空节点不能有信息 
    tr[y].fa=x,tr[x].ch[k^1]=y;//x 与 y 的关系  
    pushup(y);pushup(x);//自下往上 pushup 
}

:::

所以,结构体里面存储的信息就是这样的。

:::success[结构体信息]

struct node{
    int ch[2];//儿子 
    int val;//当前值 
    int fa;//父亲 
    int num;//当前 xor 和 
    int tag;//要不要反转左右子树 
}tr[N];

:::

然后 pushup 也就简单了。

:::success[pushup 函数]

inline void pushup(int x){
    tr[x].num=tr[tr[x].ch[0]].num xor tr[tr[x].ch[1]].num xor tr[x].val;
}

:::

具体功能

有了这些基本操作,我们就可以实现很多功能了。

连边

我们发现其原理就是放到根节点后连接。

:::success[link 函数]

inline void link(int x,int y){
  makeroot(x);splay(x);tr[x].fa=y;
}

:::

当然,有时题目不会保证 x 和 y 是不连通的,我们还需要写一个 findroot 函数。

findroot 不需要对原树做操作,所以你并不需要去 reverse(x) 以保证其向右偏的性质。

所以,我们只需要 access(x) 后 splay(x)。

然后中序遍历的第一个就是根节点,也就是一直往左走。

:::success[findroot 函数]

inline int findroot(int x){//找到当前树根 
    access(x);splay(x);
    while(tr[x].ch[0]) x=tr[x].ch[0];
    return x;
}

:::

但是你写一个 reverse(x) 也没问题,但是会把内存加大很多。

单独拉出一部分

你要支持查询路径,按理来说,应该要支持将 x -> y 中的信息 split 出来。

那么这个怎么做呢?

还是比较简单的,不难想出一个思路,我们先将其中一个变成根节点,然后把另一个翻上去。

也就是说:

:::success[split 函数]

inline void split(int x,int y){
  makeroot(x);access(y);splay(y);
}

:::

同样这里不需要 reverse(y)。

删除一条边

根据我们上面的“单独拉出一部分”,我们可以先 split(x,y)。

这个时候 x 一定会在 y 左侧,因为 y 原本的儿子应该在 y 右侧。

然后我们将这个儿子和父亲给归零即可。

当然,为了防止不是这个连接,我们需要加一个特判。

:::success[cut 函数]

inline void cut(int x,int y){
    split(x,y);
    if(tr[x].ch[1] || tr[y].ch[0]!=x) return;//如果没有连接就不管 
    tr[y].ch[0]=0;tr[x].fa=0;//清空 
}

:::

查询一个路径

这个就简单了,因为 split(x,y) 可以被 y 放到顶上,我们直接对其处理就好。

:::success[ask 函数]

inline int ask(int x,int y){
  split(x,y);return tr[y].num;
}

:::

单点更改

这个可以直接 splay 处理,毕竟只涉及一个点。

:::success[change 函数]

inline void change(int x,int k){
  splay(x);tr[x].val=k;pushup(x);
}

:::

拼起来就是模板题的代码。

:::success[模板题代码]

#include<bits/stdc++.h>
#define check(x) ((tr[tr[x].fa].ch[0]!=x) && (tr[tr[x].fa].ch[1]!=x)) //如果 x 的父亲两个儿子都不是 x,那么 x 是根节点 
#define son(x) (tr[tr[x].fa].ch[1]==x) //x 是其父亲的哪一个儿子 
using namespace std;

const int N=1e5+5;
struct node{
    int ch[2];//儿子 
    int val;//当前值 
    int fa;//父亲 
    int num;//当前 xor 和 
    int tag;//要不要反转左右子树 
}tr[N];

inline int read(){
    int s=0;char ch=getchar();
    while(!isdigit(ch)) ch=getchar();
    while(isdigit(ch)) s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
    return s;
}

inline void pushup(int x){
    tr[x].num=tr[tr[x].ch[0]].num xor tr[tr[x].ch[1]].num xor tr[x].val;
}

inline void reverse(int x){//翻转左右子树 
    if(!x) return;//注意空白节点不能有信息
    swap(tr[x].ch[0],tr[x].ch[1]);
    tr[x].tag^=1; 
}

inline void pushdown(int x){
    if(tr[x].tag){
        tr[x].tag=0;
        reverse(tr[x].ch[0]);
        reverse(tr[x].ch[1]);
    }
}

inline void rotate(int x){
    int y=tr[x].fa,z=tr[y].fa;int k=son(x);
    tr[x].fa=z;if(!check(y)) tr[z].ch[son(y)]=x;//注意,因为我们必须借助根节点父亲为空的性质,所以要保证根节点的父亲的信息不会被影响 
    tr[y].ch[k]=tr[x].ch[k^1];if(tr[x].ch[k^1]) tr[tr[x].ch[k^1]].fa=y;//y 继承 x 的一个子树,但是我们要保证空节点不能有信息 
    tr[y].fa=x,tr[x].ch[k^1]=y;//x 与 y 的关系  
    pushup(y);pushup(x);//自下往上 pushup 
}

inline void push(int x){//将整棵树的懒标记信息全部下放 
    if(!check(x)) push(tr[x].fa);
    pushdown(x);
}

inline void splay(int x){//LCT 中只需要转到根节点 
    push(x);//下放全部懒标记 
    while(!check(x)){
        int y=tr[x].fa,z=tr[y].fa;
        if(!check(y)){//有祖父 
            if(son(x) xor son(y)) rotate(x);//不是一字型,直接转 
            else rotate(y);//否则先把一字转开 
        }
        rotate(x);//保证 x 上升至少一步 
    }
    pushup(x);
}

inline void access(int x){//从 x 打通到根节点 
    for(int son=0;x;son=x,x=tr[x].fa){//一直向上移动,把这里的虚链变成实链,相连的无效实链变成虚链 
        splay(x);tr[x].ch[1]=son;pushup(x);
    }
} 

inline void makeroot(int x){//把当前点变成根节点 
    access(x);splay(x);reverse(x);
}

inline void split(int x,int y){//把 x -> y 拉出来 
    makeroot(x);access(y);splay(y);//把 x 变成根,再把 y 拉上来 
}

inline void link(int x,int y){
    makeroot(x);tr[x].fa=y;//把 x 拉成根,接上 y 
}

inline void cut(int x,int y){
    split(x,y);
    if(tr[x].ch[1] || tr[y].ch[0]!=x) return;//如果没有连接就不管 
    tr[y].ch[0]=0;tr[x].fa=0;//清空 
}

inline int findroot(int x){//找到当前树根 
    access(x);splay(x);
    while(tr[x].ch[0]) x=tr[x].ch[0];
    return x;
} 

int main(){
    int n,m;cin>>n>>m;
    for(int i=1;i<=n;i++){
        tr[i].val=read();
        tr[i].num=tr[i].val;
    }
    while(m--){
        int opt=read(),x=read(),y=read();
        if(opt==0){
            split(x,y);
            printf("%d\n",tr[y].num);
        }else if(opt==1){
            if(findroot(x)!=findroot(y)) link(x,y);//判断不在同一块 
        }else if(opt==2){
            cut(x,y);
        }else{
            splay(x);
            tr[x].val=y;
            pushup(x);
        }
    }
    return 0;
}

:::

经典错法

inline void rotate(int x){//旋转 x
    int y=tr[x].fa,z=tr[y].fa;int k1=son(x),k2=son(y);
    tr[x].fa=z;if(!check(y)) tr[z].ch[k2]=x;//x 与 z
    tr[x].ch[k1^1]=y;tr[y].fa=x;//x 与 y
    tr[y].ch[k1]=tr[x].ch[k1^1];if(tr[x].ch[k1^1]) tr[tr[x].ch[k1^1]].fa=y;//y 与其儿子
    pushup(y);pushup(x);
}

需要换成:

inline void rotate(int x){//旋转 x
    int y=tr[x].fa,z=tr[y].fa;int k1=son(x),k2=son(y);
    tr[x].fa=z;if(!check(y)) tr[z].ch[k2]=x;//x 与 z
    tr[y].ch[k1]=tr[x].ch[k1^1];if(tr[x].ch[k1^1]) tr[tr[x].ch[k1^1]].fa=y;//y 与其儿子
    tr[x].ch[k1^1]=y;tr[y].fa=x;//x 与 y
    pushup(y);pushup(x);
}

维护更多懒标记

这是一个数据结构,显然能维护的东西不应该这么少。

尝试使用 LCT 维护路径加,路径乘之类的信息。

想出思路是比较简单的,先 split(x,y),然后打一个 tag 就好了。

怎么下放呢?第一次处理这条链的 pushdown 时,直接按照儿子去下方懒标记就好。

[国家集训队] Tree II

直接写就好了,思路已在上方给出。

注意这里还需要维护一个 sz 用来处理加法。

:::success[代码]

#include<bits/stdc++.h>
#define int unsigned int
#define check(x) ((tr[tr[x].fa].ch[0]!=x) && (tr[tr[x].fa].ch[1]!=x)) //当前的 x 是否为根节点
#define son(x) (tr[tr[x].fa].ch[1]==x) //x 是父亲的哪一个儿子
using namespace std;

const int N=1e5+5,Mod=51061;
struct node{int ch[2],fa,val,sum,tag,tag1,tag2,sz;}tr[N];
/*tag 是反转,tag1 是加,tag2 是乘*/

inline int read(){
    int s=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}
    while(isdigit(ch)) s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
    return s*f;
}

inline void pushup(int x){
    tr[x].sz=tr[tr[x].ch[0]].sz+tr[tr[x].ch[1]].sz+1;
    tr[x].sum=(tr[tr[x].ch[0]].sum+tr[tr[x].ch[1]].sum+tr[x].val)%Mod;
}

inline void rotate(int x){//单旋
    int y=tr[x].fa,z=tr[y].fa;int k=son(x);
    tr[x].fa=z;if(!check(y)) tr[z].ch[son(y)]=x;
    tr[y].ch[k]=tr[x].ch[k^1];if(tr[x].ch[k^1]) tr[tr[x].ch[k^1]].fa=y;
    tr[x].ch[k^1]=y,tr[y].fa=x;
    pushup(y);pushup(x);
}

inline void reverse(int x){//反转当前点
    tr[x].tag^=1;
    swap(tr[x].ch[0],tr[x].ch[1]);
}

inline void pushdown(int x){//下放当前点的懒标记
    int ls=tr[x].ch[0],rs=tr[x].ch[1];
    if(tr[x].tag){
        tr[x].tag=0;
        reverse(ls);
        reverse(rs);
    }
    if(tr[x].tag2!=1){
        if(ls){
            tr[ls].sum=(tr[ls].sum*tr[x].tag2)%Mod;
            tr[ls].val=(tr[ls].val*tr[x].tag2)%Mod;
            tr[ls].tag1=(tr[ls].tag1*tr[x].tag2)%Mod;
            tr[ls].tag2=(tr[ls].tag2*tr[x].tag2)%Mod;
        }
        if(rs){
            tr[rs].sum=(tr[rs].sum*tr[x].tag2)%Mod;
            tr[rs].val=(tr[rs].val*tr[x].tag2)%Mod;
            tr[rs].tag1=(tr[rs].tag1*tr[x].tag2)%Mod;
            tr[rs].tag2=(tr[rs].tag2*tr[x].tag2)%Mod;
        }
        tr[x].tag2=1;
    }
    if(tr[x].tag1){
        if(ls){
            tr[ls].sum=(tr[ls].sum+(tr[x].tag1*tr[ls].sz)%Mod)%Mod;
            tr[ls].val=(tr[ls].val+tr[x].tag1)%Mod;
            tr[ls].tag1=(tr[ls].tag1+tr[x].tag1)%Mod;
        }
        if(rs){
            tr[rs].sum=(tr[rs].sum+(tr[x].tag1*tr[rs].sz)%Mod)%Mod;
            tr[rs].val=(tr[rs].val+tr[x].tag1)%Mod;
            tr[rs].tag1=(tr[rs].tag1+tr[x].tag1)%Mod;
        }
        tr[x].tag1=0;
    }
}

inline void push(int x){//下放当前链的懒标记
    if(!check(x)) push(tr[x].fa);
    pushdown(x);
}

inline void splay(int x){//把 x 转到链顶
    push(x);
    while(!check(x)){
        int y=tr[x].fa,z=tr[y].fa;
        if(!check(y)){
            if(son(x) xor son(y)) rotate(x);
            else rotate(y);
        }
        rotate(x);
    }
    pushup(x);
}

inline void access(int x){//打通 x 到根的路径
    for(int son=0;x;son=x,x=tr[x].fa){
        splay(x);tr[x].ch[1]=son;pushup(x);
    }
}

inline void makeroot(int x){//将 x 变为整棵树的根
    access(x);splay(x);reverse(x);
}

inline void split(int x,int y){//只保留 x -> y 的路径
    makeroot(x);access(y);splay(y);
}

inline void link(int x,int y){//连边
    makeroot(x);tr[x].fa=y;
}

inline void cut(int x,int y){//断边
    split(x,y);
    tr[x].fa=tr[y].ch[0]=0;
}

signed main(){
    int n,q;cin>>n>>q;
    for(int i=1;i<=n;i++) tr[i].tag2=1,tr[i].val=tr[i].sum=1,tr[i].sz=1;//初始值
    for(int i=1;i<n;i++){int x=read(),y=read();link(x,y);}
    while(q--){
        char ch;cin>>ch;
        if(ch=='+'){
            int x=read(),y=read(),val=read();
            split(x,y);tr[y].tag1=(tr[y].tag1+val)%Mod;
            tr[y].val=(tr[y].val+val)%Mod;tr[y].sum=(tr[y].sum+(1ll*tr[y].sz*val)%Mod)%Mod;
        }else if(ch=='-'){
            int x=read(),y=read();cut(x,y);
            x=read(),y=read();link(x,y);
        }else if(ch=='*'){
            int x=read(),y=read(),val=read();
            split(x,y);tr[y].tag1=(1ll*tr[y].tag1*val)%Mod,tr[y].tag2=(tr[y].tag2*val)%Mod;
            tr[y].val=(tr[y].val*val)%Mod;tr[y].sum=(tr[y].sum*val)%Mod;
        }else{
            int x=read(),y=read();
            split(x,y);cout<<tr[y].sum<<"\n";
        }
    }
    return 0;
}

:::

简单建模

[SDOI2008] 洞穴勘测

维护联通,简单题。

:::success[代码]

#include<bits/stdc++.h>
#define check(x) ((tr[tr[x].fa].ch[0]!=x) && (tr[tr[x].fa].ch[1]!=x)) //当前 x 是否为根节点
#define son(x) (tr[tr[x].fa].ch[1]==x) //当前 x 是父亲的哪一个儿子
using namespace std;

const int N=1e4+5;
struct node{int ch[2],fa,tag;}tr[N];

inline int read(){
    int s=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}
    while(isdigit(ch)) s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
    return s*f;
}

inline void rotate(int x){
    int y=tr[x].fa,z=tr[y].fa,k=son(x);
    tr[x].fa=z;if(!check(y)) tr[z].ch[son(y)]=x;
    tr[y].ch[k]=tr[x].ch[k^1];if(tr[x].ch[k^1]) tr[tr[x].ch[k^1]].fa=y;
    tr[y].fa=x,tr[x].ch[k^1]=y;
}

inline void reverse(int x){
    tr[x].tag^=1;
    swap(tr[x].ch[0],tr[x].ch[1]);
}

inline void pushdown(int x){
    if(tr[x].tag){
        tr[x].tag=0;
        reverse(tr[x].ch[0]);
        reverse(tr[x].ch[1]);
    }
}

inline void push(int x){
    if(!check(x)) push(tr[x].fa);
    pushdown(x);
}

inline void splay(int x){
    push(x);
    while(!check(x)){
        int y=tr[x].fa,z=tr[y].fa;
        if(!check(y)){
            if(son(x) xor son(y)) rotate(x);
            else rotate(y);
        }
        rotate(x);
    }
}

inline void access(int x){
    for(int son=0;x;son=x,x=tr[x].fa){
        splay(x);tr[x].ch[1]=son;
    }
}

inline void makeroot(int x){
    access(x);splay(x);reverse(x);
}

inline int findroot(int x){
    access(x);splay(x);
    while(tr[x].ch[0]) x=tr[x].ch[0];
    return x;
}

inline void split(int x,int y){
    makeroot(x);access(y);splay(y);
}

inline void link(int x,int y){
    makeroot(x);tr[x].fa=y;
}

inline void cut(int x,int y){
    split(x,y);tr[y].ch[0]=tr[x].fa=0;
}

int main(){
    int n,T;cin>>n>>T;
    while(T--){
        string s;cin>>s;
        int x=read(),y=read();
        if(s=="Query"){
            if(findroot(x)!=findroot(y)) puts("No");
            else puts("Yes");
        }else if(s=="Connect") link(x,y);
        else cut(x,y);
    }
    return 0;
}

:::

[HNOI2010] 弹飞绵羊。

题目大意:到达一个节点后会向后运动一定格数,每个地方不同,可能会更改运动的格数。

询问你从一个节点开始需要被弹多少次才会被弹出去。

将往后弹的操作进行图论建模是显然的。

这里的更改就等价于连虚边了。

我们在最后建立一个虚点 n+1 代表被弹飞了

那么我们往后连,根节点一直都是 n+1。

那么我们也就只需要支持 link 和 cut 就好。

:::success[代码]

#include<bits/stdc++.h>
#define check(x) ((tr[tr[x].fa].ch[0]!=x) && (tr[tr[x].fa].ch[1]!=x)) 
#define son(x) (tr[tr[x].fa].ch[1]==x)
using namespace std;

const int N=2e5+5;
struct node{
    int ch[2];
    int sz;
    int fa;
    int tag;
}tr[N];
int a[N];

inline void pushup(int x){tr[x].sz=tr[tr[x].ch[0]].sz+tr[tr[x].ch[1]].sz+1;}

inline void rotate(int x){
    int y=tr[x].fa,z=tr[y].fa;
    int k1=son(x),k2=son(y);
    tr[x].fa=z;if(!check(y)) tr[z].ch[k2]=x;//不能让空节点获得信息 
    tr[y].ch[k1]=tr[x].ch[k1^1];if(tr[x].ch[k1^1]) tr[tr[x].ch[k1^1]].fa=y;//不能让空节点获得信息 
    tr[y].fa=x,tr[x].ch[k1^1]=y;
    pushup(y);pushup(x);//依旧忘记 pushup 
}

inline void reverse(int x){
    if(!x) return;
    tr[x].tag^=1;
    swap(tr[x].ch[0],tr[x].ch[1]);
}

inline void pushdown(int x){
    if(!tr[x].tag) return;
    reverse(tr[x].ch[0]);
    reverse(tr[x].ch[1]);
    tr[x].tag=0;
}

inline void push(int x){
    if(!check(x)) push(tr[x].fa);
    pushdown(x);
}

inline void splay(int x){//转到根节点 
    push(x);
    while(!check(x)){
        int y=tr[x].fa,z=tr[y].fa;
        if(!check(y)){//不能让根节点获得信息 
            if(son(x) xor son(y)) rotate(x);//不是一字型 
            else rotate(y);//一字型 
        }
        rotate(x);//保证 x 上升 
    }
    pushup(x);
}

inline void access(int x){
    for(int son=0;x;son=x,x=tr[x].fa){
        splay(x);tr[x].ch[1]=son;pushup(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 cut(int x,int y){
    split(x,y);tr[y].ch[0]=0;tr[x].fa=0;pushup(y);
}

inline void link(int x,int y){
    makeroot(x);tr[x].fa=y;
}

inline int read(){
    int s=0;char ch=getchar();
    while(!isdigit(ch)) ch=getchar();
    while(isdigit(ch)) s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
    return s;
}

int main(){
    int n=read();
    for(int i=1;i<=n;i++){
        int x=read();a[i]=x;
        if(i+x<=n) link(i,i+x);
        else link(i,n+1); 
    }
    int T;cin>>T;
    while(T--){
        int opt=read();
        if(opt==1){
            int x=read()+1;//警示后人,编号是从 0 开始的(哭) 
            makeroot(x);access(n+1);splay(n+1);
            cout<<tr[n+1].sz-1<<"\n";
        }else{
            int x=read()+1,k=read();//警示后人,编号是从 0 开始的(哭) 
            int y=(x+a[x]>n?n+1:x+a[x]);cut(x,y);
            a[x]=k;y=(x+a[x]>n?n+1:x+a[x]);link(x,y);
        }
    }   
    return 0;
}

:::

[BJOI2014] 大融合

注意到查询等价于两侧子树大小之积。

所以查询就只需要断边,查询子树大小之积,再把边重新连上就好。

但是我们直接在 LCT 上记录之能够得到实子树的子树大小,这里还需要统计虚子树的信息。

所以我们还需要定义一个 ex_{sz},记录虚子树的字数大小。

此时需要更改 link、access、pushup。

:::info[修正 link]

inline void link(int x,int y){//连接 x -> y
    makeroot(x);makeroot(y);
    tr[x].fa=y;tr[y].exsz+=tr[x].sz;
    pushup(y);
}

:::

:::info[修正 access]

inline void access(int x){//打通 x 到根节点的路径
    for(int son=0;x;son=x,x=tr[x].fa){
        splay(x);tr[x].exsz+=tr[tr[x].ch[1]].sz-tr[son].sz;
        tr[x].ch[1]=son;pushup(x);
    }
}

:::

:::info[修正 pushup]

inline void pushup(int x){tr[x].sz=tr[tr[x].ch[0]].sz+tr[tr[x].ch[1]].sz+tr[x].exsz+1;}

:::

:::success[代码]

#include<bits/stdc++.h>
#define check(x) (tr[tr[x].fa].ch[0]!=x && tr[tr[x].fa].ch[1]!=x) //当前 x 是否为根节点
#define son(x) (tr[tr[x].fa].ch[1]==x) //当前 x 是父亲的哪一个儿子
using namespace std;

const int N=1e5+5;
struct node{int ch[2],fa,sz,tag,exsz;}tr[N];

inline int read(){
    int s=0;char ch=getchar();
    while(!isdigit(ch)) ch=getchar();
    while(isdigit(ch)) s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
    return s;
}

inline void pushup(int x){tr[x].sz=tr[tr[x].ch[0]].sz+tr[tr[x].ch[1]].sz+tr[x].exsz+1;}

inline void rotate(int x){//旋转 x
    int y=tr[x].fa,z=tr[y].fa;int k=son(x);
    tr[x].fa=z;if(!check(y)) tr[z].ch[son(y)]=x;//x & z
    tr[y].ch[k]=tr[x].ch[k^1];if(tr[x].ch[k^1]) tr[tr[x].ch[k^1]].fa=y;//y & y 的新儿子
    tr[y].fa=x,tr[x].ch[k^1]=y;
    pushup(y);pushup(x);
}

inline void reverse(int x){//反转一个点
    tr[x].tag^=1;
    swap(tr[x].ch[0],tr[x].ch[1]);
}

inline void pushdown(int x){//下方当前点的懒标记
    if(tr[x].tag){
        tr[x].tag=0;
        reverse(tr[x].ch[0]);
        reverse(tr[x].ch[1]);
    }
}

inline void push(int x){//下放整棵树的懒标记
    if(!check(x)) push(tr[x].fa);
    pushdown(x);
}

inline void splay(int x){//将 x 转到当前链的根
    push(x);
    while(!check(x)){
        int y=tr[x].fa,z=tr[y].fa;
        if(!check(y)){
            if(son(x) xor son(y)) rotate(x);
            else rotate(y);
        }
        rotate(x);
    }
    pushup(x);
}

inline void access(int x){//打通 x 到根节点的路径
    for(int son=0;x;son=x,x=tr[x].fa){
        splay(x);tr[x].exsz+=tr[tr[x].ch[1]].sz-tr[son].sz;
        tr[x].ch[1]=son;pushup(x);
    }
}

inline void makeroot(int x){//把 x 变成整棵树的根
    access(x);splay(x);reverse(x);
}

inline void link(int x,int y){//连接 x -> y
    makeroot(x);makeroot(y);
    tr[x].fa=y;tr[y].exsz+=tr[x].sz;
    pushup(y);
}

inline void split(int x,int y){//分割出 x -> y 的路径
    makeroot(x);access(y);splay(y);
}

inline void cut(int x,int y){//断边 x -> y
    split(x,y);tr[x].fa=tr[y].ch[0]=0;
}

int main(){
    int n,m;cin>>n>>m;
    while(m--){
        char ch;cin>>ch;
        if(ch=='A'){
            int x=read(),y=read();link(x,y);
        }else{//断开这条边,查询两侧子树大小之积,再连接
            int x=read(),y=read();
            cut(x,y);makeroot(x);makeroot(y);
            cout<<1ll*tr[x].sz*tr[y].sz<<"\n";link(x,y);
        }   
    }
    return 0;
}

:::

[ZJOI2012] 网络

定时训练写错 findroot 没过这道题,怎么回事呢QwQ

注意到 C \le 10,考虑直接开 10 个 LCT,维护的信息是简单的。

:::success[代码]

#include<bits/stdc++.h>
using namespace std;

const int N=1e5+5,K=12;
unordered_map<int,int> mp[N];//记录对应边的颜色
struct Link_Cut_Tree{
    struct node{int ch[2],fa,val,tag,maxn;}tr[N];int cnt[N];
    #define check(x) ((tr[tr[x].fa].ch[0]!=x) && (tr[tr[x].fa].ch[1]!=x)) 
    #define son(x) (tr[tr[x].fa].ch[1]==x)

    inline void pushup(int x){tr[x].maxn=max(tr[x].val,max(tr[tr[x].ch[0]].maxn,tr[tr[x].ch[1]].maxn));}

    inline void rotate(int x){
        int y=tr[x].fa,z=tr[y].fa;int k=son(x);
        tr[x].fa=z;if(!check(y)) tr[z].ch[son(y)]=x;
        tr[y].ch[k]=tr[x].ch[k^1];if(tr[x].ch[k^1]) tr[tr[x].ch[k^1]].fa=y;
        tr[y].fa=x,tr[x].ch[k^1]=y;
        pushup(y);pushup(x);
    }

    inline void reverse(int x){
        if(!x) return;tr[x].tag^=1;
        swap(tr[x].ch[0],tr[x].ch[1]);
    }

    inline void pushdown(int x){
        if(tr[x].tag){
            tr[x].tag=0;
            reverse(tr[x].ch[0]);
            reverse(tr[x].ch[1]);
        }
    }

    inline void push(int x){
        if(!check(x)) push(tr[x].fa);
        pushdown(x);
    }

    inline void splay(int x){
        push(x);
        while(!check(x)){
            int y=tr[x].fa,z=tr[y].fa;
            if(!check(y)){
                if(son(x) xor son(y)) rotate(x);
                else rotate(y);
            }
            rotate(x);
        }
        pushup(x);
    }

    inline void access(int x){
        for(int son=0;x;son=x,x=tr[x].fa){
            splay(x);tr[x].ch[1]=son;pushup(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 int findroot(int x){
        access(x);splay(x);//注意这里不能 reverse,不然下面就下不去了QwQ 
        while(tr[x].ch[0]) x=tr[x].ch[0];
        return x;
    }

    inline void link(int x,int y){
        cnt[x]++;cnt[y]++;makeroot(x);tr[x].fa=y;
    }

    inline void cut(int x,int y){
        split(x,y);cnt[x]--;cnt[y]--;
        tr[y].ch[0]=tr[x].fa=0;
        pushup(y);
    }

    inline int ask(int x,int y){
        split(x,y);return tr[y].maxn;
    }
}LCT[K];

inline int read(){
    int s=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}
    while(isdigit(ch)) s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
    return s*f;
}

int main(){ 
    int n,m,C,T;cin>>n>>m>>C>>T;
    for(int i=1;i<=n;i++){
        int x=read();
        for(int j=0;j<C;j++) LCT[j].tr[i].val=x;
    }
    for(int i=1;i<=m;i++){
        int x=read(),y=read(),w=read();
        mp[x][y]=mp[y][x]=w;LCT[w].link(x,y);
    }
    while(T--){
        int op=read();
        if(op==0){
            int x=read(),k=read();
            for(int i=0;i<C;i++){LCT[i].tr[x].val=k;LCT[i].splay(x);} //更改对应值
        }else if(op==1){
            int x=read(),y=read(),w=read();
            if(!mp[x].count(y)){puts("No such edge.");continue;}//没有这条边
            int tmp=mp[x][y];if(tmp==w){puts("Success.");continue;}//记录之前的颜色,若一样直接 success
            if(LCT[w].cnt[x]>=2 || LCT[w].cnt[y]>=2){puts("Error 1.");continue;}//不满足条件 1
            if(LCT[w].findroot(x)==LCT[w].findroot(y)){puts("Error 2.");continue;}//不满足条件 2
            LCT[tmp].cut(x,y);LCT[w].link(x,y);mp[x][y]=mp[y][x]=w;puts("Success.");//断开旧的边,连接新的边,记录颜色
        }else{
            int tmp=read(),x=read(),y=read();
            if(LCT[tmp].findroot(x)!=LCT[tmp].findroot(y)){puts("-1");continue;}//没有路径
            cout<<LCT[tmp].ask(x,y)<<"\n";//询问答案
        }
    }
    return 0;
}

``
:::

## 强强建模

### [[NOI2014] 魔法森林](https://www.luogu.com.cn/problem/P2387)。

这题真厉害!

有两个值,要求 $\max_{a_i}+\max_{b_i}$。

同时处理两个是难做的,考虑先对 $a_i$ 排序,再处理 $b_i$。

我们将 $a_i$ 排序后,按照 $a_i$ 的大小顺序插入对应边。

这等价于固定 $max_{a_i}$ 为当前枚举到的 $a_i$。

然后就需要支持两个操作:

- 查询 $\max_{b_i}$。

- 连接的边成环后,删除当前的 $\max_{b_i}$。

显然可以使用 LCT 实现。

但是边权在点上,我们可以给每条边新建一个点,记录其两端。

这样的话,我们删除这条边的时候,只需要 $splay(id)$,然后断开父亲就好,因为其两端一定是两个端点。

:::success[代码]
```cpp
#include<bits/stdc++.h>
#define check(x) (tr[tr[x].fa].ch[0]!=x && tr[tr[x].fa].ch[1]!=x) //当前点是否为根节点
#define son(x) (tr[tr[x].fa].ch[1]==x) //当前点是父亲的哪一个儿子
using namespace std;

const int N=2e5+5;
struct edge{int x,y,numa,numb;}b[N];
struct node{int ch[2],fa,val,maxn,id,tag;}tr[N];

inline int read(){
    int s=0;char ch=getchar();
    while(!isdigit(ch)) ch=getchar();
    while(isdigit(ch)) s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
    return s;
}

inline bool operator < (edge x,edge y){return x.numa<y.numa;}

inline void pushup(int p){//更新最大值 & 编号
    tr[p].maxn=tr[p].val;tr[p].id=p;
    if(tr[p].ch[0] && tr[tr[p].ch[0]].maxn>tr[p].maxn) tr[p].maxn=tr[tr[p].ch[0]].maxn,tr[p].id=tr[tr[p].ch[0]].id;
    if(tr[p].ch[1] && tr[tr[p].ch[1]].maxn>tr[p].maxn) tr[p].maxn=tr[tr[p].ch[1]].maxn,tr[p].id=tr[tr[p].ch[1]].id;
}

inline void rotate(int x){//旋转
    int y=tr[x].fa,z=tr[y].fa;int k=son(x);
    tr[x].fa=z;if(!check(y)) tr[z].ch[son(y)]=x;//x & z
    tr[y].ch[k]=tr[x].ch[k^1];if(tr[x].ch[k^1]) tr[tr[x].ch[k^1]].fa=y;//y 与新的儿子
    tr[y].fa=x;tr[x].ch[k^1]=y;//x 与 y
    pushup(y);pushup(x);
}

inline void reverse(int x){//反转 x
    tr[x].tag^=1;
    swap(tr[x].ch[0],tr[x].ch[1]);
}

inline void pushdown(int x){//下放 x 的懒标记
    if(tr[x].tag){
        tr[x].tag=0;
        reverse(tr[x].ch[0]);
        reverse(tr[x].ch[1]);
    }
}

inline void push(int x){//下放一条链的懒标记
    if(!check(x)) push(tr[x].fa);
    pushdown(x);
}

inline void splay(int x){//将 x 转到当前链的根
    push(x);
    while(!check(x)){
        int y=tr[x].fa,z=tr[y].fa;
        if(!check(y)){
            if(son(x) xor son(y)) rotate(x);
            else rotate(y);
        }
        rotate(x);
    }
    pushup(x);
}

inline void access(int x){//打通 x 到根
    for(int son=0;x;son=x,x=tr[x].fa){
        splay(x);tr[x].ch[1]=son;pushup(x);
    }
}

inline void makeroot(int x){//把 x 变成整棵树的根
    access(x);splay(x);reverse(x);
}

inline void link(int x,int y){//连接 x -> y
    makeroot(x);tr[x].fa=y;
}

inline void split(int x,int y){//只保留 x -> y 的路径
    makeroot(x);access(y);splay(y);
}

inline int findroot(int x){//找到 x 的根节点
    access(x);splay(x);
    while(tr[x].ch[0]) x=tr[x].ch[0];
    return x;
}

int main(){
    int n,m;cin>>n>>m;
    for(int i=1;i<=m;i++) b[i]={read(),read(),read(),read()};
    sort(b+1,b+m+1);int ans=2147483647;
    for(int i=1;i<=m;i++){
        tr[i+n].val=b[i].numb;
        if(findroot(b[i].x)!=findroot(b[i].y)){link(i+n,b[i].x);link(i+n,b[i].y);} //两个点不连通,直接连接
        else{//加上这条边之后成环了,可以在当前环中删除一条最劣的边
            split(b[i].x,b[i].y);int tmp=tr[b[i].y].maxn;
            if(tmp<b[i].numb) continue;//当前的这条边更劣,不加,维持原状
            int id=tr[b[i].y].id;splay(id);tr[tr[id].ch[0]].fa=tr[tr[id].ch[1]].fa=0;//清空父亲,断边
            link(i+n,b[i].x);link(i+n,b[i].y);//连上新的边
        }
        if(findroot(1)==findroot(n)){split(1,n);ans=min(ans,tr[n].maxn+b[i].numa);}//若联通,更新答案
    }
    cout<<((ans==2147483647)?-1:ans);
    return 0;
}

:::