点击打开链接

题意:

集合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的更多相关文章

  1. SPOJ ORDERSET - Order statistic set

    ORDERSET - Order statistic set   In this problem, you have to maintain a dynamic set of numbers whic ...

  2. SPOJ 3273

    传送门: 这是一道treap的模板题,不要问我为什么一直在写模板题 依旧只放代码 Treap 版 //SPOJ 3273 //by Cydiater //2016.8.31 #include < ...

  3. SQL Server数据库--》top关键字,order by排序,distinct去除重复记录,sql聚合函数,模糊查询,通配符,空值处理。。。。

    top关键字:写在select后面 字段的前面 比如你要显示查询的前5条记录,如下所示: select top 5 * from Student 一般情况下,top是和order by连用的 orde ...

  4. Hihocoder 1325 平衡树·Treap(平衡树,Treap)

    Hihocoder 1325 平衡树·Treap(平衡树,Treap) Description 小Ho:小Hi,我发现我们以前讲过的两个数据结构特别相似. 小Hi:你说的是哪两个啊? 小Ho:就是二叉 ...

  5. 【BZOJ1391】Order(网络流,最小割)

    [BZOJ1391]Order(网络流,最小割) 题面 BZOJ权限题... 良心洛谷 题目描述 有N个工作,M种机器,每种机器你可以租或者买过来. 每个工作包括若干道工序,每道工序需要某种机器来完成 ...

  6. MySQL性能优化,MySQL索引优化,order by优化,explain优化

    前言 今天我们来讲讲如何优化MySQL的性能,主要从索引方面优化.下期文章讲讲MySQL慢查询日志,我们是依据慢查询日志来判断哪条SQL语句有问题,然后在进行优化,敬请期待MySQL慢查询日志篇 建表 ...

  7. Order Statistic

    目录 The Order Statistic 引理1 的一些基本性质 顺序统计量的分布 顺序统计量的条件分布 特殊分布的特殊性质 Order Statistic The Order Statistic ...

  8. sql order by+字段,指定按照哪个字段来排序

    1.我们就可以使用 MySQL 的 ORDER BY 子句来设定你想按哪个字段哪中方式来进行排序,再返回搜索结果. 2.SELECT field1, field2,...fieldN table_na ...

  9. spoj 7258 SUBLEX(SAM,名次)

    [题目链接] http://www.spoj.com/problems/SUBLEX/en/ [题意] 给定一个字符串,询问次序为k的子串. [思路] SAM,名次 建好SAM后求出每个结点根据tra ...

随机推荐

  1. jquery mobile event

    jquery.js $(document).on("mobileinit", function() { // }); jquery.mobile.js $(document).re ...

  2. C++如何处理内联虚函数

    http://blog.csdn.net/hedylin/article/details/1775556 当一个函数是内联和虚函数时,会发生代码替换或使用虚表调用吗? 为了弄清楚内联和虚函数,让我们将 ...

  3. Java调用存储过程时报 The user specified as a definer ('root'@'%') does not exist 解决方法

    Caused by: java.sql.SQLException: The user specified as a definer (''@'') does not exist        at c ...

  4. python还不能作为主要编程语言的原因:

    1.不太熟悉,容易犯新手错误,2.调试方法不同3.写了一个函数,是否语法正确,不能知道,只有具体调用它的时候才知道4.编辑器太业余,没有输入联想功能5.要查找一个函数或变量在哪里定义的,只能通过搜索的 ...

  5. matlab numpy equivalents

    THIS IS AN EVOLVING WIKI DOCUMENT. If you find an error, or can fill in an empty box, please fix it! ...

  6. Layout Resource官方教程(1)简介

    Layout Resource SEE ALSO Layouts A layout resource defines the architecture for the UI in an Activit ...

  7. C++文件读写总结

    在C++中如何实现文件的读写? 作者: infobillows 发表日期: 2007-04-03 21:33 点击数: 465 一.ASCII 输出 为了使用下面的方法, 你必须包含头文件<fs ...

  8. vim解决中文显示乱码问题

    命令:vim ~/.vimrc 写入如下: set enc=utf-8 set fileencoding=utf-8 set fileencodings=ucs-bom,utf8,prc set gu ...

  9. BZOJ_1005_ [HNOI2008]_明明的烦恼_(组合数学+purfer_sequence+高精度+分解因数+快速幂)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1005 一棵树有n个点,给出没给节点的度,如果没有限制则为-1,求共有多少种可能的树. 分析 蒟 ...

  10. ruby2.2.2 源代码阅读笔记

    这是win32下的结构 从ruby_setup开始阅读 Ruby对象内存结构 RVALUE是一个union,内含ruby所有结构体(RBasic RObject RClass RFloat RStri ...