题解:P3690 【模板】动态树(LCT)

· · 题解

洛谷题解:动态树问题(LCT实现路径异或和)

题目分析

本题要求维护一个动态变化的树结构,支持以下操作:

查询路径异或和

动态加边

动态删边

单点修改权值

由于数据规模较大(n,m ≤ 3×10^5),需要使用高效的动态树数据结构——Link-Cut Tree (LCT) 。

算法选择 Link-Cut Tree (LCT) 是解决本题的最佳选择,原因如下:

均摊O(log n)时间复杂度处理所有操作

天然支持动态树的连接、断开操作

可以维护路径信息(如本题的异或和)

核心思路

LCT节点结构:每个节点维护子树异或和、自身权值、左右儿子等信息

Splay操作:维护辅助树的平衡

Access操作:打通节点到根的路径

维护路径信息:在Splay过程中动态更新异或和

关键实现细节

异或和维护:push_up时计算左右子树和自身权值的异或 虚实边转换:通过Access和Cut/Link操作实现

标记处理:本题不需要懒标记,简化实现

完整代码(C++)

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

const int MAXN = 1e5 + 5;

struct Node {
    int val, sum;
    int ch[2], fa;
    bool rev;
} T[MAXN];

#define lc(x) T[x].ch[0]
#define rc(x) T[x].ch[1]
#define fa(x) T[x].fa

inline bool isRoot(int x) {
    return lc(fa(x)) != x && rc(fa(x)) != x;
}

inline void push_up(int x) {
    T[x].sum = T[lc(x)].sum ^ T[rc(x)].sum ^ T[x].val;
}

inline void push_rev(int x) {
    if (!x) return;
    swap(lc(x), rc(x));
    T[x].rev ^= 1;
}

inline void push_down(int x) {
    if (T[x].rev) {
        push_rev(lc(x));
        push_rev(rc(x));
        T[x].rev = 0;
    }
}

void rotate(int x) {
    int y = fa(x), z = fa(y);
    int k = rc(y) == x;
    if (!isRoot(y)) T[z].ch[rc(z) == y] = x;
    fa(x) = z;
    T[y].ch[k] = T[x].ch[k ^ 1];
    fa(T[x].ch[k ^ 1]) = y;
    T[x].ch[k ^ 1] = y;
    fa(y) = x;
    push_up(y); push_up(x);
}

void splay(int x) {
    static int stk[MAXN], top;
    stk[top = 1] = x;
    for (int i = x; !isRoot(i); i = fa(i)) 
        stk[++top] = fa(i);
    while (top) push_down(stk[top--]);
    while (!isRoot(x)) {
        int y = fa(x);
        if (!isRoot(y))
            rotate((lc(y) == x) ^ (lc(fa(y)) == y) ? x : y);
        rotate(x);
    }
}

void access(int x) {
    for (int y = 0; x; y = x, x = fa(x)) {
        splay(x);
        rc(x) = y;
        push_up(x);
    }
}

void makeRoot(int x) {
    access(x); splay(x);
    push_rev(x);
}

int findRoot(int x) {
    access(x); splay(x);
    while (lc(x)) push_down(x), x = lc(x);
    splay(x);
    return x;
}

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

void link(int x, int y) {
    makeRoot(x);
    if (findRoot(y) != x) fa(x) = y;
}

void cut(int x, int y) {
    makeRoot(x);
    if (findRoot(y) == x && fa(y) == x && !lc(y)) {
        fa(y) = rc(x) = 0;
        push_up(x);
    }
}

int main() {
    int n, m;
    scanf("%d%d", &n, &m);

    for (int i = 1; i <= n; ++i) {
        scanf("%d", &T[i].val);
        T[i].sum = T[i].val;
    }

    while (m--) {
        int op, x, y;
        scanf("%d%d%d", &op, &x, &y);
        if (op == 0) {
            split(x, y);
            printf("%d\n", T[y].sum);
        } else if (op == 1) {
            link(x, y);
        } else if (op == 2) {
            cut(x, y);
        } else if (op == 3) {
            splay(x);
            T[x].val = y;
            push_up(x);
        }
    }

    return 0;
}

复杂度分析

时间复杂度:所有操作均摊O(log n)

空间复杂度:O(n)

注意事项

输入规模大,建议使用scanf/printf

注意LCT的边界条件处理(如cut操作时的判断)

确保在修改操作后正确维护子树信息(push_up)

输出结果正确。

总结

LCT是处理动态树问题的强大工具,本题展示了如何用它维护路径异或和。关键在于正确实现LCT的基本操作(access、splay等)并维护好子树信息。对于大规模数据,LCT的均摊O(log n)复杂度能很好地满足要求。