点击打开链接

题意:

集合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. Vases and Flowers

    hdu4614:http://acm.hdu.edu.cn/showproblem.php?pid=4614 题意:给你n个花瓶,然后有两种操作:1从a开始选择b个花瓶,放进花,输出左端点,右端点 2 ...

  2. HDU4515+计算日期

    模拟! /* 计算过了D天后的日期 之前D天的日期 */ #include<stdio.h> int judge_year( int year ){ ==&&year%!= ...

  3. Error -27791: Server xx has shut down the connection prematurely

    最近在进行一次性能测试中遇到一个问题,并发较大的时候会出现LR出现Error -27791: Server xx has shut down the connection prematurely的ER ...

  4. [二分匹配]URAL1721Two Sides of the Same Coin

    题意:给n个人,每个人都有3个参数,分别是名字,能做的事(a:statements  b:testdate  a.b都可以:anything),Rank 要求:一个人只能做一个事件,要两个人Rank相 ...

  5. MySQL学习笔记之一

    MySQL装有一个名为mysql的命令行,在提示符下输入mysql将出现如下的简单提示: ➜ ~ mysql Welcome to the MySQL monitor. Commands end wi ...

  6. poj 1167 简单搜索

    这题主要是注意好限定的条件 条件1:每个公交车都至少要到达两次 条件2:公交车相同时间和相同间隔是属于两种车辆 条件3:不同的车可能到达时间相同 上述都是深搜的重要条件: #include<al ...

  7. Sharepoint数据库存储过程

    转:http://dugan.bokee.com/630497.html Databases Table Stored Procedures(数据库表存储过程) Globals Table Store ...

  8. STL总结之functor

    STL中仿函数是重要的组成部分.所谓的仿函数就是通过重载括号运算符实现的, 如下: STL库中都是泛型仿函数如小于操作: STL中定义了许多有用的操作,如less(小于), less_equal(小于 ...

  9. HDU-1896 Stones

    http://acm.hdu.edu.cn/showproblem.php?pid=1896 题意:一个人从0开始走起,遇到偶数个石头就踢.要是同一位置有多个石头,则先扔最重的石头(也就是扔的最近的那 ...

  10. 个性CMD设置方法(转载)

    原文地址http://wenku.baidu.com/link?url=DB8X-eHwE_VGtggBmKsBimdzXeGI_6Ga90W9PmX2Px2eUqdXOnq7FhEIzsqBfTqT ...