版本比较
链分治
正文
增加 230 行删除 313 行
331 行未改动全部展开33233333433533633633733833934034134234134234334434534645 行未改动39239339439539639639739839940078 行未改动47948048148248348448348448548648748885 行未改动5745755765775785785795805805815825825835845845855865865875885885895905905915925925935945955965975985996006016026036046056066076086096106116126136146155945955966185975986205996006016226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936026036046966056066986076087007017027037047057066096107087097107117127136116127156136147176156167197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298306176188326196208346216228368378388398408418428438448458468478488496236248518528538548558568578588598608618626256268646276288668678688696296306316328746336346356366377 行未改动64564664764889064965089289365165265365465565612 行未改动66967067167291467367491691791867567667767867968068124 行未改动70670770870995195271071171271371471523 行未改动7397407417429849859867437447457469889897477487497509929939947517527537547557567573 行未改动76176276376410061007100810091010101110121013765766767768769770771772773101577477577610181019777778779780102210237817827837847857861028102978778878979010321033791792793103579479579679779830 行未改动8298308318321074833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010821001100210031004100510061088108910901007331 行未改动全部展开int _test_ = 1, cas;
namespace zrh { const int N = 1e5 + 5; int n, q, a[N], b[N], dfn[N], tot; int n, q, a[N], b[N], dfn[N], tot; vector< int > g[N]; // segment tree #define ls (u << 1) #define rs (u << 1 | 1) struct V { int lsu, rsu, msu, sum; struct V { int lsu, rsu, msu, sum; V() { lsu = rsu = msu = sum = 0; } V(int a, int b, int c, int d) { lsu = a, rsu = b, msu = c, sum = d; } } tr[N << 2]; int cov[N << 2]; V operator+(V x, V y) { V ret;45 行未改动 void dfs1(int u, int tp) { top[u] = tp, dfn[u] = ++tot, b[tot] = a[u]; if (hea[u]) dfs1(hea[u], tp); F(v, g[u]) if (v != FA[u] && v != hea[u]) dfs1(v, v); } } void tdc(int u, int v, int w) { while (top[u] != top[v]) { if (dep[top[u]] < dep[top[v]]) swap(u, v); cha(1, 1, n, dfn[top[u]], dfn[u], w), u = FA[top[u]];78 行未改动 #define ion bw(20) char i[ion], o[ion], *icl = i, *icr = i, *oc = o; char gc() { return (icl == icr && (icr = (icl = i) + fread(i, 1, ion, stdin), icl == icr)) ? EOF : *icl++; } void pc(char c) { if (oc - o == ion) fwrite(o, 1, ion, stdout), oc = o; *oc++ = c; } void rd(auto &x) { char c; int f = 1; x = 0; while (c = gc(), c < '0' || c > '9') if (c == '-') f = -1; while ('0' <= c && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = gc(); x *= f; } void pr(auto x) { int a[64], p = 0; if (x < 0) x = -x, pc('-'); do a[p++] = x % 10; while (x /= 10); while (p--) pc(a[p] + '0'); } void rd(auto &x) { char c; int f = 1; x = 0; while (c = gc(), c < '0' || c > '9') if (c == '-') f = -1; while ('0' <= c && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = gc(); x *= f; } void pr(auto x) { int a[64], p = 0; if (x < 0) x = -x, pc('-'); do a[p++] = x % 10; while (x /= 10); while (p--) pc(a[p] + '0'); } IO& operator>>(char &c) { return c = gc(), *this; } IO& operator<<(char c) { return pc(c), *this; } IO& operator<<(const char *c) { while (*c) pc(*c++); return *this; } IO& operator>>(auto &x) { return rd(x), *this; }85 行未改动- 你说得对但是加上线段树就是 $\mathcal{O}(n \log ^ 2 n)$ 了,还没 LCT 优秀(不过实际跑起来快多了就是了)。
## $\bf{2.}$ 长链剖分
先咕了。### 约定
## $\bf{2 \frac{1}{2}}.$ Splay 的复杂度证明类似重链剖分,定义长儿子为结点儿子中子树深度最大的那一个。
看到后面就知道为什么要插个这个了。### 思想
### 定义- 引理 $3$:一个结点祖先中长链条数(也即短边数量)是 $\mathcal{O}(\sqrt{n})$ 量级的。
Splay 是一种旋转平衡树,旨在通过不断旋转保证树的平衡。:::info[proof]{open}
**因为本文的重点不是 Splay 所以不做多余的解释。**
### 时间复杂度考虑构造一种最坏情况,也即每个短儿子的长链长度都比父亲恰好大 $1$。
Splay 的时间复杂度是严格 $\mathcal{O}(n \log n)$,常数大于 SGT,略大于无旋 Treap。显然此时祖先链长链条数最多的是图中右下角的点,设其为 $k$。
另外由于 Splay 的复杂度均摊,故不支持可持久化。
:::info[不严谨的 proof]
这很难呐,不想看严谨证明就用一些不那么严谨的语言吧。
打个比方,你可以理解平衡树的根是器材室的门。操作就是去找器材。
每次操作我们都在结尾加上了 `splay(x)` 这样的语句。这就类似于你使用器材后把它放在了门口。
两种情况:
若这个“器材”很常用:
- 会在第一次使用的时候被提到门口。- 每次取用的成本都很低。
若这只是一个冷门的“器材”:
- 它在提到门口的同时“裹挟”了部分其他并不是那么冷门的“器材”上来,使得它们被优化了一小部分。整体看还是更优秀。
整体看很优秀,但每次肯定有成本,不可能到 $\mathcal{O}(n)$,所以大概就是 $\mathcal{O}(n \log n)$(什么鬼)。满足 $n = \sum i = \frac{k^2 + k}{ 2}$,则 $k$ 是 $\mathcal{O}(\sqrt{n})$ 量级的。$\square$:::
:::info[比较严谨的 proof]{open}- 引理 $4$:$u$ 的 $k$ 级祖先所在长链的长度不小于 $k$。
看不懂的话只能看详细证明了。其实是抄的 OI wiki,加了一点自己的理解。不过建议还是去看 OI wiki,链接位于参考文献。:::info[proof]{}设 $u$ 的 $k$ 级祖先是 $v$,$v$ 的链顶是 $w$。
定义一个结点 $u$ 的势能 $\omega(u) = \log siz_u$,子树 $u$ 的势能 $\phi(u) = \sum \omega(v)$,其中 $v$ 在 $u$ 的子树中。
势能就类似于熵混乱度的一个概念吧。
定义成本 $c = t + \phi^{\prime} - \phi$,其中 $\phi^{\prime}$ 为操作后子树势能、$t$ 为操作代价。我们所谓时间复杂度即 $\mathcal{O}\left(\sum t\right)$。
有性质:
- 因为 $siz_{fa_{u}} \ge siz_{u}$,所以 $\omega(fa_u) \ge \omega(u)$。- 若 $siz_{p} \ge siz_{u} + siz_{v}$,则 $2\omega(p) - \omega(u) - \omega(v) \ge 2$。
$$\begin{align*}2 \omega(p) - \omega(u) - \omega(v) &= \log siz_p^2 - \log siz_u - \log siz_v \\&= \log \frac{siz_p^2}{siz_usiz_v}\\&\ge \log \frac{(siz_u + siz_v)^2}{siz_usiz_v}\\&\ge \log \frac{4siz_usiz_v}{siz_usiz_v}= \log 4 = 2\end{align*}$$
**单旋操作**
$$\begin{align*}c &= \omega^{\prime}(x) - \omega(x) + \omega^{\prime}(p) - \omega(p)\\&= \omega(p) - \omega(x) + \omega^{\prime}(p) - \omega(p)\\&= \omega^{\prime}(p) - \omega(x)\\&\le \omega^{\prime}(x) - \omega(x)\end{align*}$$
**双旋操作**
成本都有式子:
$$\begin{align*}c &= \omega^{\prime}(x)+\omega^{\prime}(p)+\omega^{\prime}(g) - \omega(x)-\omega(p)-\omega(g)\\&= \omega^{\prime}(p)+\omega^{\prime}(g)-\omega(x)-\omega(p)\end{align*}$$
最后化简分别是 $2$ 和 $3$ 倍的 $\omega^{\prime}(x) - \omega(x)$。
因为我们每次操作结尾都会 Splay 一下,故 $\omega$ 满足 finger-search 的条件。
即 $\omega^{(i)}(x) = \omega^{(i - 1)^\prime}(x)$。
那么就有一个神奇的事情:
$$\begin{align*}t &= 3\sum_{i = 1} ^ n \omega^{(i)^\prime}(x) - \omega^{(i)}(x)\\&= 3\sum_{i = 1} ^ n \omega^{(i - 1)}(x) - \omega^{(i)}(x)\\&= 3\left(\omega^{(n)}(x) - \omega(x)\right)\\&= 3\log n\end{align*}$$
它抵消了!并根据定义化简为了 $\log$!【颤音】
故,时间复杂度:
$$\begin{align*}\mathcal{O}\left(\sum t\right) = \mathcal{O}\left( \sum 3 \log n \right) = \mathcal{O}(m \log n)\end{align*}$$
$\square$
$dis(u, v) = k, dis(v, w) \ge 0,dis(u, w) \ge k$,而 $w$ 到它的子树存在一条不小于 $k$ 的链,显然 $w$ 的长链不会小于 $k$。:::
### 代码长链剖分一般用于解决树上 $k$ 级祖先问题,它以它的查询 $\mathcal{O}(1)$ 复杂度著称。
:::success[[P3369 【模板】普通平衡树](https://www.luogu.com.cn/problem/P3369)]jzp 曾经说过,这种问题想做到 $\mathcal{O}(1)$ 只能打表。不过先不管这些,先来看常规解法。
```cpp#include <bits/stdc++.h>// #define int long long#define pii pair<int, int>#define FRE(x) freopen(x ".in", "r", stdin), freopen(x ".out", "w", stdout)#define ALL(x) x.begin(), x.end()using namespace std;倍增,记 $f_{u, k}$ 为 $u$ 向上爬 $2^k$ 的祖先。
inline void cmax(int &x, int c) { x = max(x, c);}inline void cmin(int &x, int c) { x = min(x, c);}分析一下效率,我们发现第一次倍增就跳了超过 $\frac{k}{2}$ 的深度,相当优秀;不过后面的就不那么优秀了。
int _test_ = 1;择其善者,我们同样预处理一个倍增数组,不过每一次只跳一次,假设我们跳了 $2^d$ 步从 $u$ 到达了 $v$。
const int N = 1e5 + 5;根据引理 $4$,我们发现 $v$ 的链顶 $w$ 所在长链的长度大于 $2^d$,更直接的,大于 $k - 2^d$。
struct Splay { int rt, son[N][2], val[N], siz[N], fa[N], tot = 0; inline void pushup(int u) { siz[u] = siz[son[u][0]] + siz[son[u][1]] + 1; } inline int nwnode(int v) { val[++tot] = v; siz[tot]++; return tot; } inline bool get(int u) { return (son[fa[u]][1] == u); } inline void rotate(int u) { int d = get(u); int v = fa[u], w = fa[v]; fa[son[u][d ^ 1]] = v; son[v][d] = son[u][d ^ 1]; son[u][d ^ 1] = v; son[w][get(v)] = u; fa[v] = u; fa[u] = w; pushup(v), pushup(u); } inline void splay(int u, int F) { for (int f = fa[u]; f != F; rotate(u), f = fa[u]) if (fa[f] != F) rotate(get(f) == get(u) ? f : u); if (!F) rt = u; } inline int find(int v) { int p = rt; while (p) { if (val[p] == v) return p; if (val[p] > v) p = son[p][0]; else p = son[p][1]; } } inline void ins(int v) { int p = rt; while (p) { if (v <= val[p] && son[p][0]) siz[p]++, p = son[p][0]; else if (v > val[p] && son[p][1]) siz[p]++, p = son[p][1]; else break; } int g = nwnode(v); if (!rt) return rt = g, void(); fa[g] = p; siz[p]++; if (v <= val[p]) { son[p][0] = g; siz[son[p][0]]++; } else { son[p][1] = g; siz[son[p][1]]++; } splay(g, 0); } inline void del(int v) { int p = find(v); splay(p, 0); p = rt; if (son[p][0]) { int t = son[p][0]; while (t) { if (son[t][1]) t = son[t][1]; else break; } splay(t, p); fa[t] = 0; son[t][1] = son[p][1]; fa[son[p][1]] = t; rt = t; pushup(t); } else { fa[son[p][1]] = 0; rt = son[p][1]; } } inline int kth(int k) { int p = rt; while (p) { if (siz[son[p][0]] + 1 == k) return val[p]; if (siz[son[p][0]] >= k) p = son[p][0]; else k -= siz[son[p][0]] + 1, p = son[p][1]; } return val[p]; } inline int thk(int v) { int p = rt, ans = 0; while (p) { if (val[p] >= v) ans = p, p = son[p][0]; else if (val[p] < v) p = son[p][1]; } splay(ans, 0); return siz[son[ans][0]] + 1; } inline int pre(int v) { int p = rt, ans = 0; while (p) { if (val[p] >= v) p = son[p][0]; else if (val[p] < v) ans = p, p = son[p][1]; } return val[ans]; } inline int nxt(int v) { int p = rt, ans = 0; while (p) { if (val[p] > v) ans = p, p = son[p][0]; else if (val[p] <= v) p = son[p][1]; } return val[ans]; }} spy;也就是说 $u$ 的 $k$ 级祖先要么在 $v \rightarrow w$ 的路径上,要么在 $w$ 到 $w$ 的 $len_w$ 级祖先的路径上。
void init() {}我们只需要对于任意长链,都打一个长度为长链长度的表 $U_{k}$ 代表链顶的 $k$ 级祖先即可。
void clear() {}时间复杂度 $\mathcal{O}(n \log n) - \mathcal{O}(1)$。
void solve() { int q; cin >> q; while (q--) { int op, x; cin >> op >> x; if (op == 1) spy.ins(x); if (op == 2) spy.del(x); if (op == 3) cout << spy.thk(x) << "\n"; if (op == 4) cout << spy.kth(x) << "\n"; if (op == 5) cout << spy.pre(x) << "\n"; if (op == 6) cout << spy.nxt(x) << "\n"; }}- ~~广告~~拓展阅读:[树上 k 级祖先](https://www.luogu.com.cn/article/ukpadm4x)。
signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);// cin >> _test_; init(); while (_test_--) { clear(); solve(); } return 0;}```## $\bf{3.}$ 实链剖分
:::实剖用于解决动态树问题,但我们也简称它为 LCT(Link-Cut Tree)。但实链剖分不能称作动态树,动态树是一类问题。
## 实链剖分
实剖用于解决动态树(Link-Cut Tree,lct)问题,但我们也简称它为 LCT。
### 思想
类比重剖与长剖,我们也为每一个结点设计一个实儿子(当然每个结点只能有一个实儿子,这是默认的)。然而无论实儿子是谁都有可能被删掉,故最好的方法就是让实链可以动态变化。
我们考虑给所有的实链开一个深度 Splay(即中序遍历按深度从小到大排列),结点一一对应,虚边则对应在 Splay 上也连一条虚边(认父不认子)。这样就构成了一棵辅助树(Auxiliary Tree,aux tree),我们的操作就在这棵辅助树上进行。我们考虑给所有的实链开一个深度 Splay(即中序遍历按深度从小到大排列),结点一一对应,实链之间的边则在对应的 Splay 上的对应点也连一条虚边(认父不认子,即 `son[fa[u]] != u`)。这样就构成了一棵辅助树(Auxiliary Tree,aux tree),我们的操作就在这棵辅助树上进行。
上文说过,我们想让实链可以动态变化,所以来看一个重要的操作。
**Access**:定义为打通 $u$ 到根,把原树 $u$ 的祖先链上全部改成实链。7 行未改动直到 $u$ 与 $rt$ 在同一个实链上。
:::info[参考实现]```cppvoid access(int u) { void access(int u) { for (int v = 0; u; v = u, u = fa[u]) // 比较简便的写法 splay(u), son[u][1] = v, pushup(u); } splay(u), son[u][1] = v, pushup(u);}```:::
再来说说其他重要的操作。12 行未改动
:::info[参考实现]```cppvoid modify(int u) { // 不怕影响到其它实链,因为认父不认子,下传标记传不下去 swap(son[u][0], son[u][1]), tag[u] ^= 1; swap(son[u][0], son[u][1]), tag[u] ^= 1;}void mak(int u) { access(u), modify(u); } void mak(int u) { access(u), modify(u);}```:::
没了,具体见模板题代码注释。24 行未改动 #define ion bw(20) char i[ion], o[ion], *icl = i, *icr = i, *oc = o; char gc() { return (icl == icr && (icr = (icl = i) + fread(i, 1, ion, stdin), icl == icr)) ? EOF : *icl++; } void pc(char c) { if (oc - o == ion) fwrite(o, 1, ion, stdout), oc = o; *oc++ = c; } void rd(auto &x) { char c; int f = 1; x = 0; while (c = gc(), c < '0' || c > '9') if (c == '-') f = -1; while ('0' <= c && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = gc(); x *= f; } void pr(auto x) { int a[64], p = 0; if (x < 0) x = -x, pc('-'); do a[p++] = x % 10; while (x /= 10); while (p--) pc(a[p] + '0'); } void rd(auto &x) { char c; int f = 1; x = 0; while (c = gc(), c < '0' || c > '9') if (c == '-') f = -1; while ('0' <= c && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = gc(); x *= f; } void pr(auto x) { int a[64], p = 0; if (x < 0) x = -x, pc('-'); do a[p++] = x % 10; while (x /= 10); while (p--) pc(a[p] + '0'); } IO& operator>>(char &c) { return c = gc(), *this; } IO& operator<<(char c) { return pc(c), *this; } IO& operator<<(const char *c) { while (*c) pc(*c++); return *this; } IO& operator>>(auto &x) { return rd(x), *this; }23 行未改动 int son[N][2], siz[N], fa[N], val[N], xo[N], tag[N], tot = 0; #define dir(u) (son[fa[u]][1] == u) #define chk(u) (fa[u] == 0 || (son[fa[u]][0] != u && son[fa[u]][1] != u)) void modify(int u) { swap(son[u][0], son[u][1]), tag[u] ^= 1; } void up(int u) { siz[u] = siz[son[u][0]] + siz[son[u][1]] + 1; xo[u] = xo[son[u][0]] ^ xo[son[u][1]] ^ val[u]; void up(int u) { siz[u] = siz[son[u][0]] + siz[son[u][1]] + 1; xo[u] = xo[son[u][0]] ^ xo[son[u][1]] ^ val[u]; } void down(int u) { if (tag[u]) modify(son[u][0]), modify(son[u][1]), tag[u] = 0; void down(int u) { if (tag[u]) modify(son[u][0]), modify(son[u][1]), tag[u] = 0; } void push(int u) { // 从根一直下传到 u if (!chk(u)) push(fa[u]); down(u); if (!chk(u)) push(fa[u]); down(u); } void rotate(int u) { int v = fa[u], w = fa[v], d = dir(u); if (!chk(v)) son[w][dir(v)] = u; // 区别于正常 Splay,这里需要判 v 的虚实。3 行未改动 } void splay(int u) { push(u); for (int v = fa[u]; !irt(u); rotate(u), v = fa[u]) if (!chk(v)) // 同理,这里默认旋转到根 rotate(dir(u) == dir(v) ? v : u); } void access(int u) { for (int v = 0; u; v = u, u = fa[u]) splay(u), son[u][1] = v, up(u); } void mak(int u) { access(u), splay(u), modify(u); } } void access(int u) { for (int v = 0; u; v = u, u = fa[u]) splay(u), son[u][1] = v, up(u); } void mak(int u) { access(u), splay(u), modify(u); } int frt(int u) { // 查询结点 u 在原树上的根 access(u), splay(u); access(u), splay(u); while (son[u][0]) u = son[u][0], down(u); // 根深度最小所以直接全部找左儿子 splay(u); // 使其满足 finger-search return u; } return u; } void split(int u, int v) { // 把 u、v 转到相邻 mak(u); access(v); splay(v); access(v); splay(v); } // 此时所有 u 到 v 的路径信息就在 v 的子树里 void link(int u, int v) { // 连边 mak(u); // 换个根 if (frt(v) != u) fa[u] = v; // 判一下是否在同一棵树上 } void cut(int u, int v) { } void cut(int u, int v) { mak(u); // 换根 if (frt(v) == u && fa[v] == u && !son[v][0]) // 判一下 son[u][1] = fa[v] = 0, up(u); } son[u][1] = fa[v] = 0, up(u); } int que(int u, int v) { // 查询 return split(u, v), xo[v]; return split(u, v), xo[v]; } } void init() {} void clear() {}30 行未改动```
:::
### 时间复杂度证明:::success[[P6664 [清华集训 2016] 温暖会指引我们前行](https://www.luogu.com.cn/problem/P6664)]
不难发现,温度最低的尽量高,是一棵支持动态加边的最大生成树,用 lct 解决。
假设我们要连接 $(u, v)$,具体步骤为:
- 若 $u,v$ 不属于同一连通块,直接 link 即可。- 否则找到 $u \rightarrow v$ 温度最低的点,如果 $(u, v)$ 温度高于它,则 $(u, v)$ 更优,删掉那条边加入 $(u, v)$。否则不管。
一个常用的 trick,lct 不方便维护边权,可以在两个点之间建一个虚点转成点权。
```cpp#include <bits/stdc++.h>// #define fastio#define int long long#define bw(x) (1 << (x))#define eb emplace_back#define pii pair<int, int>#define inf (0x3f3f3f3f3f3f3f3f)#define F(x, v) for (auto x : (v))#define ALL(x) (x).begin(), (x).end()#define L(i, a, b) for (register int i = (a); i <= (b); ++i)#define R(i, a, b) for (register int i = (a); i >= (b); --i)#define FRE(i, o) freopen(i, "r", stdin), freopen(o, "w", stdout)#define debug(a) cerr << "\033[32m[DEBUG] " << #a << " = " << (a) << " at line " << __LINE__ << "\033[0m\n"using namespace std; bool bgmem;#ifdef fastiostruct IO { #define ion bw(20) char i[ion], o[ion], *icl = i, *icr = i, *oc = o; char gc() { return (icl == icr && (icr = (icl = i) + fread(i, 1, ion, stdin), icl == icr)) ? EOF : *icl++; } void pc(char c) { if (oc - o == ion) fwrite(o, 1, ion, stdout), oc = o; *oc++ = c; } void rd(auto &x) { char c; int f = 1; x = 0; while (c = gc(), c < '0' || c > '9') if (c == '-') f = -1; while ('0' <= c && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = gc(); x *= f; } void pr(auto x) { int a[64], p = 0; if (x < 0) x = -x, pc('-'); do a[p++] = x % 10; while (x /= 10); while (p--) pc(a[p] + '0'); } IO& operator>>(char &c) { return c = gc(), *this; } IO& operator<<(char c) { return pc(c), *this; } IO& operator<<(const char *c) { while (*c) pc(*c++); return *this; } IO& operator>>(auto &x) { return rd(x), *this; } IO& operator<<(auto x) { return pr(x), *this; }} io;#define cin io#define cout io#endiftemplate < const int P > class modint { public: int x; modint(int _ = 0) { x = (_ % P + P) % P; } explicit operator int() { return x; } friend modint operator+(modint x, modint y) { return x.x + y.x < P ? x.x + y.x : x.x + y.x - P; } friend modint operator-(modint x, modint y) { return x.x - y.x < 0 ? x.x - y.x + P : x.x - y.x; } friend modint operator*(modint x, modint y) { return modint((__int128)(x.x * y.x)); } modint& operator+=(modint x) { return (*this) = (*this) + x; } modint& operator-=(modint x) { return (*this) = (*this) - x; } modint& operator*=(modint x) { return (*this) = (*this) * x; }};inline int cmax(int& x, int c) { return x = max(x, c); }inline int cmin(int& x, int c) { return x = min(x, c); }int tes = 1, cas;namespace qwq { const int N = 4e5 + 5; int n, m, op, x, y, a[N], pos[N]; pii e[N]; namespace lct { int son[N][2], siz[N], fa[N], vlen[N], vtem[N], slen[N], stem[N], sid[N], tag[N], tot = 0; #define dir(u) (son[fa[u]][1] == u) #define irt(u) (fa[u] == 0 || (son[fa[u]][0] != u && son[fa[u]][1] != u)) void modify(int u) { swap(son[u][0], son[u][1]), tag[u] ^= 1; } void up(int u) { siz[u] = siz[son[u][0]] + siz[son[u][1]] + 1; slen[u] = slen[son[u][0]] + slen[son[u][1]] + vlen[u]; stem[u] = vtem[u], sid[u] = u; if (son[u][0] && stem[u] > stem[son[u][0]]) stem[u] = stem[son[u][0]], sid[u] = sid[son[u][0]]; if (son[u][1] && stem[u] > stem[son[u][1]]) stem[u] = stem[son[u][1]], sid[u] = sid[son[u][1]]; } void down(int u) { if (tag[u]) modify(son[u][0]), modify(son[u][1]), tag[u] = 0; } void all(int u) { if (!irt(u)) all(fa[u]); down(u); } void rotate(int u) { int v = fa[u], w = fa[v], d = dir(u); if (!irt(v)) son[w][dir(v)] = u; fa[u] = w, son[v][d] = son[u][d ^ 1]; if (son[u][d ^ 1]) fa[son[u][d ^ 1]] = v; son[u][d ^ 1] = v, fa[v] = u, up(v), up(u); } void splay(int u) { all(u); for (int v = fa[u]; !irt(u); rotate(u), v = fa[u]) if (!irt(v)) rotate(dir(u) == dir(v) ? v : u); } void access(int u) { for (int v = 0; u; v = u, u = fa[u]) splay(u), son[u][1] = v, up(u); } void mkrt(int u) { access(u), splay(u), modify(u); } int frt(int u) { access(u), splay(u); while (son[u][0]) u = son[u][0], down(u); splay(u); return u; } void split(int u, int v) { mkrt(u), access(v), splay(v); } void link(int u, int v) { mkrt(u); if (frt(v) != u) fa[u] = v; } void cut(int u, int v) { split(u, v); if (son[v][0] == u && !son[u][1]) son[v][0] = fa[u] = 0, up(v); } pii ask(int u, int v) { split(u, v); return make_pair(stem[v], sid[v]); } int length(int u, int v) { split(u, v); return slen[v]; } } void init() {} void clear() {} void solve() { cin >> n >> m; L(i, 1, n) lct::vtem[i] = lct::stem[i] = inf; lct::tot = n; L(_, 1, m) { string op; cin >> op; if (op == "find") { int id, u, v, t, l; cin >> id >> u >> v >> t >> l; u++, v++; pos[id] = ++lct::tot, e[lct::tot] = {u, v}; lct::vtem[lct::tot] = lct::stem[lct::tot] = t; lct::vlen[lct::tot] = lct::slen[lct::tot] = l; lct::sid[lct::tot] = lct::tot; if (lct::frt(u) != lct::frt(v)) lct::link(u, lct::tot), lct::link(lct::tot, v); else { auto [w, d] = lct::ask(u, v); if (w > t) continue; lct::cut(e[d].first, d), lct::cut(d, e[d].second); lct::link(u, lct::tot), lct::link(lct::tot, v); } } else if (op == "move") { int u, v; cin >> u >> v; u++, v++; if (lct::frt(u) != lct::frt(v)) { cout << "-1\n"; } else { cout << lct::length(u, v) << "\n"; } } else { int id, l; cin >> id >> l; lct::mkrt(pos[id]); lct::vlen[pos[id]] = l; lct::up(pos[id]); } } }} bool edmem; signed main() { // FRE("", "");#ifndef fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);#endif // cin >> tes; qwq::init(); while (++cas <= tes) qwq::clear(), qwq::solve();#ifdef fastio fwrite(io.o, 1, io.oc - io.o, stdout);#endif cerr << "time : " << (double)clock() / CLOCKS_PER_SEC * 1000 << "ms\n"; cerr << "memory: " << fabs(&edmem - &bgmem) / 1024 / 1024 << "mb\n"; return 0;}// I will never love You Know Who again. =(```
:::
## 参考文献
1. jzp 的课件(出于某种特殊原因无法给链接)2. [5t0_0r2 - 【学习笔记】重链剖分](https://www.cnblogs.com/5002-qwq/p/17999492)3. [OI wiki - 树上启发式合并](https://oi-wiki.org/graph/dsu-on-tree/)4. [OI wiki - Splay 树](https://oi-wiki.org/ds/splay/#)4. [OI wiki - Link Cut Tree](https://oi-wiki.org/ds/lct/)
## 鸣谢
感谢教练 jzp 的悉心教诲。
感谢 AI deepseek 对于 Splay 时间复杂度证明的解释。
感谢洛谷用户 [T7_Daniel](https://www.luogu.com.cn/user/903392) 与用户 [pies_0x](https://www.luogu.com.cn/user/964645) 的~~催更~~鼓励。感谢洛谷用户 [T7_Daniel](https://www.luogu.com.cn/user/903392) 与用户 [pies_0x](https://www.luogu.com.cn/user/964645) 的~~催更~~鼓励与提出的修改意见。