过了一遍平衡树,把代码进行了标注,方便以后回顾。
学平衡树主要为了处理区间翻转问题,和LCT(今天牛客多校考到了。。先回顾一下前置芝士)
#include <bits/stdc++.h>
const int N = 100005;
struct Splay
{
int rt, tot, fa[N], ch[N][2], val[N], cnt[N], sz[N];
//sz[x]:x节点子树的大小;cnt[x]:与x节点权值相同的数的个数;val[x]:x节点的权值
inline void init()
{
rt=tot=0;
}
inline void maintain(int x) { sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + cnt[x]; }//更新节点x的sz
inline bool get(int x) { return x == ch[fa[x]][1]; }//x是否是其父亲节点的右儿子
inline void clear(int x) {ch[x][0] = ch[x][1] = fa[x] = val[x] = sz[x] = cnt[x] = 0;}//删除节点x
inline void rotate(int x) //把x与其父亲节点交换
{
int y = fa[x], z = fa[y], chk = get(x);
ch[y][chk] = ch[x][chk ^ 1]; //利用性质,进行更改节点
fa[ch[x][chk ^ 1]] = y;
ch[x][chk ^ 1] = y;
fa[y] = x;
fa[x] = z;
if (z) ch[z][y =