【数据结构】splay 树 学习笔记

· · 算法·理论

前言

解锁新科技:{\color{red}\text{Splay 树}}。

可以进一步解锁的科技:

P6136 【模板】普通平衡树(数据加强版)

还是拿这个题当板子比较好。

如何搓一棵 Splay [1] [2]:

step 1:

class Splay{
};

step 2:

一棵 Splay 树由以下几部分构成:

  1. int root,指向根。
  2. int tot,结点个数。
  3. Node,表示结点集。

一个结点 Node 由以下几部分构成:

  1. fa
  2. ch[2]
  3. val
  4. cnt
  5. siz
  6. 各种 tag。

为了方便,先用 C 风格写,最后可以改成 oop 的。

int fa[_], son[_][2], siz[_], cnt[_], val[_];
int root, tot;

step 3:

几个重要的函数:

  1. void maintain(int x);
  2. int get(int x);
  3. void clear(int x);
void maintain(int x){
    siz[x] = siz[son[x][0]] + siz[son[x][1]] + cnt[x];
}
int get(int x){
    return son[fa[x]][1] == x;
}
void clear(int x){
    fa[x] = son[x][0] = son[x][1] = siz[x] = cnt[x] = val[x] = 0;
}

step 4:

旋转某个结点至父节点,void rotate(int x);

void rotate(int x){
    int f = fa[x], g = fa[f];
    int chk = get(x);
    son[f][chk] = son[x][chk ^ 1];
    if(son[x][chk^1])
    // 注意这里,一定要判断子节点非空,否则可能修改 0 号节点
        fa[son[x][chk ^ 1]] = f;
    son[x][chk ^ 1] = f;
    fa[f] = x, fa[x] = g;
    if(g)
        son[g][f == son[g][1]] = x;
    maintain(f), maintain(x);
    // 先 f 再 x
}

step 5:

splay 操作,将 x 强制旋转至根。

一个 splay 操作为对 x 进行一系列 splay 步骤。

  1. zig/zag 操作,在当 x.fa 为根时进行。
  2. zig-zig/zag-zag 操作,在当 x,x.fa,x.fa.fa 在同一条直线上时进行,先旋转 x.fa,再旋转 x。
  3. zig-zag/zag-zig 操作,当不共线时进行,旋转两次 x。

代码很短,但是是 Splay 树的核心,只有每次操作后都 Splay 一次才能保证复杂度正确。

void splay(int x){
    for (int f = fa[x]; (f = fa[x]); rotate(x))
    if(fa[f])
        rotate(get(x) == get(f) ? f : x);
    root = x;
}

具体证明可以去 OI-wiki

step 7:

insert 操作,插入元素。

  1. 如果树为空,插入根,退出。
  2. 如果当前节点权值等于 val,直接插入,splay 到根。
  3. 否则按照 bst 的性质向下找,发现空节点插入。

注意变量名代表的含义,不要搞混了(

void insert(int x){
    if(!root){
        root = ++tot;
        siz[tot] = cnt[tot] = 1;
        val[tot] = x;
        return;
    }
    int k = root, f = 0;
    for (;;){
        if(x==val[k]){
            cnt[k]++;
            maintain(k), maintain(f);
            splay(k);
            return;
        }
        f = k, k = son[k][val[k] < x];
        if(!k){
            tot++;
            fa[tot] = f;
            siz[tot] = cnt[tot] = 1;
            son[f][val[f] < x] = tot;
            val[tot] = x;
            maintain(f);
            splay(tot);
            return;
        }
    }
}

step 8:

查找 x 的 rank。

先插入 x,再 rank,最后删除。

若保证 x 一定在集合中可直接 rank。

int rnk(int vl){
    int x = root, ans = 0;
    for (;;){
        if(vl<val[x])
            x = son[x][0];
        else{
            ans += siz[son[x][0]];
            if(vl==val[x]){
                splay(x);
                return ans + 1;
            }
            ans += cnt[x];
            x = son[x][1];
        }
    }
}

step 9:

查询第 k 大的数,注意 k 不能大于 root.siz。

注意,不论是什么操作,最后都要 splay 一下。

int kth(int k) {
    int x = root;
    for (;;) {
        if (son[x][0] && k <= siz[son[x][0]])
            x = son[x][0];
        else {
            int tmp = siz[son[x][0]] + cnt[x];
            if (k <= tmp) {
                splay(x);
                return val[x];
            }
            k -= tmp, x = son[x][1];
        }
    }
}

step 10:

查找 x 的前驱后继。

先插入 x,然后 x 在根的位置,从左子树一直向右走/右子树一直向左走,最后删除。

这个也要 splay 的,不然复杂度不正确。

int pre() {
    int x = son[root][0];
    while (son[x][1])
        x = son[x][1];
    splay(x);
    return x;
}
int nxt() {
    int x = son[root][1];
    while (son[x][0])
        x = son[x][0];
    splay(x);
    return x;
}

step 11:

删除值为 x 的结点,需保证 x 在集合内。

先旋转值为 x 的结点至根。

这个分类讨论一下就好,最后虽然看起来没有 splay 但其实在 rnk 的时候就已经 splay 过了。

void del(int x) {
    rnk(x);
    if (cnt[root] > 1) {
        cnt[root]--;
        maintain(root);
        return;
    }
    if (!son[root][0] && !son[root][1]) {
        clear(root), root = 0;
        return;
    }
    if (!son[root][0]) {
        int k = root;
        root = son[root][1];
        fa[root] = 0;
        clear(k);
        return;
    }
    if (!son[root][1]) {
        int k = root;
        root = son[root][0];
        fa[root] = 0;
        clear(k);
        return;
    }
    int k = root, lf = pre();
    splay(lf);
    son[root][1] = son[k][1];
    if (son[k][1])
        fa[son[k][1]] = root;
    clear(k);
    maintain(root);
}

step 12:

有人觉得然后就没有了,其实,,

你还要调试

递归输出序列,调试帮手:

void print(int pos) {
    if (!pos)
        return;
    print(son[pos][0]);
    for (int i = 1; i <= cnt[pos]; i++)
        printf("%d ", val[pos]);
    print(son[pos][1]);
}
void print() {
    print(root);
    putchar('\n');
}

这种风格似乎码量很大,但也比 lrj 的指针好调 QwQ

这样就把基本操作讲完了~

P6136 code

完整版代码在这里:

code

ext:

P3391 【模板】文艺平衡树

参考资料:[3]

Splay 的强大之处不止在于她能够进行一系列的平衡树操作,还在于一个很重要的扩展应用:

打标记

能够打标记,就意味着 Splay 能够像线段树那样支持一些区间操作,尤其是...

这个操作,线段树是办不到的,因为结点代表的是一段区间,不能翻转。 但 `Splay` 可以做到(还有一个是著名的 `fhq-treap`,听别人说用起来很香)。 具体操作: 由于要维护序列上的值,那就需要将下标当做 `key`,而不是平时用的 `val`。 建树可以一个一个 `insert`,也可以像线段树那样递归二分建。 翻转就是,每个节点一个 `tag`,标记是否需要翻转。 对于一个区间翻转操作 $[l_i,r_i]$,先将在第 $l_i-1$ 名的结点旋转至根,然后将第 $r_i+1$ 名的结点旋转至根的下方。 然后构成这样的形状: ```plain o l-1 / \ / \ ... o r+1 / \ / \ [l,r] o ... / \ / \ ... ``` 这样,就在 $[l,r]$ 的子树的根上打上标记就行了。 下传标记,无论何时,遍历到的结点的标记都要下传。 ```cpp void pushdown(int x) { if (x && t[x].tag) { t[t[x].ch[0]].tag ^= 1; t[t[x].ch[1]].tag ^= 1; swap(t[x].ch[0], t[x].ch[1]); t[x].tag = 0; } } ``` 但至于为什么 `rotate` 的时候标记还要下传,不太明白这个原理,如果 `x` 的兄弟结点是空结点,而 `fa[x]` 又有标记要下传,就会将空节点旋转到上面。 但实际上好像不会碰到这种情况,( 还是把代码贴出来,[code](https://www.luogu.com.cn/record/96421423) 另外听评论区有说要先 `pushdown(f)`,再 `pushdown(x)`,但这里顺序反了还能过?? 还是说没有影响?? ___ 另附,splay 实现区间加,[code](https://www.luogu.com.cn/record/96799880) 区间加,区间乘,询问区间和,[code](https://www.luogu.com.cn/record/96799144) ### 参考资料 \[1\] [https://www.luogu.org/blog/pks-LOVING/more-senior...](https://www.luogu.org/blog/pks-LOVING/more-senior-data-structure-te-bie-qian-di-qian-tan-splay) \[2\] [https://oi-wiki.org/ds/splay/](https://oi-wiki.org/ds/splay/) \[3\] [https://www.luogu.com.cn/blog/pks-LOVING/splay-chu-...](https://www.luogu.com.cn/blog/pks-LOVING/splay-chu-li-ou-jian-cao-zuo-fan-zhuai-cao-zuo-reverse)