数据结构总结——FHQ Treap 2

· · 算法·理论

平衡树应该是现在最强大的数据结构。

FHQ Treap 的排名分裂

原理

在实际应用中,维护二叉搜索树的平衡性一般用 \text{STL} 的红黑树,而我们手写的平衡树一般用于求解区间问题。这时,需要一种新的分裂——排名分裂。普通分裂按权值,排名分裂用排名,两者都利用了平衡树的单调性。排名分裂的本质是二叉搜索树求第 k 小的元素,非常简单,直接讲解原理。

设当前节点为 x,前 k 名放入左 \text{Treap},左子树大小为 size。

不断递归上述过程,直到到达叶子节点。

代码:

FHQ Treap 排名分裂的代码巧妙利用指针,实现了接树边的操作,如下:

void rank_split_treap(int root,int k,int &rootl,int &rootr)
{
    if(root == 0)//递归边界——当前是空节点
    {
        rootl = rootr = 0;
        return;
    }
    if(k <= treap[treap[root].lson].siz)//说明该节点应该属于右 Treap
    {
        rootr = root;
        rank_split_treap(treap[root].lson,k,rootl,treap[root].lson);//递归进入左子树
    }else//说明该节点应该属于左 Treap
    {
        rootl = root;
        rank_split_treap(treap[root].rson,k-treap[treap[root].lson].siz-1,treap[root].rson,rootr);//递归进入右子树
    }
    push_up(root);
    return;
}

例题

例题1 [NOI2004] 郁闷的出纳员

思路

这些操作直指一个在线数据结构——平衡树。

I 操作和 F 操作都是平衡树的标准操作之一;A 操作可以用懒标记,整棵树用一个即可。但 S 操作不一样,它可能让一个或多个员工气愤地离开公司,该怎么处理呢?

注意到所有员工的工资下界都是一样的,如果有人会离开公司,他们必定是平衡树映射序列的前缀。基于这一点,可以用普通分裂找到并剥离该前缀,完成员工离职操作。

代码

#include <bits/stdc++.h>
using namespace std;

struct node {int val,key,lson,rson,siz;} treap[100005];
int T,k,treap_root,add_val = 0,ans = 0;
stack<int>memory;

void push_up(int pos)
{
    treap[pos].siz = treap[treap[pos].lson].siz+1+treap[treap[pos].rson].siz;
    return;
}

void split_treap(int root,int x,int &rootl,int &rootr)
{
    if(root == 0)
    {
        rootl = rootr = 0;
        return;
    }
    if(treap[root].val <= x)
    {
        rootl = root;
        split_treap(treap[root].rson,x,treap[root].rson,rootr);
    }else
    {
        rootr = root;
        split_treap(treap[root].lson,x,rootl,treap[root].lson);
    }
    push_up(root);
    return;
}

int merge_treap(int rootl,int rootr)
{
    if(rootl == 0 || rootr == 0) return rootl^rootr;
    if(treap[rootl].key <= treap[rootr].key)
    {
        treap[rootl].rson = merge_treap(treap[rootl].rson,rootr);
        push_up(rootl);
        return rootl;
    }else
    {
        treap[rootr].lson = merge_treap(rootl,treap[rootr].lson);
        push_up(rootr);
        return rootr;
    }
    return 0;
}

void add_node(int &root,int val)
{
    int rootl,rootr,new_node = memory.top();
    memory.pop();
    treap[new_node] = {val,rand(),0,0,1};
    split_treap(root,val,rootl,rootr);
    root = merge_treap(merge_treap(rootl,new_node),rootr);
    return;
}

int kth(int root,int k)
{
    if(root == 0) return -2e9;
    if(k <= treap[treap[root].lson].siz) return kth(treap[root].lson,k);
    else if(k == treap[treap[root].lson].siz+1) return treap[root].val;
    return kth(treap[root].rson,k-treap[treap[root].lson].siz-1); 
}

void init(void)
{
    treap_root = 0;
    for(int i = 1e5;i >= 1;i--) memory.push(i);
    srand(time(0));
    return;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    init();
    cin >> T >> k;
    while(T--)
    {
        char op;
        int x;
        cin >> op >> x;
        if(op == 'I')
        {
            if(x >= k) add_node(treap_root,x-add_val);
        }else if(op == 'A') add_val += x;
        else if(op == 'S')
        {
            int rootl,rootr;
            add_val -= x;
            split_treap(treap_root,k-add_val-1,rootl,rootr);
            ans += treap[rootl].siz;
            treap_root = rootr;
        }else if(op == 'F')
        {
            if(treap[treap_root].siz >= x) cout << kth(treap_root,treap[treap_root].siz-x+1)+add_val << '\n';
            else cout << "-1\n";
        }
    }
    cout << ans;
    return 0;
}

例题2 [][HNOI2012] 永无乡

思路

这些操作直指一个在线数据结构——平衡树。

Q 操作明显是查排名。B 操作看起来像合并,但是它不保证操作的两棵树满足 FHQ Treap 合并的前提条件,那该怎么进行合并呢?

用类似于启发式合并的思路,每次合并时,暴力地将较小树的节点插入较大树。这个方法看起来很暴力,复杂度很高,一个节点最多被插入几次呢?

如果一个节点进行一次插入,那么它所在的树的节点必须大于或等于另一树的节点个数。合并完成后,它所在的树节点个数至少会达到之前的两倍,每合并一次,翻一倍。而 n 个元素之间只能进行 n-1 次有效合并。算上插入节点,时间复杂度为 O(n\log^2 n),绰绰有余。

代码

#include <bits/stdc++.h>
using namespace std;

struct node {int val,key,lson,rson,siz;} treap[100005];
int n,m,T,dsu[100005];

int find_on_dsu(int x)
{
    if(dsu[x] == x) return x;
    return dsu[x] = find_on_dsu(dsu[x]);
}

void push_up(int pos)
{
    treap[pos].siz = treap[treap[pos].lson].siz+1+treap[treap[pos].rson].siz;
    return;
}

void split_treap(int root,int x,int &rootl,int &rootr)
{
    if(root == 0)
    {
        rootl = rootr = 0;
        return;
    }
    if(treap[root].val <= x)
    {
        rootl = root;
        split_treap(treap[root].rson,x,treap[root].rson,rootr);
    }else
    {
        rootr = root;
        split_treap(treap[root].lson,x,rootl,treap[root].lson);
    }
    push_up(root);
    return;
}

int merge_treap(int rootl,int rootr)
{
    if(rootl == 0 || rootr == 0) return rootl^rootr;
    if(treap[rootl].key <= treap[rootr].key)
    {
        treap[rootl].rson = merge_treap(treap[rootl].rson,rootr);
        push_up(rootl);
        return rootl;
    }else
    {
        treap[rootr].lson = merge_treap(rootl,treap[rootr].lson);
        push_up(rootr);
        return rootr;
    }
    return 0;
}

void add_node(int &root,int new_node)
{
    int rootl,rootr;
    treap[new_node].lson = treap[new_node].rson = 0;
    treap[new_node].siz = 1;
    split_treap(root,treap[new_node].val,rootl,rootr);
    root = merge_treap(merge_treap(rootl,new_node),rootr);
    return;
}

void dfs(int root,int &to_treap)
{
    if(root == 0) return;
    dfs(treap[root].lson,to_treap);
    dfs(treap[root].rson,to_treap);
    add_node(to_treap,root);
    return;
}

int kth(int root,int k)
{
    if(treap[root].siz < k) return -1;
    if(k <= treap[treap[root].lson].siz) return kth(treap[root].lson,k);
    else if(k == treap[treap[root].lson].siz+1) return root;
    return kth(treap[root].rson,k-treap[treap[root].lson].siz-1); 
}

void init(void)
{
    for(int i = 1;i <= n;i++) dsu[i] = i;
    srand(time(0));
    return;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    init();
    for(int i = 1;i <= n;i++)
    {
        int x;
        cin >> x;
        treap[i] = {x,rand(),0,0,1};
    }
    for(int i = 1;i <= m;i++)
    {
        int x,y;
        cin >> x >> y;
        x = find_on_dsu(x);
        y = find_on_dsu(y);
        if(x == y) continue;
        if(treap[x].siz < treap[y].siz) swap(x,y);
        int root = x;
        dfs(y,root);
        dsu[x] = dsu[y] = root;
        dsu[root] = root;
    }
    cin >> T;
    while(T--)
    {
        char op;
        int x,y;
        cin >> op >> x >> y;
        if(op == 'Q') cout << kth(find_on_dsu(x),y) << '\n';
        else if(op == 'B')
        {
            x = find_on_dsu(x);
            y = find_on_dsu(y);
            if(x == y) continue;
            if(treap[x].siz < treap[y].siz) swap(x,y);
            int root = x;
            dfs(y,root);
            dsu[x] = dsu[y] = root;
            dsu[root] = root;
        }
    }
    return 0;
}

例题3 【模板】文艺平衡树

思路

首先,将序列映射成一棵 Treap,用于翻转。

将一个序列整体翻转,就是不断将区间分成两部分,交换着两个区间的位置,再递归下去继续交换。这很好理解,因为翻转序列就是对于每个 1\le x \le n,原本在 x​ 前面的所有元素全部到它后面去;原本在 x 后面的所有元素全部到它前面去。上面的翻转操作依据的就是这个原理。

该怎么实现上面的翻转呢?如果每次都要递归下去交换,单次时间复杂度会达到 O((r-l)\log(r-l)),总复杂度会达到 O(n^2\log n),还不如暴力。区间修改,就要想到线段树。想到线段树,就要想到它的核心技术——懒标记。Treap 除了根节点外,访问一个节点之前,会访问所有它的直接和间接父节点,符合使用懒标记的前提条件。我们可以在需要整体旋转区间的对应 Treap 的根上打懒标记,就可以完成这一操作。

每次操作时将翻转区间对应的 Treap 用排名分裂剥离出来,打上懒标记后合并回去,分裂与合并时遇到懒标记就处理,像线段树一样。最后处理所有剩余的懒标记,按中序遍历输出。

代码

#include <bits/stdc++.h>
using namespace std;

struct node {int val,key,lson,rson,siz;bool rotate;} treap[100005];
int n,T,treap_root;

void push_up(int pos)
{
    treap[pos].siz = treap[treap[pos].lson].siz+1+treap[treap[pos].rson].siz;
    return;
}

void push_down(int pos)
{
    int lson = treap[pos].lson,rson = treap[pos].rson;
    if(treap[pos].rotate)
    {
        swap(treap[lson].lson,treap[lson].rson);
        swap(treap[rson].lson,treap[rson].rson);
        treap[lson].rotate ^= 1;
        treap[rson].rotate ^= 1;
        treap[pos].rotate = 0;
    }
    return;
}

void rank_split_treap(int root,int k,int &rootl,int &rootr)
{
    if(root == 0)
    {
        rootl = rootr = 0;
        return;
    }
    push_down(root);
    if(k <= treap[treap[root].lson].siz)
    {
        rootr = root;
        rank_split_treap(treap[root].lson,k,rootl,treap[root].lson);
    }else
    {
        rootl = root;
        rank_split_treap(treap[root].rson,k-treap[treap[root].lson].siz-1,treap[root].rson,rootr);
    }
    push_up(root);
    return;
}

int merge_treap(int rootl,int rootr)
{
    if(rootl == 0 || rootr == 0) return rootl^rootr;
    push_down(rootl);
    push_down(rootr);
    if(treap[rootl].key <= treap[rootr].key)
    {
        treap[rootl].rson = merge_treap(treap[rootl].rson,rootr);
        push_up(rootl);
        return rootl;
    }else
    {
        treap[rootr].lson = merge_treap(rootl,treap[rootr].lson);
        push_up(rootr);
        return rootr;
    }
    return 0;
}

void init(int &root)
{
    srand(time(0));
    root = 0;
    for(int i = 1;i <= n;i++)
    {
        treap[i] = {i,rand(),0,0,1,0};
        root = merge_treap(root,i);
    }
    return;
}

void rotate(int &root,int l,int r)
{
    int rootl,middle,rootr;
    rank_split_treap(root,l-1,rootl,rootr);
    rank_split_treap(rootr,r-l+1,middle,rootr);
    treap[middle].rotate ^= 1;
    swap(treap[middle].lson,treap[middle].rson);
    root = merge_treap(merge_treap(rootl,middle),rootr);
    return;
}

void output(int root)
{
    if(root == 0) return;
    push_down(root);
    output(treap[root].lson);
    cout << treap[root].val << ' ';
    output(treap[root].rson);
    return;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> T;
    init(treap_root);
    while(T--)
    {
        int l,r;
        cin >> l >> r;
        rotate(treap_root,l,r);
    }
    output(treap_root);
    return 0;
}