• 【数据结构】LinkCutTree


    https://oi-wiki.org/ds/lct/
    https://www.luogu.com.cn/problem/P1501

    和重链剖分的概念有点点像。或者应该叫做实链剖分。

    LCT维护的“原树”并非是一棵树,而是一片森林。森林中的树可以切成各种形状的树,也可以把不同的树合并。LCT内部的“辅助树”也不是一棵树,而是一片(更大的)Splay森林。

    原树是一棵有根树。支持以下操作:
    1、对原树节点x和节点y的链更新某信息
    2、对原树节点x和节点y的链查询某信息
    3、重新指定原树的根节点
    4、删除原树的边x-y
    5、添加原树的边x-y,其中y是深度更小的节点

    LCT维护一片Splay森林,其中每一棵Splay代表原树的一条深度单调递增/单调递减的链(也就是说不拐弯的链)。每一棵Splay的中序遍历是原树的链顶到链底。所以每个节点的实儿子是唯一的,父亲也是唯一的。

    实儿子:原树中,当前节点的儿子,且当前节点处于同一棵Splay中,当前节点知道它的实儿子是谁,实儿子也知道其父亲是谁。
    虚儿子:原树中,当前节点的儿子,且当前节点不处于同一棵Splay中,当前节点并不知道它的虚儿子有哪些,但是虚儿子知道其父亲是谁。

    struct LinkCutTree {
    
        static const int MAXN = 1e5 + 10;
    
        int ch[MAXN][2], pa[MAXN];
        int mul[MAXN], add[MAXN], rev[MAXN];
        int val[MAXN], siz[MAXN], sum[MAXN];
    
        inline void Init(int n) {
            for(int i = 1; i <= n; ++i) {
                ch[i][0] = 0, ch[i][1] = 0, pa[i] = 0;
                mul[i] = 1, add[i] = 0, rev[i] = 0;
                val[i] = 1, siz[i] = 1, sum[i] = val[i];
            }
        }
    
        inline void PushUp(int x) {
            int t = 0;
            siz[x] = 1;
            sum[x] = val[x];
            if(t = ch[x][0]) {
                siz[x] += siz[t];
                sum[x] = (sum[x] + sum[t]) % MOD;
            }
            if(t = ch[x][1]) {
                siz[x] += siz[t];
                sum[x] = (sum[x] + sum[t]) % MOD;
            }
        }
    
        inline void PushDown(int x) {
            int t = 0;
            if(mul[x] != 1) {
                if(t = ch[x][0]) {
                    mul[t] = (1LL * mul[t] * mul[x]) % MOD;
                    add[t] = (1LL * add[t] * mul[x]) % MOD;
                    val[t] = (1LL * val[t] * mul[x]) % MOD;
                    sum[t] = (1LL * sum[t] * mul[x]) % MOD;
                }
                if(t = ch[x][1]) {
                    mul[t] = (1LL * mul[t] * mul[x]) % MOD;
                    add[t] = (1LL * add[t] * mul[x]) % MOD;
                    val[t] = (1LL * val[t] * mul[x]) % MOD;
                    sum[t] = (1LL * sum[t] * mul[x]) % MOD;
                }
                mul[x] = 1;
            }
            if(add[x]) {
                if(t = ch[x][0]) {
                    add[t] = (add[t] + add[x]) % MOD;
                    val[t] = (val[t] + add[x]) % MOD;
                    sum[t] = (sum[t] + 1LL * siz[t] * add[x]) % MOD;
                }
                if(t = ch[x][1]) {
                    add[t] = (add[t] + add[x]) % MOD;
                    val[t] = (val[t] + add[x]) % MOD;
                    sum[t] = (sum[t] + 1LL * siz[t] * add[x]) % MOD;
                }
                add[x] = 0;
            }
            if(rev[x]) {
                if(t = ch[x][0]) {
                    swap(ch[t][0], ch[t][1]);
                    rev[t] ^= 1;
                }
                if(t = ch[x][1]) {
                    swap(ch[t][0], ch[t][1]);
                    rev[t] ^= 1;
                }
                rev[x] = 0;
            }
        }
    
        inline bool Which(int x) {
            return pa[x] && ch[pa[x]][1] == x;
        }
    
        inline bool NotRoot(int x) {
            return pa[x] && (ch[pa[x]][0] == x || ch[pa[x]][1] == x);
        }
    
        void Rotate(int x) {
            int y = pa[x], z = pa[y], k = Which(x);
            if(NotRoot(y))
                ch[z][Which(y)] = x;
            pa[x] = z;
            ch[y][k] = ch[x][!k];
            pa[ch[x][!k]] = y;
            ch[x][!k] = y;
            pa[y] = x;
            PushUp(y);
            PushUp(x);
        }
    
        void PushDownAll(int x) {
            if(NotRoot(x))
                PushDownAll(pa[x]);
            PushDown(x);
        }
    
        int Splay(int x) {
            PushDownAll(x);
            while(NotRoot(x)) {
                int y = pa[x];
                if(NotRoot(y)) {
                    if(Which(x) == Which(y))
                        Rotate(y);
                    else
                        Rotate(x);
                }
                Rotate(x);
            }
            return x;
        }
    
        int Access(int x) {
            int y = 0;
            while(x) {
                Splay(x);
                ch[x][1] = y;
                PushUp(x);
                y = x;
                x = pa[x];
            }
            return y;
        }
    
        int MakeRoot(int x) {
            Access(x);
            Splay(x);
            swap(ch[x][0], ch[x][1]);
            rev[x] ^= 1;
            return x;
        }
    
        int FindRoot(int x) {
            Access(x);
            Splay(x);
            while(ch[x][0]) {
                PushDown(x);
                x = ch[x][0];
            }
            Splay(x);
            return x;
        }
    
        int Select(int x, int y) {
            MakeRoot(x);
            Access(y);
            Splay(y);
            return y;
        }
    
        void Cut(int x, int y) {
            Select(x, y);
            ch[y][0] = 0;
            pa[x] = 0;
        }
    
        bool Cut2(int x, int y) {
            if(FindRoot(x) == FindRoot(y)) {
                Select(x, y);
                if(ch[y][0] == x && ch[x][1] == 0) {
                    ch[y][0] = 0;
                    pa[x] = 0;
                    return true;
                }
            }
            return false;
        }
    
        void Link(int x, int y) {
            MakeRoot(x);
            pa[x] = y;
        }
    
        bool Link2(int x, int y) {
            if(FindRoot(x) != FindRoot(y)) {
                Link(x, y);
                return true;
            }
            return false;
        }
    
        void Mul(int x, int y, int v) {
            Select(x, y);
            mul[y] = (1LL * v * mul[y]) % MOD;
            add[y] = (1LL * add[y] * v) % MOD;
            val[y] = (1LL * val[y] * v) % MOD;
            sum[y] = (1LL * sum[y] * v) % MOD;
        }
    
        void Add(int x, int y, int v) {
            Select(x, y);
            add[y] = (add[y] + v) % MOD;
            val[y] = (val[y] + v) % MOD;
            sum[y] = (sum[y] + 1LL * v * siz[y]) % MOD;
        }
    
        ll Sum(int x, int y) {
            Select(x, y);
            return sum[y];
        }
    
    } lct;
    

    Access:把x到原树的根节点的路径变为实路径。
    额外提供一个返回值,表示最后一次虚边变实边时,虚边父亲的编号。连续两次Access操作时,第二次Access操作的返回值就是这两个节点的LCA。返回值表示x到根节点所在的链代表的Splay的树根,这个节点已经被旋转到Splay的根,并且其父亲一定为空。

    MakeRoot:指定原树的根节点为x(换根)
    FindRoot:找x在辅助树的树根,也就是x在原树的实链的链顶
    Select:把x和y之间的路径切成一个Splay,根是y。(想要根是谁,最后就Splay一下谁就行)

    Cut:切断x和y的边
    Cut2:假如x和y之间有边,则切断x和y的边
    Link:连接x和y的边
    Link2:假如x和y之间不连通,则连接x和y的边
    Update:更新点x的权值为v

    struct LinkCutTree {
    
        static const int MAXN = 1e5 + 10;
    
        int ch[MAXN][2], pa[MAXN];
        int mul[MAXN], add[MAXN], rev[MAXN];
        int val[MAXN], siz[MAXN], sum[MAXN];
    
        inline void Init(int n) {
            for(int i = 1; i <= n; ++i) {
                ch[i][0] = 0, ch[i][1] = 0, pa[i] = 0;
                mul[i] = 1, add[i] = 0, rev[i] = 0;
                rd(val[i]);
                siz[i] = 1, sum[i] = val[i];
            }
        }
    
        inline void PushUp(int x) {
            int t = 0;
            siz[x] = 1;
            sum[x] = val[x];
            if(t = ch[x][0]) {
                siz[x] += siz[t];
                sum[x] = (sum[x] + sum[t]) % MOD;
            }
            if(t = ch[x][1]) {
                siz[x] += siz[t];
                sum[x] = (sum[x] + sum[t]) % MOD;
            }
        }
    
        inline void PushDown(int x) {
            int t = 0;
            if(mul[x] != 1) {
                if(t = ch[x][0]) {
                    mul[t] = (1LL * mul[t] * mul[x]) % MOD;
                    add[t] = (1LL * add[t] * mul[x]) % MOD;
                    val[t] = (1LL * val[t] * mul[x]) % MOD;
                    sum[t] = (1LL * sum[t] * mul[x]) % MOD;
                }
                if(t = ch[x][1]) {
                    mul[t] = (1LL * mul[t] * mul[x]) % MOD;
                    add[t] = (1LL * add[t] * mul[x]) % MOD;
                    val[t] = (1LL * val[t] * mul[x]) % MOD;
                    sum[t] = (1LL * sum[t] * mul[x]) % MOD;
                }
                mul[x] = 1;
            }
            if(add[x]) {
                if(t = ch[x][0]) {
                    add[t] = (add[t] + add[x]) % MOD;
                    val[t] = (val[t] + add[x]) % MOD;
                    sum[t] = (sum[t] + 1LL * siz[t] * add[x]) % MOD;
                }
                if(t = ch[x][1]) {
                    add[t] = (add[t] + add[x]) % MOD;
                    val[t] = (val[t] + add[x]) % MOD;
                    sum[t] = (sum[t] + 1LL * siz[t] * add[x]) % MOD;
                }
                add[x] = 0;
            }
            if(rev[x]) {
                if(t = ch[x][0]) {
                    swap(ch[t][0], ch[t][1]);
                    rev[t] ^= 1;
                }
                if(t = ch[x][1]) {
                    swap(ch[t][0], ch[t][1]);
                    rev[t] ^= 1;
                }
                rev[x] = 0;
            }
        }
    
        inline bool Which(int x) {
            return pa[x] && ch[pa[x]][1] == x;
        }
    
        inline bool NotRoot(int x) {
            return pa[x] && (ch[pa[x]][0] == x || ch[pa[x]][1] == x);
        }
    
        void Rotate(int x) {
            int y = pa[x], z = pa[y], k = Which(x);
            if(NotRoot(y))
                ch[z][Which(y)] = x;
            pa[x] = z;
            ch[y][k] = ch[x][!k];
            pa[ch[x][!k]] = y;
            ch[x][!k] = y;
            pa[y] = x;
            PushUp(y);
            PushUp(x);
        }
    
        void PushDownAll(int x) {
            if(NotRoot(x))
                PushDownAll(pa[x]);
            PushDown(x);
        }
    
        int Splay(int x) {
            PushDownAll(x);
            while(NotRoot(x)) {
                int y = pa[x];
                if(NotRoot(y)) {
                    if(Which(x) == Which(y))
                        Rotate(y);
                    else
                        Rotate(x);
                }
                Rotate(x);
            }
            return x;
        }
    
        int Access(int x) {
            int y = 0;
            while(x) {
                Splay(x);
                ch[x][1] = y;
                PushUp(x);
                y = x;
                x = pa[x];
            }
            return y;
        }
    
        int MakeRoot(int x) {
            Access(x);
            Splay(x);
            swap(ch[x][0], ch[x][1]);
            rev[x] ^= 1;
            return x;
        }
    
        int FindRoot(int x) {
            Access(x);
            Splay(x);
            while(ch[x][0]) {
                PushDown(x);
                x = ch[x][0];
            }
            Splay(x);
            return x;
        }
    
        int Select(int x, int y) {
            MakeRoot(x);
            Access(y);
            Splay(y);
            return y;
        }
    
        void Cut(int x, int y) {
            Select(x, y);
            ch[y][0] = 0;
            pa[x] = 0;
        }
    
        bool Cut2(int x, int y) {
            if(FindRoot(x) == FindRoot(y)) {
                Select(x, y);
                if(ch[y][0] == x && ch[x][1] == 0) {
                    ch[y][0] = 0;
                    pa[x] = 0;
                    return true;
                }
            }
            return false;
        }
    
        void Link(int x, int y) {
            MakeRoot(x);
            pa[x] = y;
        }
    
        bool Link2(int x, int y) {
            if(FindRoot(x) != FindRoot(y)) {
                Link(x, y);
                return true;
            }
            return false;
        }
    
        void Mul(int x, int y, int v) {
            Select(x, y);
            mul[y] = (1LL * v * mul[y]) % MOD;
            add[y] = (1LL * add[y] * v) % MOD;
            val[y] = (1LL * val[y] * v) % MOD;
            sum[y] = (1LL * sum[y] * v) % MOD;
        }
    
        void Add(int x, int y, int v) {
            Select(x, y);
            add[y] = (add[y] + v) % MOD;
            val[y] = (val[y] + v) % MOD;
            sum[y] = (sum[y] + 1LL * v * siz[y]) % MOD;
        }
    
        ll Sum(int x, int y) {
            Select(x, y);
            return sum[y];
        }
    
    } lct;
    

    维护边的信息

    由于LCT中根是会变的,所以不能直接把信息存在点上,要对每条边进行拆边。

    维护子树的信息

    需要额外统计虚儿子的贡献,由于在Splay中看不到原树的虚儿子,所以LCT对此的维护比较麻烦。
    并且要求具备逆元,可以说LCT并不擅长维护子树的信息。

  • 相关阅读:
    Coursera Algorithms week3 快速排序 练习测验: Nuts and bolts
    快速排序及三向切分快排——java实现
    自顶向下(递归)的归并排序和自底向上(循环)的归并排序——java实现
    希尔shell排序——java实现
    插入排序——java实现
    选择排序——java实现
    Coursera Algorithms week3 归并排序 练习测验: Shuffling a linked list
    单向链表的归并排序——java实现
    Coursera Algorithms week3 归并排序 练习测验: Counting inversions
    Coursera Algorithms week2 栈和队列 练习测验: Stack with max
  • 原文地址:https://www.cnblogs.com/purinliang/p/14403506.html
Copyright © 2020-2023  润新知