SPOJ 3273 Order statistic set
来源:互联网 发布:php pdo 编辑:程序博客网 时间:2024/06/18 15:16
红果果的treap模板题。。。白书版treap
Order statistic set
Time Limit: 2000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu
[Submit] [Go Back] [Status]
Description
EnglishVietnamese
In this problem, you have to maintain a dynamic set of numbers which support the two fundamental operations
- INSERT(S,x): if x is not in S, insert x into S
- DELETE(S,x): if x is in S, delete x from S
and the two type of queries
- K-TH(S) : return the k-th smallest element of S
- COUNT(S,x): return the number of elements of S smaller than x
Input
- Line 1: Q (1 ≤ Q ≤ 200000), the number of operations
- In the next Q lines, the first token of each line is a character I, D, K or C meaning that the corresponding operation is INSERT, DELETE, K-TH or COUNT, respectively, following by a whitespace and an integer which is the parameter for that operation.
If the parameter is a value x, it is guaranteed that 0 ≤ |x| ≤ 109. If the parameter is an index k, it is guaranteed that 1 ≤ k ≤ 109.
Output
For each query, print the corresponding result in a single line. In particular, for the queries K-TH, if k is larger than the number of elements in S, print the word 'invalid'.
Sample Input
Input8I -1I -1I 2C 0K 2D -1K 1K 2Output122invalid
Source
© VNOI
[Submit] [Go Back] [Status]
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <ctime>#include <algorithm>using namespace std;const int INF=0x3f3f3f3f;struct Node{ Node * ch[2]; int r,s,v; int cmp(int x) { if(x==v) return -1; return (x<v)?0:1; } void maintain() { s=1+ch[0]->s+ch[1]->s; }};Node* null ;void init(){ null=new Node(); null->s=0; null->ch[0]=null->ch[1]=null; srand(time(NULL));}void rotate(Node* &o,int d){ Node* k=o->ch[1^d]; o->ch[1^d]=k->ch[d]; k->ch[d]=o; o->maintain(); k->maintain(); o=k;}void insert(Node* &o,int x){ if(o==null) { o=new Node(); o->ch[0]=o->ch[1]=null; o->v=x;o->r=rand();o->s=1; } else { int d=o->cmp(x); if(d==-1) return ; insert(o->ch[d],x); if(o->r < o->ch[d]->r) rotate(o,d^1); } o->maintain();}void remove(Node* &o,int x){ int d=o->cmp(x); if(d==-1) { Node* u=o; if(o->ch[0]!=null&&o->ch[1]!=null) { int d2=(o->ch[0]->r>o->ch[1]->r)?1:0; rotate(o,d2); remove(o->ch[d2],x); } else { if(o->ch[0]==null) o=o->ch[1]; else o=o->ch[0]; delete u; } } else remove(o->ch[d],x); if(o!=null) o->maintain();}int find(Node* o,int x){ while(o!=null) { int d=o->cmp(x); if(d==-1) return 1; o=o->ch[d]; } return 0;}int Kth(Node* o,int k)///kth small{ while(o!=null) { int s=o->ch[0]->s; if(k==s+1) return o->v; else if(k<=s) o=o->ch[0]; else { o=o->ch[1]; k=k-s-1; } } return -INF;}int Count(Node* o,int x){ int ret=0; while(o!=null) { if(x>o->v) { ret+=o->ch[0]->s+1; o=o->ch[1]; } else o=o->ch[0]; } return ret;}int main(){ int q; scanf("%d",&q); init(); Node* root=null; while(q--) { char cmd[3];int nb; scanf("%s%d",cmd,&nb); if(cmd[0]=='I'&&find(root,nb)==0) { insert(root,nb); } else if(cmd[0]=='D'&&find(root,nb)==1) { remove(root,nb); } else if(cmd[0]=='K') { int temp=Kth(root,nb); if(temp==-INF) puts("invalid"); else printf("%d\n",temp); } else if(cmd[0]=='C') { printf("%d\n",Count(root,nb)); } } return 0;}
0 0
- SPOJ 3273 Order statistic set
- SPOJ 3273 Order statistic set
- 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 ORDERSET] Order statistic set [Splay]
- SPOJ ORDERSET Order statistic set 非旋转treap
- SPOJ3273--Order statistic set
- SPOJ ORDERSET Order statistic set 简单平衡树 或 树状数组
- 扩充的数据结构-Order Statistic Tree
- 顺序统计量(Order Statistic)小结
- statistic
- SPOJ QTree7 [SET][树链剖分]
- 算法导论 第九章:顺序统计量(Order Statistic)
- Linux C一站式学习习题答案11.5.3 k-th Order Statistic
- Redis order set 有序集合
- (STL之set与multiset)SPOJ
- 56个面试技巧性回答
- Step5.2.3 hdu 1800Flying to the Mars
- fork创建进程
- VNC源码研究(二十四)vnc-4.0-winsrc版本之winvnc工程分析
- 基于Hadoop的云盘系统客户端技术难点之三 小文件存储优化
- SPOJ 3273 Order statistic set
- C++字符串逆排序
- Ubuntu 12.04 安装 IQQ
- 编程思想
- Add Two Numbers
- eclipse中快速打开CMD窗口插件介绍
- switch 语句的一个小谜团
- solr学习第八课程----MultiCore的配置方法(第一集)
- 通过反射的方式解析R文件