正文
增加 55 行删除 55 行
331 行未改动全部展开33233333433533633733833934034134234334434534645 行未改动39239339439539639739839940078 行未改动479480481482483484485486487488156 行未改动全部展开64564664764864965065165265365465565612 行未改动66967067167267367467567667767867968068124 行未改动70670770870971071171271371471523 行未改动7397407417427437447457467477487497507517527537547557567573 行未改动76176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779862 行未改动86186286386486586686786886987023 行未改动8948958968978988999009019029039 行未改动91391491591691791891992092192292392492592692792892993093193293393473 行未改动331 行未改动全部展开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; }156 行未改动全部展开直到 $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]; }}73 行未改动331 行未改动全部展开33233333433533633733833934034134234334434534645 行未改动39239339439539639739839940078 行未改动479480481482483484485486487488156 行未改动全部展开64564664764864965065165265365465565612 行未改动66967067167267367467567667767867968068124 行未改动70670770870971071171271371471523 行未改动7397407417427437447457467477487497507517527537547557567573 行未改动76176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779862 行未改动86186286386486586686786886987023 行未改动8948958968978988999009019029039 行未改动91391491591691791891992092192292392492592692792892993093193293393473 行未改动331 行未改动全部展开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; }156 行未改动全部展开直到 $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]; }}73 行未改动