题意:让你维护一个集合,有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的更多相关文章

  1. [Liferay6.2]Liferay Dynamic Query API示例

    介绍 Liferay提供了几种方法定义复杂的查询用来检索数据库中的数据. 通常情况下,在每个service Entity中,通过定义一些'finder'方法,可以便捷地满足基本的数据查询操作. 但是, ...

  2. Mybatis Dynamic Query 框架整合

    项目地址:https://github.com/wz2cool/mybatis-dynamic-query 文档地址:https://wz2cool.gitbooks.io/mybatis-dynam ...

  3. 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 ...

  4. Mybatis Dynamic Query 1.0.2版本

    项目地址:https://github.com/wz2cool/mybatis-dynamic-query 文档地址:https://wz2cool.gitbooks.io/mybatis-dynam ...

  5. Mybatis Dynamic Query 2.0 入门

    简介 2.0 昨天打包好了,主要是整合了tk.mybatis.mapper 到项目中去,所以和1.x比起来主要多了一个通用mapper.因为作者主要是使用springboot 这里讲一下Springb ...

  6. Mybatis Dynamic Query 2.0.2

    项目地址:https://github.com/wz2cool/mybatis-dynamic-query 文档地址:https://wz2cool.gitbooks.io/mybatis-dynam ...

  7. CH#24C 逃不掉的路 和 HDU3686 Traffic Real Time Query System

    逃不掉的路 CH Round #24 - 三体杯 Round #1 题目描述 现代社会,路是必不可少的.任意两个城镇都有路相连,而且往往不止一条.但有些路连年被各种XXOO,走着很不爽.按理说条条大路 ...

  8. HDU 3686 Traffic Real Time Query System (图论)

    HDU 3686 Traffic Real Time Query System 题目大意 给一个N个点M条边的无向图,然后有Q个询问X,Y,问第X边到第Y边必需要经过的点有多少个. solution ...

  9. 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 ...

随机推荐

  1. WCF的三个名称/命名空间,你是否傻傻分不清楚?

    在定义和寄宿WCF服务的时候会面临三个名称/命名空间,它们分别是ServiceContractAttribute.ServiceBehaviorAttribute和Binding的Name和Names ...

  2. 系统安装LOL等游戏后出现VS调试报错"无法调试""拒绝访问"之类的调试错误

    一个问题抠得脑壳痛,度娘一番各种各样的答案.基本属于 1,调试权限被清0 2,文件权限问题   其中看到很多解决方案中提到"重启电脑"的说法.我也试了几次不行甚至游戏都卸载了.后来 ...

  3. DevExpress 13.1.8全面支持VS2013

    界面套包DevExpress 13.1.8重磅来袭.从这个版本开始所有.NET控件均正式支持VS2013,当然还有很多其他更新,下面是部分更新内容: DevExpress所有.NET控件: 正式支持V ...

  4. sharepoint 开发相关工具总结

    1.CAML Designer 2013 开发caml用 http://biwug-web.sharepoint.com/SitePages/Caml_designer.aspx 2.SharePoi ...

  5. 1分钟实现Autodesk Vault登录对话框

      .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courie ...

  6. Android Handler机制(三)----Looper源码解析

    一.Looper Looper对象,顾名思义,直译过来就是循环的意思,从MessageQueue中不断取出message. Class used to run a message loop for a ...

  7. Android Builder模式在开发中的应用

    最近在学习图片加载框架Glide的时候,被他精简的写法震惊了.一句话,就可以搞定. Glide.with(mContext) .load(url) .centerCrop() .placeholder ...

  8. 开发Android系统内置应用小记

    Android系统内置应用可以使用更多的API.更高的权限,与开发普通应用最大的差别在于编译,内置应用编译需要用到Android.mk文件.下面是我在开发过程中的一些小记. 1.在AndroidMai ...

  9. Android 的系统架构

    Android 的系统架构 Android其本质就是在标准的Linux系统上增加了Java虚拟机Dalvik,并在Dalvik虚拟机上搭建了一个JAVA的application framework,所 ...

  10. Android 从Gallery获取图片

    本文主要介绍Android中从Gallery获取图片 设计项目布局 <LinearLayout xmlns:android="http://schemas.android.com/ap ...