bzoj3224: Tyvj 1728 普通平衡树
2016-09-07 19:57
363 查看
链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3224
题意:中文题。
分析:treap模板练习。其实知道了怎么旋转就知道了整个treap啦,实现的时候注意细节即可。
代码:
#include<map> #include<set> #include<cmath> #include<queue> #include<bitset> #include<math.h> #include<vector> #include<string> #include<stdio.h> #include<cstring> #include<iostream> #include<algorithm> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; const int N=15; const int M=1e5+7; const int HASHSIZE=3e5+9; const int mod=100000000; const int MOD1=1000000007; const int MOD2=1000000009; const double EPS=0.00000001; typedef long long ll; const ll MOD=1000000007; const int INF=~0u>>1; const ll MAX=1ll<<55; const double pi=acos(-1.0); typedef double db; typedef unsigned long long ull; struct Treap { struct node { node *son[2]; int key,siz,wei,cnt; node(int _key,node *f) { son[0]=son[1]=f; key=_key;siz=cnt=1;wei=rand(); } void pushup() { siz=son[0]->siz+son[1]->siz+cnt; } }*null,*root; Treap() { null=new node(0,0); null->siz=null->siz=0; null->wei=INF;root=null; } void rot(node* &rt,bool d) { node* c=rt->son[!d];rt->son[!d]=c->son[d]; c->son[d]=rt;rt->pushup();c->pushup();rt=c; } void insert(const int &key,node* &rt) { if (rt==null) { rt=new node(key,null);return ; } if (key==rt->key) { rt->cnt++;rt->siz++;return ; } bool d=key>rt->key; insert(key,rt->son[d]); if (rt->wei>rt->son[d]->wei) rot(rt,!d); rt->pushup(); } void remove(const int &key,node* &rt) { if (rt==null) return ; bool d=key>rt->key; if (key==rt->key) { if (rt->cnt>1) { rt->cnt--;rt->siz--;return ; } d=rt->son[0]->wei>rt->son[1]->wei; if (rt->son[d]==null) { delete rt;rt=null;return ; } rot(rt,!d);remove(key,rt->son[!d]); } else remove(key,rt->son[d]); rt->pushup(); } node* select(int k,node* rt) { int s=rt->son[0]->siz+rt->cnt; if (k>=rt->son[0]->siz+1&&k<=s) return rt; if (s>k) return select(k,rt->son[0]); else return select(k-s,rt->son[1]); } int rank(const int &key,node* rt) { if (rt==null) return 0; int s=rt->son[0]->siz+rt->cnt; if (key==rt->key) return rt->son[0]->siz+1; if (key<rt->key) return rank(key,rt->son[0]); else return s+rank(key,rt->son[1]); } int pre(const int &k) { node* t=root;int ret=0; while (t!=null) if (t->key<k) ret=t->key,t=t->son[1]; else t=t->son[0]; return ret; } int sub(const int &k) { node* t=root;int ret=0; while (t!=null) if (t->key>k) ret=t->key,t=t->son[0]; else t=t->son[1]; return ret; } }; int main() { int i,a,b,n; Treap tree; scanf("%d", &n); while (n--) { scanf("%d%d", &a, &b); if (a==1) tree.insert(b,tree.root); if (a==2) tree.remove(b,tree.root); if (a==3) printf("%d\n", tree.rank(b,tree.root)); if (a==4) printf("%d\n", tree.select(b,tree.root)->key); if (a==5) printf("%d\n", tree.pre(b)); if (a==6) printf("%d\n", tree.sub(b)); } return 0; }
相关文章推荐
- POJ2486 树上背包裸题
- 安卓学习笔记之AsyncTask机制浅析
- 类映射,关联起两个不一致的code
- 【GDOI2013模拟4】贴瓷砖
- 错误记录01
- 学习openCV的资料
- LeetCode:Two Sum II - Input array is sorted
- 免费的高效视频会议系统 你也可以拥有
- 非root用户如何使用80端口启动tomcat
- 原来只是说说而已
- HDU:2555 人人都能参加第30届校田径运动会了
- linux iptables
- linux 内核链表
- 邮局问题(DP)
- 使用Automake 创建和使用静态库 转
- irrlicht实现沿平面移动物体
- leetcode 29. Divide Two Integers
- qt之QDrag
- 机智云开发者大会|第一个把发布会开在北京MIX的物联网公司
- 第六周作业