CF916E Jamie and Tree 题解

· · 题解

LCT 能把任何优雅的题目变成暴力。 ——某题解区

所有能用树剖做的题 LCT 都能做。 ——忘了是谁说的了

题目

给出一棵 n 个结点的树,每个结点有权值。共 q 次操作,操作有以下三种:

(1\le n,q\le10^5,|x|\le 10^8)

题解

看到 tag 里面有树剖果断选择写 LCT。

在说本题的核心操作——子树加——之前,我们先来看一个简单的 trick,用 LCT 求两点的 \rm lca。这个比较简单,两次 access 操作就好,如下:

inline int access(int x)
{
    int y = 0;
    while (x) { splay(x); Rs(x) = y; pushup(x); x = Fa(y = x); }
    return y;
}
int lca = (access(u), access(v));

原因也很显然,我们拉完 u 到根的实链后再拉 v 的。则最后一个需要调整的实链顶对应的结点自然是 \mathrm{lca}(u,v)。

好了,接下来进入重头戏,如何用 LCT 去维护子树加。因为维护子树和我们要单独维护一下结点对应虚子树的信息,所以维护子树加其实也是要对应去修改这个虚子树的信息。但不可能直接对虚子树内的每个结点一一修改信息,也不能通过下传标记的方法(因为它们根本就不在一个 \rm splay 里)去修改,所以我们要考虑新的方法。

注意到虚子树的一次修改对里面每个结点都是相同的且可以保存在根节点里,所以我们可以对每个结点开一个 dlt 表示当前结点虚子树已经加了多少值了。这样当结点的虚实发生改变时再根据 dlt 进行修改就好了,其实跟下传标记的思想是类似的。

来看具体代码实现,首先是我们需要维护什么。注意到下放 dlt 其实是针对对应结点的所有子树的(包括虚子树和实子树),所以要维护一个 dtag 表示虚子树的下传标记,stag 表示实子树的下传标记,然后每次虚实变化的时候对应修改就好。为了维护 dtag 和 stag 下传时对结点总和 sum 造成的影响,我们需要虚子树大小之和 szt 和实子树大小之和 szc,而为了维护它们,我们又需要每个点的虚子树大小 szl 和每个点的子树(原树上的)大小 sz。其余要维护的就没有什么好说的了,都是一些比较常规的操作。

然后是一些比较关键的函数,首先是下传 dtag 和 stag 的 addD,addS。

void addS(int x, ll v) 
{  
    if (!x) return ;
    node[x].stag += v; node[x].w += v;
    node[x].sum += v * node[x].szc;
}
void addD(int x, ll v)
{
    if (!x) return ;
    node[x].dtag += v; node[x].dlt += v;
    node[x].sum += v * node[x].szt;
}

需要注意的点是,对于实子树的下放,要算上当前结点,对于虚子树的下放不要算上当前结点。以及对于 sum 的更新用的 sz 值到底是哪个。

然后是维护虚实变化的 ins,del。

void ins(int x, int y) 
{ 
    if (!y) return ;
    addS(y, -node[x].dlt); addD(y, -node[x].dlt);
    node[x].szl += node[y].sz; node[x].suml += node[y].sum;
}
void del(int x, int y)
{
    if (!y) return ;
    node[x].szl -= node[y].sz; node[x].suml -= node[y].sum;
    addS(y, node[x].dlt); addD(y, node[x].dlt);
}

需要注意的点是,加入虚子树时应该先去掉 dlt 再加入,而从虚子树删去时应该先删去再撤销 dlt 的影响。

还有一点,本题中为了方便,不再维护实子树上的信息,处理询问时这样处理:

ll ask(int rt, int x) { return split(rt, x), (node[x].w + node[x].suml + node[x].szl * node[x].dlt); }

直接把 x 和 rt 拉到一个实链里,这样 x 的子树就全是虚子树了,直接回答虚子树的信息就好。

完整代码及后续

#include <cstdio>
#include <algorithm>
#define Ls(x) (node[x].ch[0])
#define Rs(x) (node[x].ch[1])
#define Fa(x) (node[x].fa)
const int N = 1e5 + 10; typedef long long ll;
int st[N], tp;
struct LCT
{
    struct Splay
    {
        //sz 表示这个点在树上的子树大小,szl 表示这个点的虚子树大小
        //szc表示这个点的所在实子树大小,szt 表示这个点所在实子树内所有点的虚子树大小之和
        //sum 表示这个点的树上的子树权值和,suml 表示这个点虚子树子树权值和
        //dlt 表示这个点子树已经加了多少值了
        //dtag 表示要下放到虚子树的标记,stag 表示要下放到实子树的标记
        int fa, ch[2], revFlag; ll w;
        int sz, szl, szc, szt; ll suml, sum, dlt, dtag, stag;
    }node[N]; 
    void pushup(int x) 
    {  
        node[x].sz = node[Ls(x)].sz + node[Rs(x)].sz + node[x].szl + 1;
        node[x].szc = node[Ls(x)].szc + node[Rs(x)].szc + 1;
        node[x].szt = node[Ls(x)].szt + node[Rs(x)].szt + node[x].szl;
        node[x].sum = node[Ls(x)].sum + node[Rs(x)].sum + node[x].w + node[x].suml + node[x].szl * node[x].dlt;
    }
    void reverse(int x) { if (!x) return ; std::swap(Ls(x), Rs(x)); node[x].revFlag ^= 1; }
    void addS(int x, ll v) 
    {  
        if (!x) return ;
        node[x].stag += v; node[x].w += v;
        node[x].sum += v * node[x].szc;
    }
    void addD(int x, ll v)
    {
        if (!x) return ;
        node[x].dtag += v; node[x].dlt += v;
        node[x].sum += v * node[x].szt;
    }
    void pushdown(int x)
    {
        if (node[x].revFlag)
        {
            reverse(Ls(x)); reverse(Rs(x));
            node[x].revFlag = 0;
        }
        if (node[x].stag)
        {
            addS(Ls(x), node[x].stag); addS(Rs(x), node[x].stag);
            node[x].stag = 0;
        }
        if (node[x].dtag)
        {
            addD(Ls(x), node[x].dtag); addD(Rs(x), node[x].dtag);
            node[x].dtag = 0;
        }
    }
    int get(int x) { return Rs(Fa(x)) == x; }
    int nRoot(int x) { return Rs(Fa(x)) == x || Ls(Fa(x)) == x; }
    void rotate(int x)
    {
        int fa = Fa(x), gf = Fa(fa), d = get(x), dd = get(fa);
        if (nRoot(fa)) node[gf].ch[dd] = x;
        node[fa].ch[d] = node[x].ch[d ^ 1]; Fa(node[x].ch[d ^ 1]) = fa;
        node[x].ch[d ^ 1] = fa; Fa(fa) = x; Fa(x) = gf;
        pushup(fa); pushup(x);
    }
    void splay(int x)
    {
        int y = st[tp = 1] = x;
        while (nRoot(y)) st[++tp] = y = Fa(y);
        while (tp) pushdown(st[tp--]);
        for (; nRoot(x); rotate(x))
            if (nRoot(Fa(x))) rotate(get(x) == get(Fa(x)) ? Fa(x) : x);
        pushup(x);
    }
    void ins(int x, int y) 
    { 
        if (!y) return ;
        addS(y, -node[x].dlt); addD(y, -node[x].dlt);
        node[x].szl += node[y].sz; node[x].suml += node[y].sum;
    }
    void del(int x, int y)
    {
        if (!y) return ;
        node[x].szl -= node[y].sz; node[x].suml -= node[y].sum;
        addS(y, node[x].dlt); addD(y, node[x].dlt);
    }
    int access(int x)
    {
        int y = 0;
        while (x)
        {
            splay(x); ins(x, Rs(x)); del(x, y);
            Rs(x) = y; pushup(x); x = Fa(y = x);
        }
        return y;
    }
    void makeroot(int x) { access(x); splay(x); reverse(x); }
    void split(int x, int y) { makeroot(x); access(y); splay(y); }
    void link(int x, int y) { makeroot(x); makeroot(y); access(y); Fa(x) = y; Rs(y) = x; pushup(y); }
    void add(int rt, int x, ll v)
    {   
        split(rt, x); node[x].dlt += v; node[x].w += v;
        node[x].sum += (node[x].szl + 1) * v;
    }   
    ll ask(int rt, int x) { return split(rt, x), (node[x].w + node[x].suml + node[x].szl * node[x].dlt); }
}lct;
int main()
{
    int n, q, rt = 1; scanf("%d%d", &n, &q);
    for (int i = 1; i <= n; ++i) scanf("%lld", &lct.node[i].w), lct.pushup(i);
    for (int i = 1, x, y; i < n; ++i) scanf("%d%d", &x, &y), lct.link(x, y);
    for (int i = 1, opt, u, v, x, lca; i <= q; ++i)
    {
        scanf("%d", &opt);
        switch (opt)
        {
            case 1:
                scanf("%d", &v); rt = v;
                break;
            case 2:
                scanf("%d%d%d", &u, &v, &x); lct.makeroot(rt);
                lca = (lct.access(u), lct.access(v)); lct.add(rt, lca, x);
                break;
            case 3:
                scanf("%d", &v); 
                printf("%lld\n", lct.ask(rt, v));
        }
    }
    return 0;
}

感谢 \tt \color{black}j\color{red}erry3128 对我 LCT 的耐心指导和代码参考。

本题中需要维护的信息是满足可加性的,但如果不满足呢?我们就需要对虚子树开一个平衡树维护了,具体见:P3979 遥远的国度。

萌新初学 LCT 维护子树信息,如果有理解偏颇或者没理解到的地方请轻喷并指出qwq