SPOJ 3273 - Order statistic set , Treap
题意:
集合S支持一下四种操作:
INSERT(S,x) : 假设S中没有x,则插入x
DELETE(S,x): 假设S中有x,则删除x
K-TH(S): 输出S中第K小的数
COUNT(S,x): 统计S中小于x的数有多少个
一共同拥有Q(1 ≤ Q ≤ 200000)次操作。
Treap模板。。
#include<cstdio>
#include<cstring>
#include<cstdlib>
const int inf=0x3f3f3f3f; struct Node {
Node *ch[2];
int r, v, s;
Node(int v):v(v) { ch[0] = ch[1] = NULL; r = rand(); s = 1; } int cmp(int x) const {
if (x == v) return -1;
return (x < v) ? 0 : 1;
} void maintain() {
s = 1;
if(ch[0] != NULL) s += ch[0]->s;
if(ch[1] != NULL) s += ch[1]->s;
}
}; void rotate(Node* &o, int d) {
Node* k = o->ch[d^1]; o->ch[d^1] = 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(x);
else{
/*int d = (x < o->v ? 0 : 1); // 同意插入同样结点*/
int d =o->cmp(x); if(d==-1) return ; //不同意插入同样结点*/
insert(o->ch[d], x);
if(o->ch[d]->r > o->r) rotate(o, d^1);
else 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] == NULL) ? 0 : 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 rank(Node* o, int x){
int ret = 0;
while(o != NULL){
int s = (o->ch[0] == NULL) ? 0 : o->ch[0]->s;
if(o->v < x){ <span style="color:#ff0000;">//因为是统计比x小的数的个数,所以不能取等号</span>
ret += s + 1;
o = o->ch[1];
}else{
o = o->ch[0];
}
}
return ret;
} int main()
{
int q;
Node* root=NULL;
//freopen("in.txt","r",stdin);
scanf("%d",&q);
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",rank(root,nb));
}
}
return 0;
}
SPOJ 3273 - Order statistic set , Treap的更多相关文章
- SPOJ ORDERSET - Order statistic set
ORDERSET - Order statistic set In this problem, you have to maintain a dynamic set of numbers whic ...
- SPOJ 3273
传送门: 这是一道treap的模板题,不要问我为什么一直在写模板题 依旧只放代码 Treap 版 //SPOJ 3273 //by Cydiater //2016.8.31 #include < ...
- SQL Server数据库--》top关键字,order by排序,distinct去除重复记录,sql聚合函数,模糊查询,通配符,空值处理。。。。
top关键字:写在select后面 字段的前面 比如你要显示查询的前5条记录,如下所示: select top 5 * from Student 一般情况下,top是和order by连用的 orde ...
- Hihocoder 1325 平衡树·Treap(平衡树,Treap)
Hihocoder 1325 平衡树·Treap(平衡树,Treap) Description 小Ho:小Hi,我发现我们以前讲过的两个数据结构特别相似. 小Hi:你说的是哪两个啊? 小Ho:就是二叉 ...
- 【BZOJ1391】Order(网络流,最小割)
[BZOJ1391]Order(网络流,最小割) 题面 BZOJ权限题... 良心洛谷 题目描述 有N个工作,M种机器,每种机器你可以租或者买过来. 每个工作包括若干道工序,每道工序需要某种机器来完成 ...
- MySQL性能优化,MySQL索引优化,order by优化,explain优化
前言 今天我们来讲讲如何优化MySQL的性能,主要从索引方面优化.下期文章讲讲MySQL慢查询日志,我们是依据慢查询日志来判断哪条SQL语句有问题,然后在进行优化,敬请期待MySQL慢查询日志篇 建表 ...
- Order Statistic
目录 The Order Statistic 引理1 的一些基本性质 顺序统计量的分布 顺序统计量的条件分布 特殊分布的特殊性质 Order Statistic The Order Statistic ...
- sql order by+字段,指定按照哪个字段来排序
1.我们就可以使用 MySQL 的 ORDER BY 子句来设定你想按哪个字段哪中方式来进行排序,再返回搜索结果. 2.SELECT field1, field2,...fieldN table_na ...
- spoj 7258 SUBLEX(SAM,名次)
[题目链接] http://www.spoj.com/problems/SUBLEX/en/ [题意] 给定一个字符串,询问次序为k的子串. [思路] SAM,名次 建好SAM后求出每个结点根据tra ...
随机推荐
- no identities are available for signing
原地址:http://www.cnblogs.com/imzzk/p/3501868.html 今天将做好的app提交到app store,结果就出现标题上的错误.“No identities are ...
- PIXLCLOUND
http://pixlcloud.com/main/career/ https://www.recordedfuture.com/siem-threat-intelligence-part-1/
- maven 项目编译时候提示:Error building POM (may not be this project's POM).
编译时候提示Error building POM (may not be this project's POM)的错误,具体信息如下: [0] 'dependencies.dependency.ver ...
- http://blog.csdn.net/shirdrn/article/details/6270506
http://blog.csdn.net/shirdrn/article/details/6270506
- Android MediaStore与Media.EXTERNAL_CONTENT_URI
MediaStore这个类是Android系统提供的一个多媒体数据库,android中多媒体信息都可以从这里提取.这个MediaStore包括了多媒体数据库的所有信息,包括音频,视频和图像,andro ...
- 【HDOJ】1720 A+B coming
水题. #include <stdio.h> #include <string.h> #define MAXNUM 1005 int stoi(char); int main( ...
- [转] AC自动机详解
转载自:http://hi.baidu.com/nialv7/item/ce1ce015d44a6ba7feded52d AC自动机详解 AC自动机是用来处理多串匹配问题的,即给你很多串,再给你一篇文 ...
- [swustoj 764] 校门外的树 Plus Plus
校门外的树 Plus Plus(0764) 问题描述 西南某科技大学的校门外长度为 L 的公路上有一排树,每两棵相邻的树之间的间隔都是 1 米.我们可以把马路看成一个数轴,马路的一端在数轴 1 的位置 ...
- MFC UpdateData自动更新
嗯,添加一个按钮和我们自己定义的成员变量 关联. 方法1. 在*.rc 或者class 类视图中点击[add variable],就会自动生成DDX_Text(*,*)的内容及其IF 方法2. 自己 ...
- 【Android】Android程序保护与破解浅析
此文源自组内成员分享的PPT,其他成员的文档由于没有得到授权,暂不公开. 本文命令如果没有特殊注明,均为windows 7环境. 本文只涉及大概的知识点,不涉及具体的细节,需要注意. 反编译 apkt ...