无旋转Treap 学习笔记

参考和学习:https://www.cnblogs.com/BCOI/p/9072444.html

对了,参考的blog中删点他应该写错了。

这个是按照val 进行切分的treap

写了模板普通平衡树

#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    <bitset>
#include    <cctype>
#include    <cstdio>
#include    <string>
#include    <vector>
#include     <stack>
#include     <cmath>
#include     <queue>
#include      <list>
#include       <map>
#include       <set>
#include   <cassert>

using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000")  //c++
// #pragma GCC diagnostic error "-std=c++11"
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3)

#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue



typedef long long ll;
typedef unsigned long long ull;

typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3;

//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n'

#define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
#define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
#define max3(a,b,c) max(max(a,b), c);
//priority_queue<int ,vector<int>, greater<int> >que;

const ll mos = 0x7FFFFFFF;  //2147483647
const ll nmos = 0x80000000;  //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
const int mod = 1e9+7;
const double esp = 1e-8;
const double PI=acos(-1.0);
const double PHI=0.61803399;    //黄金分割点
const double tPHI=0.38196601;


template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x=f?-x:x;
}


/*-----------------------showtime----------------------*/
        const int maxn = 100009;
        int tot = 0,root,r1,r2,r3;
        struct node{
            int sz,val,rd,cnt;
            int ch[2];
        }tr[maxn];
        int newNode(int v){
            tot++;
            tr[tot].sz = 1; tr[tot].val = v;tr[tot].rd = rand();
            tr[tot].cnt = 1;
            return tot;
        }
        void update(int x){
            tr[x].sz = tr[tr[x].ch[0]].sz + tr[tr[x].ch[1]].sz + tr[x].cnt;
        }

        void split(int x,int val, int &a, int &b){
            if(!x) {a = b = 0;return;}
            if(tr[x].val <= val){
                a = x; split(tr[x].ch[1], val, tr[x].ch[1], b);
            }
            else {
                b = x; split(tr[x].ch[0], val, a, tr[x].ch[0]);
            }
            update(x);
        }
        int merge(int x,int y){
            if(x == 0 || y == 0)return x+y;
            if(tr[x].rd <= tr[y].rd){
                tr[x].ch[1] = merge(tr[x].ch[1], y);
                update(x);  return x;
            }
            else {
                tr[y].ch[0] = merge(x, tr[y].ch[0]);
                update(y); return y;
            }
        }

        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(tr[r2].ch[0], tr[r2].ch[1]);
            root = merge(merge(r1,r2), r3);
        }

        int Rank(int val){
            split(root,val-1,r1,r2);
            int res = tr[r1].sz + 1;
            merge(r1,r2);
            return res;
        }

        int kth(int rt,int k){
            int x = rt;
            while(true){
                if(x == 0)return -1;
                if(k <= tr[tr[x].ch[0]].sz){
                    x = tr[x].ch[0];
                }
                else if(k > tr[tr[x].ch[0]].sz + tr[x].cnt){
                    k = k - tr[tr[x].ch[0]].sz - tr[x].cnt;
                    x = tr[x].ch[1];
                }
                else return tr[x].val;
            }
        }

        int pre(int val){
            r1 = r2 = 0;
            split(root, val-1,r1,r2);
            int res = kth(r1, tr[r1].sz);
            merge(r1,r2);
            return res;
        }
        int nxt(int val){
            r1 = r2 = 0;
            split(root,val,r1,r2);
            int res = kth(r2,1);
            merge(r1,r2);
            return res;
        }
int main(){
        srand(time(0));
        int t;  scanf("%d", &t);
        while(t--){
            int op,x;
            scanf("%d%d", &op, &x);
            if(op == 1) insert(x);
            else if(op == 2 )delet(x);
            else if(op == 3)printf("%d\n", Rank(x));
            else if(op == 4)printf("%d\n", kth(root,x));
            else if(op == 5)printf("%d\n", pre(x));
            else if(op == 6)printf("%d\n", nxt(x));
        }
}
Treap

 

转载于:https://www.cnblogs.com/ckxkexing/p/9747575.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值