Treap——模板
来源:互联网 发布:js省市区三级联动插件 编辑:程序博客网 时间:2024/05/01 10:39
基于旋转的Treap。
复杂度O(
#include<cstdio>#include<cstdlib>int max(int x,int y){ return x>y?x:y; }int min(int x,int y){ return x<y?x:y; }struct node{ node* ch[2]; int key,fix,size,cnt; node(){} node(int w,node* son){ key=w; ch[0]=ch[1]=son; fix=rand(); size=cnt=1; } void maintain(){ size=ch[0]->size + ch[1]->size + cnt; }};typedef node* P_node;struct Treap{ node nil; P_node root,null,len; Treap(){ root=null=&nil; null->key=null->fix=1e+9; null->size=null->cnt=0; null->ch[0]=null->ch[1]=null; } void rot(P_node &p,int d){ P_node k=p->ch[d^1]; p->ch[d^1]=k->ch[d]; k->ch[d]=p; p->maintain(); k->maintain(); p=k; } void _Insert(P_node &p,int tkey){ if(p==null) p=new node(tkey,null); else if(p->key==tkey) p->cnt++; else{ int d=tkey>p->key; _Insert(p->ch[d],tkey); if(p->ch[d]->fix > p->fix) rot(p,d^1); } p->maintain(); } void _Erase(P_node &p,int tkey){ if(p->key==tkey){ if(p->cnt>1) p->cnt--; else if(p->ch[0]==null||p->ch[1]==null){ P_node tem=p; p->ch[0]==null?p=p->ch[1]:p=p->ch[0]; delete tem; }else{ int d=p->ch[0]->fix > p->ch[1]->fix; rot(p,d); _Erase(p->ch[d],tkey); } } else _Erase(p->ch[tkey>p->key],tkey); p->maintain(); } int _Kth(P_node p,int k){ if(p==null||k<1||k>p->size) return 0; if(k<p->ch[0]->size+1) return _Kth(p->ch[0],k); if(k>p->ch[0]->size+p->cnt) return _Kth(p->ch[1],k-p->ch[0]->size-p->cnt); return p->key; } int _Rank(P_node p,int tkey,int res){ if(p->key==tkey) return p->ch[0]->size+res+1; if(tkey<p->key) return _Rank(p->ch[0],tkey,res); return _Rank(p->ch[1],tkey,res+ p->ch[0]->size + p->cnt); } int _Pred(P_node p,int tkey){ if(p==null) return -1e+9; if(tkey<=p->key) return _Pred(p->ch[0],tkey); return max(p->key,_Pred(p->ch[1],tkey)); } int _Succ(P_node p,int tkey){ if(p==null) return 1e+9; if(tkey>=p->key) return _Succ(p->ch[1],tkey); return min(p->key,_Succ(p->ch[0],tkey)); } void _Print(P_node p){ if(p==null) return; _Print(p->ch[0]); for(int i=1;i<=p->cnt;i++) printf("%d ",p->key); _Print(p->ch[1]); } void Insert(int tkey){ _Insert(root,tkey); } void Erase(int tkey){ _Erase(root,tkey); } int Kth(int k){ return _Kth(root,k); } int Rank(int tkey){ return _Rank(root,tkey,0); } int Pred(int tkey){ return _Pred(root,tkey); } int Succ(int tkey){ return _Succ(root,tkey); } void Print(){ _Print(root); printf("\n"); } } T;int n;int main(){ freopen("treap.in","r",stdin); freopen("treap.out","w",stdout); scanf("%d",&n); for(int i=1;i<=n;i++){ int pd,x; scanf("%d%d",&pd,&x); if(pd==1) T.Insert(x); if(pd==2) T.Erase(x); if(pd==3) printf("%d\n",T.Rank(x)); if(pd==4) printf("%d\n",T.Kth(x)); if(pd==5) printf("%d\n",T.Pred(x)); if(pd==6) printf("%d\n",T.Succ(x)); } return 0;}
0 0
- Treap——模板
- 模板——Treap实现名次树
- 【Treap】Treap模板
- Treap模板+Treap介绍。。。
- ACM常用模板——数据结构——treap树
- Treap模板——POJ1442——Black Box
- 平衡树——Treap (含完整模板)
- Treap模板
- 【Treap模板】
- (模板)treap
- treap 模板
- Treap模板
- treap 模板
- treap模板
- Treap 模板
- 【模板】Treap
- treap模板
- Treap模板
- CodeBlocks恢复最初设置,解决无编译器问题
- vs2012 opencv c++
- 决策树的剪枝和CART算法
- HBase学习笔记——入门简介
- SAML
- Treap——模板
- 二叉搜索树的第k个节点
- lay lie lie
- 程序猿如何“智斗”产品经理
- 树莓派3一根网线直连电脑(针对树莓派不能上网有方法解决)
- git rebase 和 git fetch 区别
- 文章标题
- 表达式谜题(谜题均转自JAVA学习宝典)
- 杭电题型