码迷,mamicode.com
首页 > 其他好文 > 详细

Treap模板

时间:2018-07-31 15:14:01      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:using   efi   long   min   double   void   ase   sign   amp   

非指针Treap

#include<bits/stdc++.h>
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pqueue priority_queue
#define NEW(a,b) memset(a,b,sizeof(a))
const double pi=4.0*atan(1.0);
const double e=exp(1.0);
const int maxn=3e6+8;
typedef long long LL;
typedef unsigned long long ULL;
//typedef pair<LL,LL> P;
const LL mod=1e9+7;
const ULL base=1e7+7;
using namespace std;
struct node{
    int son[2];
    int siz;
    int key,w;
}a[100008];
int tot=0;
int root=0;
void up(int i){
    a[i].siz=a[a[i].son[0]].siz+a[a[i].son[1]].siz+1;
}
void Rotate(int &i,int d){
    int t=a[i].son[d];
    a[i].son[d]=a[t].son[!d];
    a[t].son[!d]=i;
    up(i);up(t);
    i=t;
}
void Insert(int &i,int key){
    if(i==0){
        i=++tot;
        a[i].siz=1;a[i].key=key;a[i].w=rand();
        return ;
    }
    a[i].siz++;
    if(a[i].key>=key) {
        Insert(a[i].son[0],key);
        if(a[a[i].son[0]].w<a[i].w) Rotate(i,0);
    }
    else{
        Insert(a[i].son[1],key);
        if(a[a[i].son[1]].w<a[i].w) Rotate(i,1);
    }
}
void Del(int &i,int key){
    if(a[i].key==key){
        if(a[i].son[0]*a[i].son[1]==0)  {i=a[i].son[0]+a[i].son[1];return ;}
        if(a[a[i].son[0]].w>a[a[i].son[1]].w){
            Rotate(i,1);
            Del(a[i].son[0],key);
        }
        else{
            Rotate(i,0);
            Del(a[i].son[1],key);
        }
    }
    else if(a[i].key>key){
        Del(a[i].son[0],key);
    }
    else{
        Del(a[i].son[1],key);
    }
    up(i);
}
int Find(int i,int key){
    if(i==0) return 1;
    if(a[i].key>=key) return Find(a[i].son[0],key);
    else return a[a[i].son[0]].siz+Find(a[i].son[1],key)+1;
}
int Search(int i,int rak){
    if(a[a[i].son[0]].siz==rak-1) return a[i].key;
    if(a[a[i].son[0]].siz>=rak) return Search(a[i].son[0],rak);
    else  return Search(a[i].son[1],rak-a[a[i].son[0]].siz-1);
}
int pre(int i,int key){
    if(i==0) return -10000008;
    if(a[i].key<key) return max(a[i].key,pre(a[i].son[1],key));
    return pre(a[i].son[0],key);
}
int bhe(int i,int key){
    if(i==0) return 10000008;
    if(a[i].key>key) return min(a[i].key,bhe(a[i].son[0],key));
    return bhe(a[i].son[1],key);
}
int main(){
    int n;
    scanf("%d",&n);
    int opt,x;
    while(n--){
        scanf("%d%d",&opt,&x);
        if(opt==1){
            Insert(root,x);
        }
        if(opt==2){
            Del(root,x);
        }
        if(opt==3){
            printf("%d\n",Find(root,x));
        }
        if(opt==4){
            printf("%d\n",Search(root,x));
        }
        if(opt==5){
            printf("%d\n",pre(root,x));
        }
        if(opt==6){
            printf("%d\n",bhe(root,x));
        }
    }
}

 

Treap模板

标签:using   efi   long   min   double   void   ase   sign   amp   

原文地址:https://www.cnblogs.com/Profish/p/9395460.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!