跳到正文

个人记录

重链剖分

yinjunrui约 11 分钟
原文
文章信息与版本

文章信息

发布
2026年8月15日
收录
2026年8月16日
更新
2026年11月9日
篇幅
约 11 分钟

版本

  1. v1当前

tips: 今天的题实际上大部分都有比树链剖分更优的做法,只是因为今天主题不是它,故不讲(我不会说我不会的)

P3703

LCT板子不讲

首先,因为每次操作颜色都是新的,所以通过树上差分维护操作 22,具体的,树上 xx 到 yy 的距离为: disx+disy−2⋅dislca(x,y)+1dis_x+dis_y-2\cdot dis_{lca(x, y)} + 1 其中 disxdis_x 表示 11 到 xx 的路径上的颜色数

那么操作 33 也可以维护,即找 xx 子树内最大的 disvdis_v

对于一个点的更新,容易发现它只会对它子树内的答案

而操作 11 是对路径修改,所以可以考虑树链剖分修改

我们约定:

  • 一条边所连接的两个点的颜色不同为异色边,否则为同色边。
  • 认为重链上的边都是同色边,轻边都是异色边。
  • 维护 s[t]s[t] 表示以 tt 为链头的重链内的同色边
  • 维护 s1[t]s1[t] 表示与以 tt 为链头的重链内的点相连接的轻边中的异色边

对于一次操作 11,我们设 t=top[x]t = top[x]

  1. 将 s[t]s[t] 中在区间内的边都删除,因为此时的轻边都变为了异色边,并将对应子树内的 disvdis_v 加 11
  2. 将原先 s1[t]s1[t] 中对应区间内的边从 s1[t]s1[t] 中删除,并将对应子树的 disvdis_v 减 11
  3. tt 和 fa[t]fa[t] 是轻边关系,若 tt 不在 s[top[fa[t]]]s[top[fa[t]]] 内,则说明 tt 和 fa[t]fa[t] 是 异色边变成同色边,将 tt 加入 s[top[fa[t]]]s[top[fa[t]]],并将 tt 的子树的 disvdis_v 减 11
  4. 若 son[x]son[x] 不在 s1[t]s1[t] 内,则代表 xx 和 son[x]son[x] 这条重边原先是同色,现在是异色,则将 son[x]son[x] 加入,并将 son[x]son[x] 中的 disvdis_v 加 11

因为在一次操作 11 后,之前做过一次的区间的答案不会被更新,每条重边在被更新后,所以所有边至多总共被更新 n+qn + q 次

具体维护时,可以用 setset 维护 ss 和 s1s1 以快速查询区间内的边

C++
#include<bits/stdc++.h>
#define int long long
#define pii pair<int, int>
using namespace std;
const int N = 5e5 + 5;
int n, m, cnt, siz[N], dfn[N], son[N], dep[N], fa[N], rk[N], top[N];
vector<int> e[N];
set<pii> s[N], s1[N]; // s[t] 代表以t为链头的同色边的轻儿子 s1[t] 代表以t为链头的异色边的重儿子(深度偏移, 节点编号)
void dfs1(int x, int f){
    dep[x] = dep[f] + 1, siz[x] = 1, fa[x] = f, son[x] = 0;
    for(int v : e[x]){
        if(v == f) continue;
        dfs1(v, x);
        siz[x] += siz[v];
        if(siz[v] > siz[son[x]]) son[x] = v;
    }
}
void dfs2(int x, int t){
    dfn[x] = ++cnt, rk[cnt] = x, top[x] = t;
    if(son[x]){
        dfs2(son[x], t);
        s1[t].insert({dep[x] - dep[t] + 1, son[x]});
    }
    for(int v : e[x])
        if(v != fa[x] && v != son[x])
            dfs2(v, v);
}
struct tree{
    int laz[N << 2], mx[N << 2];
    void pushup(int x){
        mx[x] = max(mx[x << 1], mx[x << 1 | 1]);
    }
    void pushdown(int x, int l, int r){
        laz[x << 1 | 1] = laz[x << 1 | 1] + laz[x];
        mx[x << 1 | 1] = mx[x << 1 | 1] + laz[x];
        laz[x << 1] = laz[x << 1] + laz[x];
        mx[x << 1] = mx[x << 1] + laz[x];
        laz[x] = 0;
    }
    void build(int x, int l, int r){
        if(l == r){
            mx[x] = dep[rk[l]];
            return;
        }
        int mid = l + r >> 1;
        build(x << 1, l, mid), build(x << 1 | 1, mid + 1, r);
        pushup(x);
    }
    void update(int x, int l, int r, int L, int R, int v){
        if(L <= l && r <= R){
            laz[x] += v, mx[x] += v;
            return;
        }
        int mid = l + r >> 1;
        if(laz[x]) pushdown(x, l, r);
        if(L <= mid) update(x << 1, l, mid, L, R, v);
        if(mid < R) update(x << 1 | 1, mid + 1, r, L, R, v);
        pushup(x);
    }
    int query(int x, int l, int r, int L, int R){
        if(L <= l && r <= R)
            return mx[x];
        int mid = l + r >> 1, res = 0;
        if(laz[x]) pushdown(x, l, r);
        if(L <= mid) res = max(res, query(x << 1, l, mid, L, R));
        if(mid < R) res = max(res, query(x << 1 | 1, mid + 1, r, L, R));
        return res;
    }
} tr;
int lca(int x, int y){
    while(top[x] != top[y]){
        if(dep[top[x]] < dep[top[y]]) swap(x, y);
        x = fa[top[x]];
    }
    return (dep[x] < dep[y] ? x : y);
}
void update_son(int x) {
    int u = x, last = 0;
    while (u) {
        int t = top[u], d = dep[u] - dep[t];
        while (!s[t].empty() && (*s[t].begin()).first <= d) {
            auto it = *s[t].begin();
            s[t].erase(s[t].begin());
            tr.update(1, 1, n, dfn[it.second], dfn[it.second] + siz[it.second] - 1, 1);
        }
        while (!s1[t].empty() && (*s1[t].begin()).first <= d) {
            auto it = *s1[t].begin();
            s1[t].erase(s1[t].begin());
            tr.update(1, 1, n, dfn[it.second], dfn[it.second] + siz[it.second] - 1, -1);
        }
        if (son[u]) {
            int son_d = d + 1;
            auto it = s1[t].lower_bound({son_d, 0});
            if (it == s1[t].end() || (*it).first != son_d) {
                s1[t].insert({son_d, son[u]});
                tr.update(1, 1, n, dfn[son[u]], dfn[son[u]] + siz[son[u]] - 1, 1);
            }
        }
        if (last) {
            s[t].insert({d, last});
            tr.update(1, 1, n, dfn[last], dfn[last] + siz[last] - 1, -1);
        }

        last = t;
        u = fa[t];
    }
}
signed main(){
    cin.tie(0)->sync_with_stdio(0);
    cin >> n >> m;
    for(int i = 1, u, v; i < n; i++){
        cin >> u >> v;
        e[u].emplace_back(v), e[v].emplace_back(u);
    }
    dfs1(1, 0), dfs2(1, 1);
    tr.build(1, 1, n);
    for(int i = 1, op, x, y, v; i <= m; i++){
        cin >> op;
        if(op == 1){
            cin >> x;
            update_son(x);
        }
        if(op == 2){
            cin >> x >> y;
            int l = lca(x, y);
            int dx = tr.query(1, 1, n, dfn[x], dfn[x]);
            int dy = tr.query(1, 1, n, dfn[y], dfn[y]);
            int dl = tr.query(1, 1, n, dfn[l], dfn[l]);
            cout << dx + dy - 2 * dl + 1 << '\n';
        }
        if(op == 3){
            cin >> x;
            cout << tr.query(1, 1, n, dfn[x], dfn[x] + siz[x] - 1) << '\n';
        }
    }
    return 0;
}

AT_kupc2021_e

首先对于题目要求在赋完权值后最小生成树依旧由红边组成,则可以转化为在加入一条蓝边时,它两端的节点 uu 和 vv 已经被红边所联通,则就是在树上 uu 和 vv 路径上的红边都需要比蓝边小,才能保证唯一性

则我们可以视作这条路径上的每条红边都被这条蓝边约束,则我们可以统计出每条红边被多少条蓝边约束 例如树上差分 disxdis_x 加 11、disydis_y 加 11、dislca(x,y)dis_{lca(x,y)} 减 22,再做dfs即可

在我们分配边权时,应当优先给编号大的蓝边和没有约束的红边,当我们给一条蓝边分配时,我们发现在这之后,它所约束的红边所分配的边权一定严格小于它,所以相当于对于它所覆盖的那段路径上的边都可以减少一次约束,当一条边约束为 00 时,则代表所有约束它的蓝边都被赋上了边权,则它就可以丢进优先队列中匹配边权了

而对于路径上的边权维护,我们用线段树就可以了

原文