SPOJ 3273 - Order statistic set Treap模板题
来源:互联网 发布:淘宝网小米4s充电线 编辑:程序博客网 时间:2024/06/03 14:26
题意:
有个集合..起初为空...有Q(1<=Q<=200000)个操作或者询问
操作insert: I x: 若集合中没有x...把x放入
操作delete: D x:若集合中有x..删去x
询问K-th: K x:输出集合中第x大的数是什么...若总个数没有k个输出invalid
询问Count: C x:输出集合中有多少个数比x小...
题解:
裸treap了....操作还挺全的...为了找到第k大的数以及小于某个数的个数...每个点上加一个变量.用于存储以其为根的子树有多少个节点...insert和delete参考了nocow中CmYkRgB123的代码...两个询问时原创的~不用递归相当和谐..
Program:
#include<iostream>#include<stdio.h>#include<string.h>#include<cmath>#include<queue>#include<stack>#include<set>#include<time.h>#include<map>#include<algorithm>#define ll long long#define eps 1e-5#define oo 1000000007#define pi acos(-1.0)#define MAXN 200005using namespace std; struct node{ int l,r,key,fix,size;};struct treap{ node h[MAXN]; int root,num; void initial() { srand((int)time(0)),num=root=0; } void rot_l(int &x) { int R=h[x].r,L=h[x].l; h[x].size=h[x].size-h[R].size+h[h[R].l].size; h[R].size+=h[L].size+1; h[x].r=h[R].l,h[R].l=x; x=R; } void rot_r(int &x) { int L=h[x].l,R=h[x].r; h[x].size=h[x].size-h[L].size+h[h[L].r].size; h[L].size+=h[R].size+1; h[x].l=h[L].r,h[L].r=x; x=L; } bool insert(int &k,int key) { if (!k) { k=++num; h[k].l=h[k].r=0,h[k].size=1; h[k].key=key,h[k].fix=rand(); return true; } if (h[k].key==key) return false; if (h[k].key>key) { if (!insert(h[k].l,key)) return false; h[k].size++; if (h[h[k].l].fix>h[k].fix) rot_r(k); return true; }else { if (!insert(h[k].r,key)) return false; h[k].size++; if (h[h[k].r].fix>h[k].fix) rot_l(k); return true; } } int count(int key) { int g=0,k=root; while (k) { if (h[k].key>key) k=h[k].l; else g+=h[h[k].l].size+1,k=h[k].r; } return g; } int k_th(int kth) { int g=0,k=root; if (h[root].size<kth) return -1; while (h[h[k].l].size+g+1!=kth) { if (h[h[k].l].size+g+1>=kth) k=h[k].l; else g+=h[h[k].l].size+1,k=h[k].r; } return h[k].key; } bool del(int &k,int key) { if (!k) return false; if (h[k].key>key) { if (!del(h[k].l,key)) return false; h[k].size--; } else if (h[k].key<key) { if (!del(h[k].r,key)) return false; h[k].size--; } else { if (!h[k].l && !h[k].r) k=0; else if (!h[k].l) k=h[k].r; else if (!h[k].r) k=h[k].l; else { if (h[h[k].l].fix<h[h[k].r].fix) { rot_l(k); if (!del(h[k].l,key)) return false; h[k].size--; } else { rot_r(k); if (!del(h[k].r,key)) return false; h[k].size--; } } } return true; } }mytreap;int main(){ int m,x; char c; mytreap.initial(); scanf("%d",&m); while (m--) { do { c=getchar(); } while (c<'A' || c>'Z'); scanf("%d",&x); if (c=='I') mytreap.insert(mytreap.root,x); else if (c=='C') printf("%d\n",mytreap.count(x-1)); else if (c=='K') { x=mytreap.k_th(x); if (x==-1) printf("invalid\n"); else printf("%d\n",x); }else if (c=='D') mytreap.del(mytreap.root,x); } return 0;}
- SPOJ 3273 - Order statistic set Treap模板题
- SPOJ 3273 - Order statistic set , Treap
- SPOJ 3273 Order statistic set(Treap应用)
- [SPOJ ORDERSET] Order statistic set [Treap]
- SPOJ 3273 Order statistic set
- SPOJ 3273 Order statistic set
- SPOJ ORDERSET Order statistic set 非旋转treap
- [SPOJ ORDERSET] Order statistic set [Splay]
- SPOJ3273--Order statistic set
- SPOJ ORDERSET Order statistic set 简单平衡树 或 树状数组
- SPOJ 3273 Treap
- treap模板(set,map,multiset, 可持久化treap)
- Treap基础模板题
- hdu4585 shaolin【 Treap模板题】
- 【Treap】Treap模板
- Treap模板+Treap介绍。。。
- Treap模板
- 【Treap模板】
- 学习mongodb(一)
- asp.net缓存
- 2013数据库大会:盖国强-Oracle优化器与算法案例解析
- Jquery显示、隐藏元素以及添加删除样式
- 闪亮登场:开发者制作图形图表所需的11款JS工具包
- SPOJ 3273 - Order statistic set Treap模板题
- hl7中V2版本的ACK消息的构造
- 浅谈防火墙对FTP的影响及故障排除
- Filezilla虚拟目录的配置(解决设置多个目录只显示主目录的问题)
- cocos2dx移植到Android报错:collect2: ld returned 1 exit status
- MFC 窗体字体加载 触发修改
- 一篇分析诊断被"hang"住数据库的资料(Oracle Performance Diagnostic Guide——Hang/Locking)
- linux2.6内核链表
- 如何在service中弹出dialog