浅谈重链剖分
Fail_Void_Vanish
·
·
个人记录
闲话:标题没写树链剖分,因为我没学过长链剖分,到时候补(也可能不补了)。
但是树链剖分大部分情况下就是指重链剖分。
前置知识
你需要学会熟练地使用:
$A$:LCA 即查找 $u$,$v$ 的最近公共祖先,对重链剖分的跳重链的过程可以更好地理解。
例题(前置知识)
---
如果是大佬请自行跳过。
[BJOI2018] 求和
---
> [P4427](https://www.luogu.com.cn/problem/P4427)。
>
> 给定一棵 $n$ 个节点的树,以及 $Q$ 次询问,每次询问给定 $\left(l,r,k\right)$,请你求出树上编号为 $l$ 和 $r$ 之间(包括 $l$,$r$)的所有节点的深度的 $k$ 次方和,答案对 $998244353$ 取模。
>
> 根节点深度为 $0$。
>
> $1\le n,Q\le 3\times 10^5$,$1\le k\le 50$。
容易发现这棵树不带修改,且 $k$ 比较小。所以可以在询问前预处理对于 $i\in [1,k] \ \land\ i\in \mathbb{Z}^+$ 的答案$^{\textcircled{1}}$。
$\textcircled{1}$:答案指的是从根节点到当前节点 $p$ 的路径上的所有节点的深度的 $i$ 次方和。以下记作 $dis_{p,i}$。
然后套出 LCA 板子,每次询问求出 $i$,$j$ 的 LCA $lca$,答案即为 $dis_{i,k} + dis_{j,k} - dis_{lca,k}\times 2 + a_{lca}^{\ \ \ \ k}$,$fa$ 的定义同 LCA,$a_i$ 指 $i$ 的点权。
时间复杂度为 $\mathcal O(n + Q\log n)$。
> code 有空补。
---
还有维护满足结合律的区间和、区间积、区间最值、区间翻转、区间反转等的线段树,这里我就不阐述了。
引入
---
【模板】重链剖分/树链剖分
---
这里通过[板子](https://www.luogu.com.cn/problem/P3384)引入。
> 给定一棵包含 $n$ 个结点的树(根节点为 $r$),每个节点有一个点权,以及 $Q$ 次询问,询问内容如下:
>
> - `1 x y z`,将树 $x$ 到 $y$ 的简单路径上所有节点的值都加上 $z$。
>
> - `2 x y`,求树从 $x$ 到 $y$ 的简单路径上所有节点的值之和。
>
> - `3 x z`,将以 $x$ 为根节点的子树内所有节点值都加上 $z$。
>
> - `4 x`,求以 $x$ 为根节点的子树内所有节点值之和。
>
> 对于操作 $2,4$ 的答案需要对给定的 $p$ 取模。
>
> $1\le n \le {10}^5$,$1\le Q \le {10}^5$,$1\le r\le N$,$1\le p \le 2^{30}$。所有输入的数均在 `int` 范围内。
考虑只有操作 $2$,那么就如『例题(前置知识)』里一样直接 dfs 预处理 + LCA 查询,时间复杂度为 $\mathcal O(n + Q\log n)$。
那如果带上操作 $4$ 呢?那么 LCA 就无能为力了。
这个时候我们要引入本文的重点:重链剖分!
### 定义:
当然哈图论总是有新的名称。
- 重儿子:根节点的儿子中点权最大的儿子。
- 重边:根节点连向重儿子的边。
- 重链:若干条重边连接起来的东西,注意单个剩余节点也算重链。
- 轻儿子:除了重儿子以外的儿子。
- 轻边:所有连向儿子的边除了重边的边。
### 静态问题
套用一下 <https://oi-wiki.org> 的图(略带修改):

可以发现:
- 重链的开头一定是轻儿子(重儿子都被加入重链了,一条重链总有限制长度,限制要求为轻儿子开头)。
- dfs 序并非传统的搜索,而是重儿子优先,轻儿子随便顺序都可以。如果儿子的点权有多个最大值,那么任选一个即可。
- 同一个重链内的 dfs 序是**连续的**(原因动一下脑就可以想到)。
- 同一棵子树内的 dfs 序也是**连续的**(类似于 dfs 序)。
然后我们将如上树的点权按照 **dfs 序**折射成一个数组 $w$:
$$w:\boxed{2}\boxed{6}\boxed{4}\boxed{5}\boxed{10}\boxed{1}\boxed{1}\boxed{3}\boxed{3}\boxed{4}\boxed{1}\boxed{5}\boxed{9}\boxed{3}\boxed{8}\boxed{3}\boxed{5}\boxed{514}\boxed{114}$$
回到操作 $2$,我们发现,节点 $x$,$y$ 之间的简单路径实际上是由**多条重链组合**加上**一段同一条重链内的路径**,于是我们可以使用跳重链的方式累加答案,可以用**前缀和**维护,具体内容到时候展开。
然后是操作 $4$,由于子树内的下标在 $w$ 中是连续的,所以可以实现预处理子树大小,然后直接**前缀和**计算即可。
时间复杂度下面讲。
### 动态问题
~~讲了这么点,树还是静态的。~~
不过没关系,因为我们离正解只有一步之遥。
对于修改操作,我们可以像查询操作一样,$1$ 也跳重链,$3$ 通过子树大小计算后修改。
这样我们支持区间加和与区间查询的数据结构,我们能够想到的是:**线段树**!
BIT 常数小,但是范围局限,区间最值的实现过程也不好写,所以这里我们选择幸运儿线段树。
### 时空复杂度分析:
首先是 dfs,时间复杂度为 $\mathcal O(n)$,空间复杂度为 $\mathcal O(n)$。
其次查询和修改,时间复杂度为 $O(\log^2 n)^{\textcircled{2}}$,空间复杂度为 $\mathcal O(n)$。
$\textcircled{2}$:首先线段树查询和修改是 $\mathcal O(\log n)$ 的;其次跳重链的时候总是跳到轻儿子上面,于是乎一条路径上最多有 $\log n$ 个轻儿子,也就是最多会跳 $\mathcal O(\log n)$ 条重链,总时间复杂度为 $\mathcal O(\log^2 n)$。
因此,对于操作 $2,4$,时间复杂度为 $\mathcal O(n + Q\log n)$(可以用前缀和 $\mathcal O(1)$ 解决查询);对于**所有**操作,时间复杂度为 $\mathcal O(n + Q\log^2 n)$,足以通过本题。
### 一些小问题
我曾经思考过一个问题,普通的 dfs 生成的序列能不能维护?
对于子树内的下标无疑是连续的,但是对于两个节点之间的路径我们无从下手。
所以这就是重链剖分产生的意义。(~~猜测~~
综上所述,如果只是修改与查询子树内的答案,你当然直接 dfs 序也可以,不用重链剖分(~~偷懒~~。
### 代码实现
似乎一段一段放代码比一下子放代码更容易理解,所以我还是一段一段放代码。(~~大雾~~
#### dfs 部分
先罗列一下要生成啥:
- $i$ 所属重链中最顶端的节点 $top_i$(不记录路径),因为重链中的节点有用的只有头顶节点。
- 子树 $i$ 的大小 $sz_i$。
- $w$ 数组。
- dfs 遍历顺序($w$ 数组中的下标)$id_i$。
- 节点的父亲 $fa_i$:跳重链需要用,即 $fa_{top_u}$。
- $i$ 的重儿子 $wson_i$,辅助完成 $top$。
- 节点深度 $deep_i$(下面会讲到)。
观察到,$wson$ 需要先完成才能完成 $top$;实现 $top$ 的 dfs 是『重度优先搜索』,而 $id$ 由于是『重度优先搜索』下的编号所以和 $top$ 放在一起。$w$ 也需要在『重度优先搜索』下完成所以和它们放在一起(dfs2),剩余的都放在 dfs1。
```cpp
inline void dfs1(int u, int fath) {//处理 deep ,sz ,fa & sz。
//u 是当前节点,fath 是父亲。
deep[u] = deep[fath] + 1 ,sz[u] = 1 ,fa[u] = fath;//很好理解。
for (int v : edge[u]) {
if (v == fath) continue;
dfs1(v, u);
sz[u] += sz[v];//遍历树的模板。
if (sz[wson[u]] < sz[v]) wson[u] = v;//更新重儿子。
}
}
inline void dfs2(int u, int t) { //处理 top ,id,w。
//u,t 分别表示当前节点和 u 所在重链的头顶 t。
top[u] = t ,id[u] = ++ cnt ,w[cnt] = a[u];
//如果你连重儿子都没有你就是叶节点,更新过了就别继续了,不然这样 0 节点会多占一个 id 和 w 的空间,top 倒是没啥关系。
if (!wson[u]) return;
dfs2(wson[u], t);//『重度优先搜索』。
for (int v : edge[u]) {
if (v == fa[u] || v == wson[u]) continue;//你都是重儿子了都遍历过了就别占用空间了。
dfs2(v, v);//轻儿子开头重链啊,顶端就是自己哈。
}
/*
PS:这里占用空间的解释:
第一会导致少计算几个节点;
第二会导致重复计算,并且少计算几个节点。
因为 sz 是不会更新的。
所以并不只是单纯的『占用空间』。
*/
}
```
#### 线段树部分
上模板!
```cpp
/*
PS:作者默认你会熟练应用线段树,这里不解释。
区间求和和查询都是最基础的板子。
*/
struct segment_tree {
int l, r, add, sum;
} tr[N << 2];
inline void pushup(int u) {
tr[u].sum = tr[lc].sum + tr[rc].sum;
}
inline void pushdown(int u) {
if (tr[u].add) {
tr[lc].sum += tr[u].add * (tr[lc].r - tr[lc].l + 1);
tr[lc].add += tr[u].add;
tr[rc].sum += tr[u].add * (tr[rc].r - tr[rc].l + 1);
tr[rc].add += tr[u].add;
tr[u].add = 0;
}
}
inline void build(int u, int l, int r) {
tr[u] = {l, r, 0, w[r]};
if (l == r) return;
int mid = ((l + r) >> 1);
build(lc, l, mid);
build(rc, mid + 1, r);
pushup(u);
}
inline void update(int u, int L, int R, int k) {
int l = tr[u].l, r = tr[u].r;
if (l >= L && r <= R) {
tr[u].add += k;
tr[u].sum += k * (r - l + 1);
return;
}
pushdown(u);
int mid = ((l + r) >> 1);
if (L <= mid) update(lc, L, R, k);
if (mid < R) update(rc, L, R, k);
pushup(u);
}
inline void update_tree(int u, int k) {
update(1, id[u], id[u] + sz[u] - 1, k);
}
inline int query(int u, int L, int R) {
int l = tr[u].l, r = tr[u].r;
if (l >= L && r <= R) return tr[u].sum;
pushdown(u);
int mid = ((l + r) >> 1);
int res = 0;
if (L <= mid) res += query(lc, L, R);
if (mid < R) res += query(rc, L, R);
return res;
}
inline int query_tree(int u) {
return query(1, id[u], id[u] + sz[u] - 1);
}
```
#### 跳重链部分
啊哈前面抛了一个问题在这(~~你们应该都忘掉了~~):$deep$ 有啥用?
第一处:我们一致让 $top_u$ 的深度大于等于 $top_v$ 的深度,不然一直跳 $u$ 会导致这样的情况:

第二处:最后的同一个重链内的修改/查询,目标区间 $[L,R]$ 一定满足 $L\leq R$。由于在**同一条重链中**,所以 $deep$ 更深的那个 $id$ 更大。
```cpp
inline void update_path(int u, int v, int k) {
while (top[u] != top[v]) {//不在同一条重链。
if (deep[top[u]] < deep[top[v]]) swap(u, v);//原因见上。
update(1, id[top[u]], id[u], k);//修改。
u = fa[top[u]];//跳重链。
}
if (deep[u] > deep[v]) swap(u, v);//原因见上。
update(1, id[u], id[v], k);//不要忘了 awa。
}
inline int query_path(int u, int v) {
//和 update_path 一模一样。
int res = 0;
while (top[u] != top[v]) {
if (deep[top[u]] < deep[top[v]]) swap(u, v);
res += query(1, id[top[u]], id[u]);
u = fa[top[u]];
}
if (deep[u] > deep[v]) swap(u, v);
res += query(1, id[u], id[v]);
return res;
}
```
#### 剩下的都是读入部分,略。
### 完整代码
```cpp
#include<bits/stdc++.h>
#define int long long
#define up(i,x,y) for(register int i=x;i<=y;i++)
#define pr printf
using namespace std;
inline int read() {
int x = 0, f = 0;
char ch = getchar();
while (!isdigit(ch)) {
f |= (ch == '-');
ch = getchar();
}
while (isdigit(ch))x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return f ? -x : x;
}
inline void write(int x) {
if (x < 0)putchar('-'), x = -x;
if (x > 9)write(x / 10);
putchar(x % 10 | 48);
}
inline void writeln(int x) {
write(x), putchar('\n');
}
inline void writesp(int x) {
write(x), putchar(' ');
}
#define lc u << 1
#define rc u << 1 | 1
const int N = 1e5 + 10;
int a[N];
vector <int> edge[N];
int fa[N], deep[N], sz[N], wson[N], top[N], id[N], w[N], cnt;
inline void dfs1(int u, int fath) {
deep[u] = deep[fath] + 1 ,sz[u] = 1 ,fa[u] = fath;
for (int v : edge[u]) {
if (v == fath) continue;
dfs1(v, u);
sz[u] += sz[v];
if (sz[wson[u]] < sz[v]) wson[u] = v;
}
}
inline void dfs2(int u, int t) { //处理 top ,id,nw。
top[u] = t ,id[u] = ++ cnt ,w[cnt] = a[u];
if (!wson[u]) return;
dfs2(wson[u], t);
for (int v : edge[u]) {
if (v == fa[u] || v == wson[u]) continue;
dfs2(v, v);
}
}
struct segment_tree {
int l, r, add, sum;
} tr[N << 2];
inline void pushup(int u) {
tr[u].sum = tr[lc].sum + tr[rc].sum;
}
inline void pushdown(int u) {
if (tr[u].add) {
tr[lc].sum += tr[u].add * (tr[lc].r - tr[lc].l + 1);
tr[lc].add += tr[u].add;
tr[rc].sum += tr[u].add * (tr[rc].r - tr[rc].l + 1);
tr[rc].add += tr[u].add;
tr[u].add = 0;
}
}
inline void build(int u, int l, int r) {
tr[u] = {l, r, 0, w[r]};
if (l == r) return;
int mid = ((l + r) >> 1);
build(lc, l, mid);
build(rc, mid + 1, r);
pushup(u);
}
inline int query(int u, int L, int R) {
int l = tr[u].l, r = tr[u].r;
if (l >= L && r <= R) return tr[u].sum;
pushdown(u);
int mid = ((l + r) >> 1);
int res = 0;
if (L <= mid) res += query(lc, L, R);
if (mid < R) res += query(rc, L, R);
return res;
}
inline int query_path(int u, int v) {
int res = 0;
while (top[u] != top[v]) {
if (deep[top[u]] < deep[top[v]]) swap(u, v);
res += query(1, id[top[u]], id[u]);
u = fa[top[u]];
}
if (deep[u] > deep[v]) swap(u, v);
res += query(1, id[u], id[v]);
return res;
}
inline int query_tree(int u) {
return query(1, id[u], id[u] + sz[u] - 1);
}
inline void update(int u, int L, int R, int k) {
int l = tr[u].l, r = tr[u].r;
if (l >= L && r <= R) {
tr[u].add += k;
tr[u].sum += k * (r - l + 1);
return;
}
pushdown(u);
int mid = ((l + r) >> 1);
if (L <= mid) update(lc, L, R, k);
if (mid < R) update(rc, L, R, k);
pushup(u);
}
inline void update_path(int u, int v, int k) {
while (top[u] != top[v]) {
if (deep[top[u]] < deep[top[v]]) swap(u, v);
update(1, id[top[u]], id[u], k);
u = fa[top[u]];
}
if (deep[u] > deep[v]) swap(u, v);
update(1, id[u], id[v], k);
}
inline void update_tree(int u, int k) {
update(1, id[u], id[u] + sz[u] - 1, k);
}
signed main() {
int n = read(), m = read(), root = read(), p = read();
up(i, 1, n) a[i] = read();
up(i, 1, n - 1) {
int u = read(), v = read();
edge[u].push_back(v);
edge[v].push_back(u);
}
dfs1(root, 0);//根节点没有父亲哈。
dfs2(root, root);//根节点的重链顶端肯定是自己哈。
build(1, 1, n);
while (m --) {
int op = read(), u = read();
if (op == 1) {
int v = read(), k = read();
update_path(u, v, k);
}
if (op == 2) {
int v = read();
writeln(query_path(u, v) % p);
}
if (op == 3) {
int k = read();
update_tree(u, k);
}
if (op == 4) writeln(query_tree(u) % p);
}
return 0;
} // 158 行代码完结撒花!
```

重链剖分应用 $1$:边权转点权
---
$T1$ [USACO11DEC] Grass Planting G
---
> [P3038](https://www.luogu.com.cn/problem/P3038)。
>
> 给出一棵有 $n$ 个节点的树,以及 $m$ 次操作:
>
> - `P x y`:将 $x$,$y$ 之间的简单路径上的边权均加一。
>
> - `Q x y`:查询 $x$,$y$ 之间的那一条边的边权,保证两个节点**直接相连**。
>
> 初始边权均为 $0$。
>
> $2\le n\le 10^5$,$1\le m\le 10^5$。
那时的我:哎呀这玩意不是很简单嘛,套个模板就行了。
~~结果你们一定猜得到。~~
注意板子题维护的是『点权』,而现在我们需要维护『边权』。
假设有一对父子关系 $u$,$v$,边权为 $w$($deep_u < deep_v$),那么可以将 $w$ 赋值到 $a_v$。
这样在修改/查询时,剩余的情况为在同一个重链内的 $u$,$v$,这时候 $\operatorname{update}/\operatorname{query}$ 的两个参数 `id[u] ,id[v]` 就需要改为 `id[u] + 1 ,id[v]`,这样就不会计算 $fa_u$ 和 $u$ 之间的那条边的边权了!
注意在线段树修改/查询时可能出现 $id_u + 1 > id_v$(即 $id_u = id_v$)的情况,对我们理解有点误导,这时候特判就行了(不加也没关系,~~**助于理解**~~)。
时间复杂度为 $\mathcal O(m\log^2 n)$。
$T2$ 月下“毛景树”
---
> [P4315](https://www.luogu.com.cn/problem/P4315)
>
> 给定一棵 $n$ 个节点的树,有若干如下操作,以 `Stop` 结束:
>
> - `Change k w`:将第 $k$ 条边的边权改为 $w$。
>
> - `Cover u v w`:将 $u$,$v$ 之间的边的边权都改为 $w$。
>
> - `Add u v w`:将 $u$,$v$ 之间的边的边权都加 $w$。
>
> - `Max u v`:求 $u$,$v$ 之间的边的边权的最大值。
>
> $1\le n\le 10^5$,操作和询问数目不超过 $10^5$,且在操作后所有边权都不会大于 $10^9$。
板子题。
`Change` 操作就是单点修改,`Cover` 操作就是区间覆盖,`Add` 操作就是区间加,`Max` 操作就是区间求最大值。
这里就是维护区间覆盖(懒标记)、区间最大值(信息)、区间加(懒标记)的板子,熟练应用线段树就可以 AC 了。
注意这里是『边权』不是『点权』。
时间复杂度为 $\mathcal O(m\log^2 n)$,$m$ 为操作次数。
## $1.2.1^+$ 关于边权的一个小问题
既然能把 $(u,v,w)$ 折射到 $a_v = w$,那么能不能把 $(u,v,w)$ 折射到 $a_u = w$ 呢?
明显更麻烦,所以不提倡,当然你硬要做可以写几个特判,我可能会在视频里提到。
$1.2.2$ 重链剖分应用 $2$:求 LCA
---
既然『前置知识』里有 LCA,那么我们能不能把 LCA 不改成『前置知识』呢(~~线段树还是算了~~)?
答案是可以的。
首先预处理一模一样,仍是 `dfs1` 和 `dfs2`。
> 现在要证明一个点:对于两个节点 $x$,$y$,若不在同一条重链上,则 $\operatorname{LCA}(x,y)$ 也不在较深的重链上。
证明:

此时 $x$,$y$ 所属重链没有交集,LCA 也不在这 $y$ 所属重链上。

此时两者 LCA 为 $x$,$x$ 不在 $y$ 所属重链中。
分类讨论完毕,因此得证。
所以我们可以模仿跳重链的过程,跳到同一重链时,取深度较浅的节点即为 LCA(想不通记得注意重链是一条链)。
上面证明过了一条路径的重链个数为 $\mathcal O(\log n)$ 级别,时间复杂度为 $\mathcal O(n\log n)$。
$T1$【模板】最近公共祖先(LCA)
---
> [P3379](https://www.luogu.com.cn/problem/P3379)。
>
> 给定 $Q$ 组询问,每次给定 $x$,$y$,求在给出的 $n$ 个节点的树中,$\operatorname{LCA}(x,y)$。
>
> $1\le n ,m\le 5\times 10^5$,$1\le x ,y\le n$。
看懂上面的,这就是板子题了 awa。
时间复杂度为 $\mathcal O(Q\log n)$。
```cpp
/* 蒟蒻中期码风,略丑,勿喷。 */
#include<bits/stdc++.h>
#define int long long
#define up(i,x,y) for(register int i=x;i<=y;i++)
using namespace std;
inline int read(){int x = 0, f = 0;char ch = getchar();while (!isdigit(ch)) {f |= (ch == '-');ch = getchar();}while (isdigit(ch))x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();return f ? -x : x;}
inline void write(int x){if (x < 0)putchar('-'), x = -x;if (x > 9)write(x / 10);putchar(x % 10 | 48);}
inline void writeln(int x){write(x), putchar('\n');}
inline void writesp(int x){write(x), putchar(' ');}
const int N = 5e5 + 10;
vector <int> edge[N];
int fa[N],deep[N],son[N],sz[N];
int top[N];
inline void dfs1(int u,int fath){
fa[u] = fath ,deep[u] = deep[fath] + 1, sz[u] = 1;
for(int v : edge[u]){
if(v == fath) continue;
dfs1(v,u);
sz[u] += sz[v];
if(sz[son[u]] < sz[v]) son[u] = v;
}
} inline void dfs2(int u,int t){
top[u] = t;
if(!son[u]) return;
dfs2(son[u],t);
for(int v : edge[u]){
if(v == fa[u] || v == son[u]) continue;
dfs2(v,v);
}
}
/* 以上都是基本操作。 */
inline int LCA(int u,int v){
while(top[u] != top[v]){
if(deep[top[u]] < deep[top[v]]) swap(u,v);
u = fa[top[u]];
}
return deep[u] < deep[v] ? u : v;// 选取深度较低的点为 LCA。
} signed main() {
int n = read() , Q = read() ,s = read();
up(i,1,n - 1){int u = read() ,v = read();edge[u].push_back(v);edge[v].push_back(u);}
dfs1(s,0);
dfs2(s,s);
while(Q--){int u = read() ,v = read() ;writeln(LCA(u,v));}
return 0;
}
```
现在前置知识(例题)里的求 LCA 步骤就可以用树剖了!(~~小题大做~~
---
$2\ \texttt{Problem Part}
从现在开始,如果是很简单的题我就不写题目大意了。
一般蓝都是板子。
P3178。
P3384 严格弱化版,不予评价。
时间复杂度为 \mathcal O(m\log^2 n)。
P2590。
P4315 不严格弱化版,不予评价。
时间复杂度为 \mathcal O(Q\log^2 n)。
P2146。
题目很长,但是实际上很简单。
『没有环』提示我们这是一棵树。
考虑把当前为安装状态的看成 1,当且为不安装状态的看成 0。
然后就是区间覆盖+区间求和的板子,用重剖辅助即可。
时间复杂度为 \mathcal O(Q\log^2 n)。
P5838。
有点提升。
如果一个一个重链判断,暴力搜索节点查看是否存在 a_i = C,重链个数为 \mathcal O(\log n) 级,节点个数为 \mathcal O(n),一不小心一次询问就被良心出题人卡到 \mathcal O(n\log n),无法 AC。
观察到 1\le C_i\le n,于是我们把颜色为 i 的节点在『重度优先搜索』下的编号(即 id)存进 vec_i,然后在重链判断时二分即可(重链节点的 id 一定连续)。
时间复杂度为 \mathcal O(m\log^2 n),可以 AC。
P1505。
不难,但很烦,维护的节点很多。
倒不如说一道线段树强化题(,学过树剖这题直接秒。
时间复杂度为 \mathcal O(m\log^2 n)。
:::info[没写完不要看]
P4092。
给定一颗根为 1 的有根树,以及 n - 1 条单项边,有以下两种操作:
上面都是一眼,这题至少没有一眼。
首先对于节点 u,可能能够改变答案的只有以 u 为根的子树。
所以对于 [id_u,id_u+sz_u-1] 的节点,。。。。
| 对于查询的时候可以一直跳重链,去到的最大 id 值即为答案,因为对于同一条重链,id 值单调上升,我们只需要写一个反函数输出原本编号节点即可。 |
参考图片:
- https://oi-wiki.org/graph/hld/。
参考文献: