FHQ Treap树

Treap树简介

Treap是一个合成词,Treap=Tree+Heap,顾名思义,它是具有堆性质的树,它的本质还是BST(二叉查找树),但因为引入了随机的权值使其在概率期望上达到了 l o g 2 n log_2{n} log2n的复杂度。

Treap树的维护有旋转法和FHQ两种方式,FHQ是近年来逐渐流行的,其编码更加简单且可以解决区间翻转和区间移动等问题。

FHQ Treap树例题

P3391 【模板】文艺平衡树 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

#include <bits/stdc++.h>

using namespace std;
const int N = 1e5 + 10;
struct Node {
    int ls, rs;
    int pri; //权值
    int num;//储存值
    int size;//子树大小
    int lazy;
} t[N];
int cnt = 0,root=0;
void pushdown(int x){
    if(t[x].lazy){
        swap(t[x].ls,t[x].rs);
        t[t[x].ls].lazy^=1;
        t[t[x].rs].lazy^=1;
        t[x].lazy=0;
    }
}
void NewNode(int x) {
    ++cnt;
    t[cnt].ls = t[cnt].rs = t[cnt].lazy = 0;
    t[cnt].size = 1;
    t[cnt].num = x;
    t[cnt].pri = rand();
}
void Update(int x){
    t[x].size=t[t[x].ls].size+t[t[x].rs].size+1;
}
int Merge(int L,int R){
    if(L==0||R==0)
        return L+R;
    if(t[L].pri>t[R].pri) {
        pushdown(L);
        t[L].rs = Merge(t[L].rs, R);
        Update(L);
        return L;
    }
    else {
        pushdown(R);
        t[R].ls = Merge(L, t[R].ls);
        Update(R);
        return R;
    }

}
void inorder(int u){
    if(u==0)
        return;
    pushdown(u);
    inorder(t[u].ls);
    cout<< t[u].num << " ";
    inorder(t[u].rs);
}
void Split(int u,int x,int &L,int &R){
    if(u==0){
        L=R=0;
        return;
    }
    pushdown(u);

     if (t[t[u].ls].size+1<=x){
         L=u;
         Split(t[u].rs,x-(t[t[u].ls].size+1),t[L].rs,R);
     }
     else{
         R=u;
         Split(t[u].ls,x,L,t[R].ls);
     }
    Update(u);
}
int main() {
    srand(time(NULL));
    int m, n;
    cin >> m >> n;
    for(int i=1;i<=n;i++) {
        NewNode(i);
        root = Merge(root,cnt);
    }
    int l, r;
    int L,R,M;
    while (m--) {
        cin >> l >> r;
        Split(root,r,L,R);
        Split(L,l-1,L,M);
        t[M].lazy^=1;
        root= Merge(Merge(L,M),R);
    }
    inorder(root);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

辰宝IWZ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值