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 ...
随机推荐
- 对于 APM 用户的一次真实调查分析(下)
一.前言 对 APM 用户的一次真实调查分析(上)中,我们主要聊到了现阶段国外 APM 行业对各个企业的渗透率.大部分使用 APM 工具的企业规模以及 APM 工具在用户心中的地位等问题,有兴趣的朋友 ...
- UPUPW PHP环境集成包
UPUPW PHP环境集成包 http://www.upupw.net/
- android activity在横竖屏切换的时候不重新调用onCreate方法
在安卓系统中,横竖屏切换会默认重新调用onCreate等生命周期方法,如果此时有一些临时数据没有保存下来,很有可能会导致该数据丢失. 因此我们可以进行以下设置,来避免恒切换时重新调用onCreate方 ...
- 项目管理系统 SQL2005数据库查询CPU100%问题研究
[一篮饭特稀原创,转载请注明出自http://www.cnblogs.com/wanghafan/p/4595084.html] 在项目管理系统中出现查询工程明细出现CPU100%卡死症状: 1.打 ...
- 145. Binary Tree Postorder Traversal
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...
- gdb查看虚函数表、函数地址
1. 查看函数地址 看函数在代码的哪一行,使用info line就可以看到类似下面这中输出 点击(此处)折叠或打开 (gdb) info line a.cpp:10 Line 10 of &q ...
- 【HDOJ】1068 Girls and Boys
匈牙利算法,最开始暴力解不知道为什么就是wa,后来明白,一定要求最优解.查了一下匈牙利算法相关内容,大致了解. #include <stdio.h> #include <string ...
- Oracle EBS R12 WIP Component Issue&Return Process
oracleassemblytransactionscomponentsjobsreference 目录(?)[-] 定义BOM 定义Routing 定义WIP Discrete Job 发料 Mat ...
- eclipse 代码中突然出现特殊字符
在写代码的时候,不知道点到了 eclipse 的哪个属性,代码中就出现了一些特殊字符,也不能删除. 请问,在 eclipse 中该怎么设置,才能将这些字符去掉. 如下图所示: 解决方法: 选择Wind ...
- 分享你最喜欢的技巧和提示(Xcode,Objective-C,Swift,C...等等)
http://www.cocoachina.com/ios/20151231/14846.html 笔者分享总结如下(本篇会不定期进行更新) : Objective-C 1.让Xcode的控制台支持L ...