Splay & LCT 笔记

· · 算法·理论

Splay

节点维护的信息

```cpp int idx=0,rt=0; int son[MAXN][2],fat[MAXN]; int cnt[MAXN],siz[MAXN],val[MAXN]; #define ls(x) son[x][0] #define rs(x) son[x][1] #define dir(x) ((x)==rs(fat[x])) inline void push_up(int x){ siz[x]=siz[ls(x)]+cnt[x]+siz[rs(x)]; } ``` $dir(x)$ 表示 $x$ 是其父亲的哪个儿子。 --- ### 旋转 ![](https://oi-wiki.org/ds/images/splay-rotate.svg) 注意旋转一定要先更新 $y$ 再更新 $x$! ```cpp inline void rotate(int x){ int y=fat[x],z=fat[y]; bool c=dir(x); if(z){ son[z][dir(y)]=x; } son[y][c]=son[x][!c]; if(son[y][c]){ fat[son[y][c]]=y; } son[x][!c]=y; fat[x]=z; fat[y]=x; push_up(y); push_up(x); } ``` --- ### splay 操作 要 $splay(x)$,即将节点 $x$ 旋转至根。 设 $y=fat_x,z=fat_y$。 - $\mathrm{zig}

当 z=0 时,即 y 为根节点时,只需要 rotate(x) 即可。

inline void splay(int x){
    while(fat[x])
    {
        int y=fat[x];
        if(!fat[y]){
            rotate(x);
        }
        else{
            if(dir(x)==dir(y)){
                rotate(y);
                rotate(x);
            }
            else{
                rotate(x);
                rotate(x);
            }
        }
    }
    rt=x;
}

插入/删除/其他操作

插入 x:直接找到 x 该在树上的位置:

删除 x:splay(x),将 x 删除,合并 ls_x,rs_x 两棵子树。

若只有一棵子树,别忘了 fat_{rt}\gets 0。

合并 x,y(x<y):z 为 y 子树内最小值,splay(z),将 z 左儿子设为 x。

inline void ins(int x){
    idx++;
    int id=idx;
    int p=rt;
    while(p)
    {
        siz[p]++;
        if(x==val[p]){
            cnt[p]++;
            splay(p);
            return ;
        }
        if(x<val[p]){
            if(!ls(p)){
                break;
            }
            p=ls(p);
        }
        else{
            if(!rs(p)){
                break;
            }
            p=rs(p);
        }
    }
    fat[id]=p;
    siz[id]=1;
    cnt[id]=1;
    val[id]=x;
    if(!rt){
        rt=id;
    }
    else{
        if(val[id]<val[p]){
            ls(p)=id;
        }
        else{
            rs(p)=id;
        }
    }
    splay(id);
}
inline void del(int x){
    int p=rt;
    while(val[p]^x)
    {
        p=(x<val[p])?ls(p):rs(p);
    }
    splay(p);
    cnt[p]--;
    siz[p]--;
    if(!cnt[p]){
        int a=ls(p),b=rs(p);
        if(!a||!b){
            rt=a|b;
            fat[rt]=0;
            return ;
        }
        fat[a]=0;
        fat[b]=0;
        while(ls(b))
        {
            b=ls(b);
        }
        splay(b);
        fat[a]=b;
        ls(b)=a;
    }
}

之后的普通平衡树操作是 trival 的,只需要把最后节点 splay 即可。

代码

namespace splay{
    int idx=0,rt=0;
    int son[MAXN][2],fat[MAXN];
    int cnt[MAXN],siz[MAXN],val[MAXN];
    #define ls(x) son[x][0]
    #define rs(x) son[x][1]
    #define dir(x) ((x)==son[fat[x]][1])
    inline void push_up(int x){
        siz[x]=siz[ls(x)]+cnt[x]+siz[rs(x)];
    }
    inline void rotate(int x){
        int y=fat[x],z=fat[y];
        bool c=dir(x);
        if(z){
            son[z][dir(y)]=x;
        }
        son[y][c]=son[x][!c];
        if(son[y][c]){
            fat[son[y][c]]=y;
        }
        son[x][!c]=y;
        fat[x]=z;
        fat[y]=x;
        push_up(y);
        push_up(x);
    }
    inline void splay(int x){
        while(fat[x])
        {
            int y=fat[x];
            if(!fat[y]){
                rotate(x);
            }
            else{
                if(dir(x)==dir(y)){
                    rotate(y);
                    rotate(x);
                }
                else{
                    rotate(x);
                    rotate(x);
                }
            }
        }
        rt=x;
    }
    inline void ins(int x){
        idx++;
        int id=idx;
        int p=rt;
        while(p)
        {
            siz[p]++;
            if(x==val[p]){
                cnt[p]++;
                splay(p);
                return ;
            }
            if(x<val[p]){
                if(!ls(p)){
                    break;
                }
                p=ls(p);
            }
            else{
                if(!rs(p)){
                    break;
                }
                p=rs(p);
            }
        }
        fat[id]=p;
        siz[id]=1;
        cnt[id]=1;
        val[id]=x;
        if(!rt){
            rt=id;
        }
        else{
            if(val[id]<val[p]){
                ls(p)=id;
            }
            else{
                rs(p)=id;
            }
        }
        splay(id);
    }
    inline void del(int x){
        int p=rt;
        while(val[p]^x)
        {
            p=(x<val[p])?ls(p):rs(p);
        }
        splay(p);
        cnt[p]--;
        siz[p]--;
        if(!cnt[p]){
            int a=ls(p),b=rs(p);
            if(!a||!b){
                rt=a|b;
                fat[rt]=0;
                return ;
            }
            fat[a]=0;
            fat[b]=0;
            while(ls(b))
            {
                b=ls(b);
            }
            splay(b);
            fat[a]=b;
            ls(b)=a;
        }
    }
    inline int qry_rnk(int x){
        int res=0;
        int p=rt;
        while(true)
        {
            if(val[p]==x){
                res+=siz[ls(p)]+cnt[p];
                break;
            }
            if(x<val[p]){
                if(!ls(p)){
                    break;
                }
                p=ls(p);
            }
            else{
                res+=siz[ls(p)]+cnt[p];
                if(!rs(p)){
                    break;
                }
                p=rs(p);
            }
        }
        splay(p);
        return res;
    }
    inline int qry_kth(int k){
        int p=rt;
        while(k)
        {
            if(k<=siz[ls(p)]){
                p=ls(p);
            }
            else if(k>siz[ls(p)]+cnt[p]){
                k-=siz[ls(p)]+cnt[p];
                p=rs(p);
            }
            else{
                break;
            }
        }
        splay(p);
        return val[p];
    }
}

LCT

实链剖分

辅助树

用 Splay 维护实链,Splay 相连接形成的树形结构叫辅助树。

一些辅助树放在一起就形成了 LCT,LCT 维护的是辅助树森林。

例如现在有一棵树:

它的辅助树可以是这样(黑色边树为一棵 Splay):

接下来注意区分原树和 Splay。

Splay 节点维护的信息

```cpp int son[MAXN][2],fat[MAXN]; #define ls(x) son[x][0] #define rs(x) son[x][1] #define dir(x) ((x)==rs(fat[x])) #define isr(x) ((x)!=ls(fat[x])&&(x)!=rs(fat[x])) inline void push_up(int x){ } inline void push_tag(int x){ } inline void push_down(int x){ } ``` $dir(x)$ 表示 $x$ 是 **Splay 上**其父亲的哪个儿子。 $isr(x)$ 表示 $x$ 是否为**其所在的 Splay** 的根。 LCT 绝大部分时候都要在 Splay 上维护区间翻转标记,见后文。 --- ### update $update(x)$:从 $x$ **所在 Splay** 的根开始,沿从根到 $x$ 下放标记。 在 $splay(x)$ 之前用,保证交换左右儿子的标记下放。 ```cpp inline void update(int x){ if(!isr(x)){ update(fat[x]); } push_down(x); } ``` --- ### 旋转和 splay 操作 和 Splay 一样,判根用 $isr$,$splay$ 之前 $update$。 注意修改 $z$ 的儿子前先判 $isr(y)$! ```cpp inline void rotate(int x){ int y=fat[x],z=fat[y]; bool c=dir(x); if(z&&!isr(y)){ son[z][dir(y)]=x; } son[y][c]=son[x][!c]; if(son[y][c]){ fat[son[y][c]]=y; } son[x][!c]=y; fat[x]=z; fat[y]=x; push_up(y); push_up(x); } inline void splay(int x){ update(x); while(!isr(x)) { int y=fat[x]; if(isr(y)){ rotate(x); } else{ if(dir(x)==dir(y)){ rotate(y); rotate(x); } else{ rotate(x); rotate(x); } } } } ``` --- ### access LCT 最重要最基础的操作,$access(x)$ 能把**原树上** $x$ 到根变成一条实链,也就是整到一棵 Splay 里。 而且这棵 Splay 将只含有**原树上**根到 $x$ 的点。 对于这样一颗**辅助树**,执行 $access(N)$: ![](https://oi-wiki.org/ds/images/lct-access-2.svg) 过程: ![](https://oi-wiki.org/ds/images/lct-access-4.svg) ![](https://oi-wiki.org/ds/images/lct-access-5.svg) ![](https://oi-wiki.org/ds/images/lct-access-6.svg) - 先 $splay(N)$,将 $N$ 旋到**其所在 Splay** 的根。 - 断开**原树上** $N$ 到实儿子的实边,$rs_N\gets 0$(中序遍历是实链,右子树就是 $N$ 下半部分)。 - $N$ **Splay 上**的父亲为 $I$,$splay(I)$。 - 断开**原树上** $I$ 到实儿子的实边,将 $N$ 设为 $I$ 实儿子,$rs_I\gets N$。 - 同理,$splay(A)$,$rs_A\gets I$。 然后就完成了。 ![](https://oi-wiki.org/ds/images/lct-access-7.svg) 整理(**一切操作都在 Splay 上,与原树无关**): - 将当前节点 $splay$。 - 将它的右儿子设为上次操作的节点,更新维护的信息。 - 跳到它的父亲,重复。 ```cpp inline void access(int x){ int y=0; while(x) { splay(x); rs(x)=y; push_up(x); y=x; x=fat[x]; } } ``` 可以返回最后 **Splay 的**根。 ```cpp inline int access(int x){ int y=0; while(x) { splay(x); rs(x)=y; push_up(x); y=x; x=fat[x]; } return y; } ``` 这样 $access(x)$,再 $access(y)$ 就会返回**原树的** $\operatorname{lca}(x,y)$。 --- ### makeroot 另一个 LCT 的重要函数。 维护路径信息时,有时 $path(x,y)$ 不满足 $\operatorname{lca}(x,y)\ne x/y$,怎么 $access$ 都提不出 $path(x,y)$。 因为 $x,y$ 不可能属于同一条实链,所以 $path(x,y)$ 无法出现在同一棵 Splay 里。 但是要想维护它的信息,怎么办呢? 直接把**原树**的根看成 $x$ 就行了,也就是**给原树换根**。 Splay 里的点是按中序遍历从上到下存的,设原来的**原树**根是 $rt$,换成 $x$ 产生的影响: - $access(x)$,提出 $x$ 到 $rt$,并 $splay(x)$。 - 发现 $x$ 到 $rt$ 的这条实链上下翻转了,也就是 Splay 的中序遍历翻转,打翻转标记即可。 ```cpp inline void makeroot(int x){ access(x); splay(x); push_tag(x); } ``` --- ### find $find(x)$:找到 $x$ 所在**原树上**的根。 $access(x)$ 然后 $splay(x)$,不停往其左儿子走即可(中序遍历第一个点)。最后记得 $splay$。 ```cpp inline int find(int x){ access(x); splay(x); int p=x; while(ls(p)) { p=ls(p); push_down(p); } splay(p); return p; } ``` --- ### split $split(x,y)$:将**原树上** $x$ 到 $y$ 的路径提取。 $makeroot(x)$,$access(y)$。 ```cpp inline void split(int x,int y){ makeroot(x); access(y); } ``` --- ### link $link(x,y)$:在**原树上**连接 $x$ 和 $y$。 - 先 $makeroot(x)$。 - 判断操作是否合法,判定 $find(y)\ne x$。 - $fat_x\gets y$,将 $x$ 作为 $y$ 的虚儿子。 ```cpp inline void link(int x,int y){ makeroot(x); if(find(y)^x){ fat[x]=y; } } ``` --- ### cut $link(x,y)$:在**原树上**断开 $x$ 和 $y$ 之间的边。 - 先 $makeroot(x)$。 - 判断操作是否合法,$access(y)$,$splay(x)$,判定 $rs_x=y$。 - $fat_y\gets 0$,$rs_x\gets 0$,断开连接。 ```cpp inline void cut(int x,int y){ makeroot(x); access(y); splay(x); if(rs(x)==y){ fat[y]=0; rs(x)=0; } //push_up(x); } ``` 这里可以不更新维护的信息,因为 $access$ 和 $rotate$ 里面都有。 --- ### 代码 ```cpp namespace LCT{ int son[MAXN][2],fat[MAXN]; #define ls(x) son[x][0] #define rs(x) son[x][1] #define dir(x) ((x)==rs(fat[x])) #define isr(x) ((x)!=ls(fat[x])&&(x)!=rs(fat[x])) bool tag[MAXN]; inline void push_up(int x){ } inline void push_tag(int x){ swap(ls(x),rs(x)); tag[x]=!tag[x]; } inline void push_down(int x){ if(tag[x]){ if(ls(x)){ push_tag(ls(x)); } if(rs(x)){ push_tag(rs(x)); } tag[x]=false; } } inline void update(int x){ if(!isr(x)){ update(fat[x]); } push_down(x); } inline void rotate(int x){ int y=fat[x],z=fat[y]; bool c=dir(x); if(z&&!isr(y)){ son[z][dir(y)]=x; } son[y][c]=son[x][!c]; if(son[y][c]){ fat[son[y][c]]=y; } son[x][!c]=y; fat[x]=z; fat[y]=x; push_up(y); push_up(x); } inline void splay(int x){ update(x); while(!isr(x)) { int y=fat[x]; if(isr(y)){ rotate(x); } else{ if(dir(x)==dir(y)){ rotate(y); rotate(x); } else{ rotate(x); rotate(x); } } } } inline void access(int x){ int y=0; while(x) { splay(x); rs(x)=y; push_up(x); y=x; x=fat[x]; } } inline void makeroot(int x){ access(x); splay(x); push_tag(x); } inline int find(int x){ access(x); splay(x); int p=x; while(ls(p)) { push_down(p); p=ls(p); } splay(p); return p; } inline void split(int x,int y){ makeroot(x); access(y); } inline void link(int x,int y){ makeroot(x); if(find(y)^x){ fat[x]=y; } } inline void cut(int x,int y){ makeroot(x); access(y); splay(x); if(rs(x)==y){ fat[y]=0; rs(x)=0; } push_up(x); } } ``` --- ### 时间复杂度 和 Splay 一样,均摊 $O(n\log n+m\log n)$,常数巨大。 --- ## LCT 习题 未标明“树”或“图”,则初始为 $n$ 个单点。 --- ### [P3690 【模板】动态树(LCT)](https://www.luogu.com.cn/problem/P3690) > 板题。 > > 带点权。操作:单点修改、连边、删边、查询路径点权 $\operatorname{xor}$ 和。 > > $n\le 10^5$,$m\le 3\times 10^5$。 TLE 原因:$update$ 中 $isr(x)$ 写成 $!x$。 [Code](https://www.luogu.com.cn/record/222883033) --- ### [P2147 [SDOI2008] 洞穴勘测](https://www.luogu.com.cn/problem/P2147) > 比模板更模板。 > > 操作:连边、删边、查询连通性。 > > $n\le 10^4$,$m\le 2\times 10^5$。 [Code](https://www.luogu.com.cn/record/223084560) [弱化版(双倍经验)P3950](https://www.luogu.com.cn/problem/P3950) [双倍经验SP9577](https://www.luogu.com.cn/problem/SP9577) --- ### [SP8791 DYNALCA](https://www.luogu.com.cn/problem/SP8791) > 操作:连边、删边、查询 $\mathrm{lca}$。 > > $n\le 10^4$,$m\le 2\times 10^5$。 用 $access$ 里面的方法求 $\mathrm{lca}$ 即可。 [Code](https://www.luogu.com.cn/record/223488955) --- ### [P1505 [国家集训队] 旅游](https://www.luogu.com.cn/problem/P1505) > 树,带边权。操作:修改边权、路径边权取负、查询路径边权和/最大值/最小值。 > > $n,m\le 2\times 10^5$。 树剖题,用 LCT 做。 维护边权不好做,考虑转点权。将原来的树边 $(i,j)$ 建虚点 $e$,建边 $(i,e),(j,e)$。 之后 LCT 的边权题几乎都要转点权。 注意:只有虚点有点权,实点 `push_up` 时要先将 `mnv,mxv` 分别设为 `INF,-INF`,而非 $val_x$。 记得空间开两倍。 [Code](https://www.luogu.com.cn/record/222883033) --- ### [P4312 [COI 2009] OTOCI](https://www.luogu.com.cn/problem/P4312) > 带点权。操作:连边、单点修改、查询路径点权和。 > > $n\le 3\times 10^4$,$m\le 3\times 10^5$。 离线树剖,在线 LCT。 [Code](https://www.luogu.com.cn/record/223077684) [双倍经验SP4155](https://www.luogu.com.cn/problem/SP4155) --- ### [P1501 [国家集训队] Tree II](https://www.luogu.com.cn/problem/P1501) > 树,带点权。操作:连删边、路径加、路径乘、查询路径和。(模 $51061$) > > $n,m\le 10^5$。 按照 [线段树 2](https://www.luogu.com.cn/problem/P3373) 的方式打 tag 即可。记得维护 $siz$,打加法 tag 更新 $sum$ 时要用。 $51061^2$ 会爆 `int`。 [Code](https://www.luogu.com.cn/record/223083250) --- ### [P3203 [HNOI2010] 弹飞绵羊](https://www.luogu.com.cn/problem/P3203) > 有长为 $n$ 的序列 $k$,$k_i$ 表示能从 $i$ 跳到 $i+k_i$。 > > $m$ 次操作:单点修改 $k$、查询 $i$ 跳几次 $>n$。 > > $n\le 2\times 10^5$,$m\le 10^5$。 $i\to i+k_i$ 连边,$>n$ 的建虚点 $n+1$。 查询 $i\sim n+1$ 的路径的节点数。 [Code](https://www.luogu.com.cn/record/223292291) --- ### [P4219 [BJOI2014] 大融合](https://www.luogu.com.cn/problem/P4219) > 操作:连边、查询包含边 $(x,y)$ 的简单路径数量。 > > $n,m\le 10^5$。 发现需要维护**原树**的 $siz$。 维护一个点虚儿子的答案和即可,在 $access,link,cut$ 修改虚儿子时更新。 注意 $link$ 要 $makeroot(x)$ 也要 $makeroot(y)$。 LCT 用这种方法维护子树信息,要求信息有**可差分性**。 [Code](https://www.luogu.com.cn/record/223301358) --- ### [P4299 首都](https://www.luogu.com.cn/problem/P4299) > 操作:连边、查询 $x$ 所处树的重心(多重心去编号最小)。 > > $n\le 10^5$,$m\le 2\times 10^5$。 结论:连接两个连通块,新的重心在原来两个重心的路径上。 于是把这条路径提出来在 Splay 上二分即可。 同样要维护虚儿子 $siz$ 和。 [Code](https://www.luogu.com.cn/record/249680790) --- ## LCT 维护生成树 只能支持动态加边,不能删边。 下面以最小生成树为例: 考虑加一条边 $(x,y)$ 对 MST 的影响: - $x,y$ 原来不连通,$(x,y)$ 加入 MST。 - $x,y$ 原来连通。令 $(u,v)$ 为原来 MST 上 $x$ 到 $y$ 路径上权值最大的边。 - $w(x,y)<w(u,v)$,MST 中加入 $(x,y)$,删除 $(u,v)$。 - $w(x,y)\ge w(u,v)$,$(x,y)$ 没用,MST 不变。 当然你要删边也不是不行,套个线段树分治,多一只 $\log$ 罢了。(目测双 $\log$ 过不了 $10^5$) --- ### [P4172 [WC2006] 水管局长](https://www.luogu.com.cn/problem/P4172) > 图,带边权 $w$,一条路径的长度为所有边权的最大值。 > > 操作:删边、求 $u\to v$ 的最短路。 > > $n\le 10^3$,$m,q\le 10^5$。 这题原本保证删边次数最多 $5000$ 次,是暴力题。 $u\to v$ 的最短路一定是此时 MST 上 $u,v$ 间的路径。 离线反向操作,变成加边。 [Code](https://www.luogu.com.cn/record/223379368) --- ### [P4234 最小差值生成树](https://www.luogu.com.cn/problem/P4234) > 图,带边权 $w$,生成树的权值是边的 $\max w-\min w$,求最小生成树。 > > $n\le 5\times 10^4$,$m\le 2\times 10^5$。 将所有边按照 $w$ 排序,依次加边,当出现环时删除环上 $w$ 最小的边即可。 注意判自环。 [Code](https://www.luogu.com.cn/record/223363026) [推销整体二分解法](https://www.luogu.com.cn/article/juk56gb5) --- ### [P2387 [NOI2014] 魔法森林](https://www.luogu.com.cn/problem/P2387) > 图,带二元组边权 $(x,y)$。路径权值是边的 $\max x+\max y$,求 $1\to n$ 的最短路。 > > $n\le 5\times 10^4$,$m\le 10^5$。 将边按照 $x$ 排序,依次加边,维护 $y$ 的 MST 就行了。 [Code](https://www.luogu.com.cn/record/223427468) --- ### [P5385 [Cnoi2019] 须臾幻境](https://www.luogu.com.cn/problem/P5385) > 图,边有编号 $1\sim m$。$q$ 次询问:只保留 $[l,r]$ 的边时,图的连通块数量。**强制在线**。 > > $n,q\le 10^5$,$m\le 2\times 10^5$。 **图的连通块数等于点数减去生成森林的边数。** 将边 $i$ 的权值设为 $i$,依次加入 $1\sim i$ 维护最大生成树。 加入第 $i$ 条边,若成环,删去了环上最小的边,记删去的边为 $a_i$,若不成环则 $a_i=0$。 只保留 $[l,r]$ 的边时,一个生成森林即为只保留 $a_i<l$ 的边。 所以答案就是 $\displaystyle n-\sum_{i=l}^r[a_i<l]$。在线,主席树即可。 注意判自环。 [Code](https://www.luogu.com.cn/record/223456314) --- ### [SP9576 DYNACON2](https://www.luogu.com.cn/problem/SP9576) > 操作:连边、删边、查询连通性。但是不保证连边不成环。(人话:动态图连通性) > > $n,m\le 10^5$。 [动态图连通性](https://www.luogu.com.cn/problem/P5247)?并非,这题可离线。 删边采取懒删除,记录边的删除时间 $t$,连通性变为查询 $\min t$ 最大的路径。 维护最大生成树即可。 注意查询时先判是否再最大生成森林上连通。 [Code](https://www.luogu.com.cn/record/223473864) --- ### [P4319 变化的道路](https://www.luogu.com.cn/problem/P4319) > 操作:加边、删边、求最小生成树边权和。 > > $n\le 5\times 10^4$,$m\le 1.5\times 10^5$。时限 $7s$。 线段树分治。$O(n\log n\log V)$(认为 $n,m$ 同阶)常数看着办。