LCT 板子
namespace LCT {
int f[N], c[N][2], s[N], st[N];
bool r[N];
inline bool nroot(int);
inline void pushup(int);
inline void pushr(int);
inline void pushdown(int);
inline void rotate(int);
inline void splay(int);
inline void access(int);
inline void makeroot(int);
inline int findroot(int);
inline void split(int, int); // split(x, y) 表示取出 x,y 的路径
inline void link(int, int);
inline void cut(int, int);
}
using LCT::s;
namespace LCT {
#define rc c[x][1]
#define lc c[x][0]
inline bool nroot(int x) { return c[f[x]][0] == x || c[f[x]][1] == x; }
inline void pushup(int x) { s[x] = s[lc] + s[rc]+????; } // s 数组是记录结果。
inline void pushr(int x) {int t = lc; lc = rc; rc = t; r[x] ^= 1;}
inline void pushdown(int x) {
if (r[x]) {
if (lc) pushr(lc);
if (rc) pushr(rc);
r[x] = 0;
}
}
inline void rotate(int x) {
int y = f[x], z = f[y], k = c[y][1] == x, w = c[x][!k];
if (nroot(y)) c[z][c[z][1] == y] = x; c[x][!k] = y; c[y][k] = w;
if (w)f[w] = y; f[y] = x; f[x] = z;
pushup(y);
}
inline void splay(int x) {
int y = x, z = 0;
st[++z] = y;
while (nroot(y)) st[++z] = y = f[y];
while (z) pushdown(st[z--]);
while (nroot(x)) {
y = f[x]; z = f[y];
if (nroot(y)) rotate((c[y][0] == x) ^ (c[z][0] == y) ? x : y);
rotate(x);
}
pushup(x);
}
inline void access(int x) {
for (int y = 0; x; x = f[y = x])
splay(x), rc = y, pushup(x);
}
inline void makeroot(int x) { access(x); splay(x); pushr(x); }
inline int findroot(int x) {
access(x); splay(x);
while (lc) pushdown(x), x = lc;
splay(x);
return x;
}
inline void split(int x, int y) { makeroot(x); access(y); splay(y); }
inline void link(int x, int y) {
makeroot(x);
if (findroot(y) != x)f[x] = y;
}
inline void cut(int x, int y) {
makeroot(x);
if (findroot(y) == x && f[y] == x && !c[y][0]) {
f[y] = c[x][1] = 0;
pushup(x);
}
}
}
作者:Aonynation
出处:https://www.cnblogs.com/Oier-GGG/p/16462568.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架