正文
增加 60 行删除 352 行
331 行未改动全部展开33233333433533633733833934034134234334434534645 行未改动39239339439539639739839940078 行未改动479480481482483484485486487488132 行未改动全部展开6216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229237 行未改动93193293393493593693793893994094194212 行未改动95595695795895996096196296396496596696724 行未改动9929939949959969979989991000100123 行未改动10251026102710281029103010311032103310341035103610371038103910401041104210433 行未改动1047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108462 行未改动114711481149115011511152115311541155115623 行未改动11801181118211831184118511861187118811899 行未改动119912001201120212031204120512061207120812091210121112121213121412151216121712181219122057 行未改动1278127912801281128212831284128512861287128812891290129112921293129412951296129712981299331 行未改动全部展开int _test_ = 1, cas;namespace zrh {const int N = 1e5 + 5;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;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'); }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; }132 行未改动全部展开时间复杂度 $\mathcal{O}(n \log n) - \mathcal{O}(1)$。- ~~广告~~拓展阅读:[树上 k 级祖先](https://www.luogu.com.cn/article/ukpadm4x)。## $\bf{2 \frac{1}{2}}.$ Splay 的复杂度证明看到后面就知道为什么要插个这个了。### 定义Splay 是一种旋转平衡树,旨在通过不断旋转保证树的平衡。**因为本文的重点不是 Splay 所以不做多余的解释。**### 时间复杂度Splay 的时间复杂度是严格 $\mathcal{O}(n \log n)$,常数大于 SGT,略大于无旋 Treap。另外由于 Splay 的复杂度均摊,故不支持可持久化。:::info[不严谨的 proof]这很难呐,不想看严谨证明就用一些不那么严谨的语言吧。打个比方,你可以理解平衡树的根是器材室的门。操作就是去找器材。每次操作我们都在结尾加上了 `splay(x)` 这样的语句。这就类似于你使用器材后把它放在了门口。两种情况:若这个“器材”很常用:- 会在第一次使用的时候被提到门口。- 每次取用的成本都很低。若这只是一个冷门的“器材”:- 它在提到门口的同时“裹挟”了部分其他并不是那么冷门的“器材”上来,使得它们被优化了一小部分。整体看还是更优秀。整体看很优秀,但每次肯定有成本,不可能到 $\mathcal{O}(n)$,所以大概就是 $\mathcal{O}(n \log n)$(什么鬼)。::::::info[比较严谨的 proof]{open}看不懂的话只能看详细证明了。其实是抄的 OI wiki,加了一点自己的理解。不过建议还是去看 OI wiki,链接位于参考文献。定义一个结点 $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$:::### 代码:::success[[P3369 【模板】普通平衡树](https://www.luogu.com.cn/problem/P3369)]```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;inline void cmax(int &x, int c) {x = max(x, c);}inline void cmin(int &x, int c) {x = min(x, c);}int _test_ = 1;const int N = 1e5 + 5;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;void init() {}void clear() {}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";}}signed main() {ios::sync_with_stdio(0);cin.tie(0), cout.tie(0);// cin >> _test_;init();while (_test_--) {clear();solve();}return 0;}```:::## 实链剖分实剖用于解决动态树(Link-Cut Tree,lct)问题,但我们也简称它为 LCT。### 思想类比重剖与长剖,我们也为每一个结点设计一个实儿子(当然每个结点只能有一个实儿子,这是默认的)。然而无论实儿子是谁都有可能被删掉,故最好的方法就是让实链可以动态变化。我们考虑给所有的实链开一个深度 Splay(即中序遍历按深度从小到大排列),结点一一对应,虚边则对应在 Splay 上也连一条虚边(认父不认子)。这样就构成了一棵辅助树(Auxiliary Tree,aux tree),我们的操作就在这棵辅助树上进行。上文说过,我们想让实链可以动态变化,所以来看一个重要的操作。**Access**:定义为打通 $u$ 到根,把原树 $u$ 的祖先链上全部改成实链。7 行未改动直到 $u$ 与 $rt$ 在同一个实链上。:::info[参考实现]```cppvoid access(int u) {for (int v = 0; u; v = u, u = fa[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;}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'); }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 down(int u) {if (tag[u]) modify(son[u][0]), modify(son[u][1]), tag[u] = 0;}void push(int u) { // 从根一直下传到 uif (!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);}int frt(int u) { // 查询结点 u 在原树上的根access(u), splay(u);while (son[u][0]) u = son[u][0], down(u); // 根深度最小所以直接全部找左儿子splay(u); // 使其满足 finger-searchreturn u;}void split(int u, int v) { // 把 u、v 转到相邻mak(u);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) {mak(u); // 换根if (frt(v) == u && fa[v] == u && !son[v][0]) // 判一下son[u][1] = fa[v] = 0, up(u);}int que(int u, int v) { // 查询return split(u, v), xo[v];}}void init() {}void clear() {}62 行未改动#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; }23 行未改动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]];9 行未改动} 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]; }}57 行未改动```:::### 时间复杂度证明## 参考文献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/#)5. [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) 的~~催更~~鼓励。331 行未改动全部展开33233333433533633733833934034134234334434534645 行未改动39239339439539639739839940078 行未改动479480481482483484485486487488132 行未改动全部展开6216226236246256266276286296306316326336346356366377 行未改动64564664764864965065165265365465565612 行未改动66967067167267367467567667767867968068124 行未改动70670770870971071171271371471523 行未改动7397407417427437447457467477487497507517527537547557567573 行未改动76176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779862 行未改动86186286386486586686786886987023 行未改动8948958968978988999009019029039 行未改动91391491591691791891992092192292392492592692792892993093193293393457 行未改动99299399499599699799899910001001100210031004100510061007331 行未改动全部展开int _test_ = 1, cas;namespace zrh {const int N = 1e5 + 5;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;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'); }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; }132 行未改动全部展开时间复杂度 $\mathcal{O}(n \log n) - \mathcal{O}(1)$。- ~~广告~~拓展阅读:[树上 k 级祖先](https://www.luogu.com.cn/article/ukpadm4x)。## $\bf{3.}$ 实链剖分实剖用于解决动态树问题,但我们也简称它为 LCT(Link-Cut Tree)。但实链剖分不能称作动态树,动态树是一类问题。### 思想类比重剖与长剖,我们也为每一个结点设计一个实儿子(当然每个结点只能有一个实儿子,这是默认的)。然而无论实儿子是谁都有可能被删掉,故最好的方法就是让实链可以动态变化。我们考虑给所有的实链开一个深度 Splay(即中序遍历按深度从小到大排列),结点一一对应,实链之间的边则在对应的 Splay 上的对应点也连一条虚边(认父不认子,即 `son[fa[u]] != u`)。这样就构成了一棵辅助树(Auxiliary Tree,aux tree),我们的操作就在这棵辅助树上进行。上文说过,我们想让实链可以动态变化,所以来看一个重要的操作。**Access**:定义为打通 $u$ 到根,把原树 $u$ 的祖先链上全部改成实链。7 行未改动直到 $u$ 与 $rt$ 在同一个实链上。:::info[参考实现]```cppvoid access(int u) {for (int v = 0; u; v = u, u = fa[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;}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'); }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 down(int u) {if (tag[u]) modify(son[u][0]), modify(son[u][1]), tag[u] = 0;}void push(int u) { // 从根一直下传到 uif (!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);}int frt(int u) { // 查询结点 u 在原树上的根access(u), splay(u);while (son[u][0]) u = son[u][0], down(u); // 根深度最小所以直接全部找左儿子splay(u); // 使其满足 finger-searchreturn u;}void split(int u, int v) { // 把 u、v 转到相邻mak(u);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) {mak(u); // 换根if (frt(v) == u && fa[v] == u && !son[v][0]) // 判一下son[u][1] = fa[v] = 0, up(u);}int que(int u, int v) { // 查询return split(u, v), xo[v];}}void init() {}void clear() {}62 行未改动#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; }23 行未改动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]];9 行未改动} 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]; }}57 行未改动```:::## 参考文献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 - Link Cut Tree](https://oi-wiki.org/ds/lct/)## 鸣谢感谢教练 jzp 的悉心教诲。感谢洛谷用户 [T7_Daniel](https://www.luogu.com.cn/user/903392) 与用户 [pies_0x](https://www.luogu.com.cn/user/964645) 的~~催更~~鼓励与提出的修改意见。