bzoj3224: Tyvj 1728 普通平衡树
来源:互联网 发布:如何屏蔽公司网络监控 编辑:程序博客网 时间:2024/04/30 03:10
链接: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;}
0 0
- [Bzoj3224]Tyvj 1728 普通平衡树
- [BZOJ3224]Tyvj 1728 普通平衡树 && treap
- [BZOJ3224]Tyvj 1728 普通平衡树
- 【bzoj3224】Tyvj 1728 普通平衡树
- [BZOJ3224] Tyvj 1728 普通平衡树
- 【bzoj3224】Tyvj 1728 普通平衡树
- 【BZOJ3224】Tyvj 1728 普通平衡树
- bzoj3224: Tyvj 1728 普通平衡树
- bzoj3224 Tyvj 1728 普通平衡树
- 【BZOJ3224】 Tyvj 1728 普通平衡树
- 【bzoj3224】Tyvj 1728 普通平衡树
- 【bzoj3224】Tyvj 1728 普通平衡树
- bzoj3224 Tyvj 1728 普通平衡树 treap
- bzoj3224 Tyvj 1728 普通平衡树
- [BZOJ3224]Tyvj 1728 普通平衡树 && 重写平衡树
- bzoj3224: Tyvj 1728 普通平衡树(伸展树)
- bzoj3224 Tyvj 1728 普通平衡树 ( 替罪羊树)
- 【模板】【bzoj3224】Tyvj 1728 普通平衡树 Treap
- 34. Search for a Range-二分搜索
- irrlicht实现沿平面移动物体
- 机智云开发者大会|第一个把发布会开在北京MIX的物联网公司
- qt之QDrag
- LeetCode:Two Sum II - Input array is sorted
- bzoj3224: Tyvj 1728 普通平衡树
- ads 修改snap的单位
- 使用Automake 创建和使用静态库 转
- 反射+注解
- 邮局问题(DP)
- MySQL隐式转化整理
- HDU:2555 人人都能参加第30届校田径运动会了
- 学习openCV的资料
- 【GDOI2013模拟4】贴瓷砖