fhq_treap简单食用 一种不用旋转的平衡树

我们都知道 treap是tree+heap也就是树堆
那treap和splay他们都是比较好的平衡树 至于红黑树就不提了 代码量能雷死你
至于sbt听说性能挺优越的 但是没学
今天讲的是fhq_treap也就是非旋平衡树
我们知道为什么splay和treap需要旋转呢 因为如果你遇到一条链的查询 那么你的时间复杂度就是 O(链的长度) 但是树在某些情况下会退化成链 那么操作就会退化复杂度
所以splay和treap通过旋转尽可能维持树的结构
但是fhq大佬提出了不用旋转的数据结构
我们主要是核心是 split 和 merge
split就是分离树 代码如下

void split(int x,int k,int &a,int &b)
    {
   
        if(!x) {
   a = b = 0;return;}
        int ls = t[x].ch[0],rs = t[x].ch[1];
        if(k<=t[ls].size) b = x,split(ls,k,a,ls);
        else a = x,split(rs,k-t[ls].size-1,rs,b);
    }

这是前k个分离处理 意思和线段树差不多 如果在左子树 那么去左子树里面找 也就是 x变成ls
b = x 也就是代表右子树的根此时就是 x 左子树还没有找到根 必须去左子树里面找根 所以第三第四个参是 a 和 ls
下面同理
然后你拆了以后要Merge回来

int Merge(int x,int y)
    {
   
        if(x==0||y==0) return x+y;
        if(t[x].rd<t[y].rd)
        {
   
            t[x].ch[1] = Merge(t[x].ch[1],y);
            up(x);return x;
        }
        else
        {
   
            t[y].ch[0] = Merge(x,t[y].ch[0]);
            up(y);return y;
        }
    }

注意这里是rd 核心思想就是我不旋转 我通过随机的大小来决定我是左连还是右连
但是为了维护中序遍历的原则 我们一定是把x右子树插y x插y的左子树
然后这样就可以合并了
所以fhq_treap就是通过这两个操作实现常数小 且像一个平衡的树的结构的平衡树

下面有几道例题
P3369
敲板子即能过 没设srand也能过 服了

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
#include <ctime>
//#pragma comment(linker, "/STACK:10240000,10240000")
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
typedef unsigned long long ull;
const ull hash1 = 201326611;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){
   return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){
   int ans=1;while(b){
   if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){
   return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){
   if(!b) {
   d = a;x = 1;y=0;}else{
   exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
//ull ha[MAX_N],pp[MAX_N];

inline int read()
{
   
    int date = 0,m = 1; char ch = 0;
    while(ch!='-'&&(ch<'0'|ch>'9'))ch = getchar();
    if(ch=='-'){
   m = -1; ch = getchar();}
    while(ch>='0' && ch<='9')
    {
   
        date = date*10+ch-'0';
        ch = getchar();
    }return date*m;
}

/*namespace sgt
{
    #define mid ((l+r)>>1)

    #undef mid
}*/

/*int root[MAX_N],cnt,sz;
namespace hjt
{
    #define mid ((l+r)>>1)
    struct node{int l,r,maxx;}T[MAX_N*40];

    #undef mid
}*/
const int MAX_N = 100025;
int root,r1,r2,r3,r4,cnt = 0;
namespace fhq
{
   
    struct treap
    {
   
        int ch[2],size,rd,val;
    }t[MAX_N];
    void up(int rt) {
   t[rt].size = t[t[rt].ch[0]].size+t[t[rt].ch[1]].size+1;}
    void split(int x,int val,int &a,int &b)
    {
   
        if(!x) {
   a = b = 0;return;}
        if(t[x].val>val) b = x,split(t[x].ch[0],val,a,t[x].ch[0]);
        else a = x,split(t[x].ch[1],val,t[x].ch[1],b);
        up(x);
    }
    int Merge(int x,int y)
    {
   
        if(x==0||y==0) return x + y;
        if(t[x].rd < t[y].rd)
        {
   
            t[x].ch[1] = Merge(t[x].ch[1],y);
            up(x);return x;
        }
        else
        {
   
            t[y].ch[0] = Merge(x,t[y].ch[0]);
            up(y);return y;
        }
    }
    int newnode(int val)
    {
   
        t[++cnt].val = val;t[cnt].rd = rand();t[cnt].size =1;
        return cnt;
    }
    void Insert(int val)
    {
   
        split(root,val,r1,r2);
        root = Merge(r1,Merge(newnode(val),r2));
    }
    void delet(int val)
    {
   
        r1 = r2 = r3 = 0;split(root,val,r1,r3);
        split(r1,val-1,r1,r2);
        r2 = Merge(t[r2].ch[0
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值