treap树的操作--查找区间第k大
2016-09-07 13:59
351 查看
treap树是线段树的升级版。
可以快速的进行区间第k大数的查询。
~但是还是认为splay好用(^_^).
进入正题:
treap是树和堆的有效结合,可以优化普通的线段树。
而红黑,AVL还是有点复杂。
treap的操作有插入,左旋,右旋。
其实原理比较简单,在树中维护一个”优先级“,”优先级“
采用随机数的方法,但是”优先级“必须满足根堆的性质,当然是“大根堆”或者“小根堆”都无所谓,比如下面的一棵树:
先把迷之结构体扔出来:
1.插入:
2.左旋:
总程序:
可以快速的进行区间第k大数的查询。
~但是还是认为splay好用(^_^).
进入正题:
treap是树和堆的有效结合,可以优化普通的线段树。
而红黑,AVL还是有点复杂。
treap的操作有插入,左旋,右旋。
其实原理比较简单,在树中维护一个”优先级“,”优先级“
采用随机数的方法,但是”优先级“必须满足根堆的性质,当然是“大根堆”或者“小根堆”都无所谓,比如下面的一棵树:
先把迷之结构体扔出来:
struct node{ int x,w,h,r,l; };
1.插入:
void insert(int v,int u){ int p; if(!v)return; if(u>a[v].x){ if(!a[v].r){ a[++e].x=u; a[e].w=rand(); a[e].h=v; a[v].r=e; p=e; while(a[p].h && a[p].w<=a[a[p].h].w){ if(p==a[a[p].h].r) rotate_left(p,a[p].h); else rotate_right(p,a[p].h); } if(a[p].h==0)root=p; }else insert(a[v].r,u); }else{ if(!a[v].l){ a[++e].x=u; a[e].w=rand(); a[e].h=v; a[v].l=e; p=e; while(a[p].h && a[p].w<=a[a[p].h].w){ if(p==a[a[p].h].r) rotate_left(p,a[p].h); else rotate_right(p,a[p].h); } if(a[p].h==0)root=p; }else insert(a[v].l,u); } }
2.左旋:
void rotate_left(int v,int u){ int t=a[u].h; a[u].r=a[v].l;a[a[v].l].h=u; a[u].h=v;a[v].l=u; a[v].h=t; if(a[t].l==u)a[t].l=v; else a[t].r=v; }
3.右旋: void rotate_right(int v,int u){ int t=a[u].h; a[u].l=a[v].r;a[a[v].r].h=u; a[u].h=v;a[v].r=u; a[v].h=t; if(a[t].l==u)a[t].l=v; else a[t].r=v; }
总程序:
#include<cstdio>
#include<cstdlib>
using namespace std;
struct node{ int x,w,h,r,l; };
struct node a[100001];
int e,root=1;
void rotate_left(int v,int u){ int t=a[u].h; a[u].r=a[v].l;a[a[v].l].h=u; a[u].h=v;a[v].l=u; a[v].h=t; if(a[t].l==u)a[t].l=v; else a[t].r=v; }
void rotate_right(int v,int u){
int t=a[u].h;
a[u].l=a[v].r;a[a[v].r].h=u;
a[u].h=v;a[v].r=u;
a[v].h=t;
if(a[t].l==u)a[t].l=v;
else a[t].r=v;
}
void insert(int v,int u){
int p;
if(!v)return;
if(u>a[v].x){
if(!a[v].r){
a[++e].x=u;
a[e].w=rand();
a[e].h=v;
a[v].r=e;
p=e;
while(a[p].h && a[p].w<=a[a[p].h].w){
if(p==a[a[p].h].r)
rotate_left(p,a[p].h);
else rotate_right(p,a[p].h);
}
if(a[p].h==0)root=p;
}else insert(a[v].r,u);
}else{
if(!a[v].l){
a[++e].x=u;
a[e].w=rand();
a[e].h=v;
a[v].l=e;
p=e;
while(a[p].h && a[p].w<=a[a[p].h].w){
if(p==a[a[p].h].r)
rotate_left(p,a[p].h);
else rotate_right(p,a[p].h);
}
if(a[p].h==0)root=p;
}else insert(a[v].l,u);
}
}
void out(int s){
if(!s)return;
out(a[s].l);
printf("%d ",a[s].x);
out(a[s].r);
}
int main(){
int i,j,k,m,n;
scanf("%d",&n);
scanf("%d",&k);
srand(3221);
a[1].x=k;e=1;
a[1].w=rand();
for(i=2;i<=n;i++){
scanf("%d",&k);
insert(root,k);
}
out(root);
return 0;
}
相关文章推荐
- poj2761——treap,区间第K小值
- POJ 2761(Treap离线求区间第k大数)
- HDU 5592 ZYB's Premutation 线段树(查找动态区间第K大)
- poj 1177 又是一道十分恶心到线段树。 主要是用线段树到区间操作。可以用来节省线性查找的时间。 离散化也十分好的一道题。
- ZOJ 2112 动态区间第K大(二分答案+线段树套Treap)
- POJ 1442 Black Box(【Treap】求动态区间第k大)
- poj1716(treap树求区间第k小的数字)
- 不会改变操作对象内容的STL算法(元素查找,序列查找,统计元素个数,比较区间,找最值)
- HDU 5592 ZYB's Premutation 线段树(查找动态区间第K大)
- 主席树静态查找区间第k小模板(并没有杰少详细)即讲解
- Treap模板(第K大,插入删除查找)
- 查找给定区间内第K大/小的数
- hdu 5412 CRB and Queries(动态区间第k大值,区间能修改)(整体二分,树状数组套平衡树,线段树套treap)
- SBTree的左旋右旋以及各种调整操作的基本实现(包含查找第k大的数值)
- 无旋treap的区间操作实现
- 7GOJ 24 girls [可持久化Treap--区间操作]
- POJ2761---Feed the dogs (Treap求区间第k大)
- poj2761(静态区间第k大,treap)
- Treap:查找第k大元素
- 查找给定区间内第K大的元素