UESTC 395 Dynamic Query System --Treap
题意:让你维护一个集合,有8种操作:
1. I x 插入一个数
2. R x 删除x
3. S 输出总的数个数(集合大小)
4. L x 查询小于x的数的个数
5. W k 查询集合中数从小到大排列的第k个数
6. C x 查询x的个数
7. MI 查询集合中最小的数
8. MA 查询集合中最大的数
一道平衡树模拟的裸题,直接套版然后改下细节就行了。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std; struct node
{
node *ch[];
int v,r,sz,cnt;
void UP(){ sz = ch[]->sz + ch[]->sz + cnt; }
int cmp(int x)const
{
if(x == v) return -;
return x > v;
}
}*null,*root; void create(node *&o,int v=)
{
o = new node();
o->sz = o->cnt = ;
o->v = v;
o->r = rand();
o->ch[] = o->ch[] = null;
} void rotate(node *&o,int d)
{
node *p = o->ch[d];
o->ch[d] = p->ch[d^];
p->ch[d^] = o;
o->UP(),p->UP();
o = p;
} int countx(node *o,int v)
{
while(o != null)
{
int d = o->cmp(v);
if(d == -)
return o->cnt;
o = o->ch[d];
}
return ;
} void insert(node *&o,int v)
{
if(o == null)
{
create(o,v);
return;
}
int d = o->cmp(v);
if(d == -)
{
o->cnt++;
o->UP();
return;
}
insert(o->ch[d],v);
if(o->r > o->ch[d]->r)
rotate(o,d);
if(o != null)
o->UP();
} void del(node *&o,int v)
{
if(o == null) return;
int d = o->cmp(v);
if(d == -)
{
if(o->cnt > )
o->cnt--;
else
{
if(o->ch[] == null) o = o->ch[];
else if(o->ch[] == null) o = o->ch[];
else
{
int d2 = o->ch[]->r < o->ch[]->r;
if(o->ch[d2] == null)
{
delete o;
o = null;
return;
}
rotate(o,d2);
del(o->ch[d2^],v);
}
}
}
else del(o->ch[d],v);
if(o != null)
o->UP();
} int Kth(node *o,int k)
{
if(o == null || k == || k > o->sz) return -;
int left = o->ch[]->sz;
if(k > left && k <= left + o->cnt)
return o->v;
if(k > left + o->cnt)
return Kth(o->ch[],k-left-o->cnt);
return Kth(o->ch[],k);
} int Count(node *o,int v)
{
if(o == null) return ;
int left = o->ch[]->sz;
if(o->v == v) return left;
else if(v < o->v)
return Count(o->ch[],v);
return Count(o->ch[],v)+left+o->cnt;
} void init()
{
create(null);
null->sz = ;
root = null;
} int main()
{
int t,n,i,x;
char ss[];
scanf("%d",&t);
while(t--)
{
init();
scanf("%d",&n);
while(n--)
{
scanf("%s",ss);
if(ss[] == 'M')
{
if(ss[] == 'I')
printf("%d\n",Kth(root,));
else
printf("%d\n",Kth(root,root->sz));
}
else if(ss[] == 'I')
{
scanf("%d",&x);
insert(root,x);
}
else if(ss[] == 'S')
{
printf("%d\n",root->sz);
}
else if(ss[] == 'R')
{
scanf("%d",&x);
del(root,x);
}
else if(ss[] == 'L')
{
scanf("%d",&x);
printf("%d\n",Count(root,x));
}
else if(ss[] == 'W')
{
scanf("%d",&x);
printf("%d\n",Kth(root,x));
}
else if(ss[] == 'C')
{
scanf("%d",&x);
printf("%d\n",countx(root,x));
}
}
}
return ;
}
UESTC 395 Dynamic Query System --Treap的更多相关文章
- [Liferay6.2]Liferay Dynamic Query API示例
介绍 Liferay提供了几种方法定义复杂的查询用来检索数据库中的数据. 通常情况下,在每个service Entity中,通过定义一些'finder'方法,可以便捷地满足基本的数据查询操作. 但是, ...
- Mybatis Dynamic Query 框架整合
项目地址:https://github.com/wz2cool/mybatis-dynamic-query 文档地址:https://wz2cool.gitbooks.io/mybatis-dynam ...
- HDU 3686 Traffic Real Time Query System(双连通分量缩点+LCA)(2010 Asia Hangzhou Regional Contest)
Problem Description City C is really a nightmare of all drivers for its traffic jams. To solve the t ...
- Mybatis Dynamic Query 1.0.2版本
项目地址:https://github.com/wz2cool/mybatis-dynamic-query 文档地址:https://wz2cool.gitbooks.io/mybatis-dynam ...
- Mybatis Dynamic Query 2.0 入门
简介 2.0 昨天打包好了,主要是整合了tk.mybatis.mapper 到项目中去,所以和1.x比起来主要多了一个通用mapper.因为作者主要是使用springboot 这里讲一下Springb ...
- Mybatis Dynamic Query 2.0.2
项目地址:https://github.com/wz2cool/mybatis-dynamic-query 文档地址:https://wz2cool.gitbooks.io/mybatis-dynam ...
- CH#24C 逃不掉的路 和 HDU3686 Traffic Real Time Query System
逃不掉的路 CH Round #24 - 三体杯 Round #1 题目描述 现代社会,路是必不可少的.任意两个城镇都有路相连,而且往往不止一条.但有些路连年被各种XXOO,走着很不爽.按理说条条大路 ...
- HDU 3686 Traffic Real Time Query System (图论)
HDU 3686 Traffic Real Time Query System 题目大意 给一个N个点M条边的无向图,然后有Q个询问X,Y,问第X边到第Y边必需要经过的点有多少个. solution ...
- HDU3686 Traffic Real Time Query System 题解
题目 City C is really a nightmare of all drivers for its traffic jams. To solve the traffic problem, t ...
随机推荐
- [deviceone开发]-UI组件的动画示例
一.简介 自定义组件模版(头部,按钮,加减数量,加载,底部弹出,开关(文字/无文字),选项卡(2-4), radio)全部带自定义动画效果,需从组件商店中添加:do_Animator组件 二.效果图 ...
- js资源加载优化
互联网应用或者访问量大的应用,对js的加载优化是不可少的.下面记录几种优化方法 CDN + 浏览器缓存 CDN(content delivery network)内容分发网络, 最传统的优化方式.其 ...
- 总结CSS3新特性(媒体查询篇)
CSS3的媒体查询是对CSS2媒体类型的扩展,完善; CSS2的媒体类型仅仅定义了一些设备的关键字,CSS3的媒体查询进一步扩展了如width,height,color等具有取值范围的属性; medi ...
- ArcGIS Desktop 遇到严重的应用程序错误
由于项目初验,忙了几个月(感觉忙得并不值),好久都没更新博客了. 一.问题 在关闭ArcMap时,ArcGIS Desktop 遇到严重的应用程序错误.环境是Windows 10,新装的系统.以前出现 ...
- Java核心:类加载和JVM内存的分配
类的加载: 指的是将class文件的二进制数据读入到运行时数据区(JVM在内存中划分的) 中,并在方法区内创建一个class对象. 类加载器: 负责加载编译后的class文件(字节码文件)到JVM(J ...
- 【代码笔记】iOS-禁止输入表情符号
一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...
- nexus搭建maven私服
下载nexus 首先,从以下地址下载nexus: http://www.sonatype.com/download-oss-sonatype 选择下载nexus-2.13.0-01-bundle.ta ...
- Windows Phone 8.0 Updates 2 and 3模拟器更新
2014年元旦后,微软发布了Windows Phone 8 Updates 2 and 3的模拟器更新,即系统版本号分别是8.0.10322和 8.0.10512.其中,在Update 3 Emula ...
- crontab执行不生效-【问题篇】
背景:shell脚本每隔两分钟从数据库取数据库放到脚本所在目录,做好计划任务发现不生效. 解决:脚本中文件路径问题 测试:在/data/test目录下写的脚本,直接在本目录touch以分钟结尾的文件. ...
- c# UpdateLayeredWindow异形窗口
#region UpdateLayeredWindow #region 重写窗体的 CreateParams 属性 protected override CreateParams CreatePara ...