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 ...
随机推荐
- iOS加密之MD5加密
话不多说,上代码! MyMD5.h里面 #import <Foundation/Foundation.h> @interface MyMD5 : NSObject { } +(NSStri ...
- Linux新手扫盲
一. Linux特点 1.免费/开源: 2.支持多线程/多用户: 3.安全性好: 4.对内存和文件管理优越. Linux最小只需4M ——> 嵌入式开发 二. 文件目录 Linux系统所有软硬件 ...
- Xcode中的常用快捷键
新建项目 com + shift +N 新建文件 com + N 偏好设置 通用 com + , 跳到指定行 com + L 当前行加断点 com + \ 移动编辑区最上方 ...
- 操作系统开发系列—12.g.在内核中设置键盘中断
8259A虽然已经设置完成,但是我们还没有真正开始使用它呢. 所有的中断都会触发一个函数spurious_irq(),这个函数的定义如下: PUBLIC void spurious_irq(int i ...
- iOS 开发之路(登陆验证调用WebService)二
swift3.0下使用Alamofire调用Webservice遇到的一些问题以及解决方案. 首先是针对没有证书的https下的接口处理问题(ps:不推荐在正式版本中使用),manager.reque ...
- 【代码笔记】iOS-点击顶点处,弹出另一个小的界面
一,效果图. 二,文件目录. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewControlle ...
- 【Android】HorizontalScrollView内子控件横向拖拽
前言 网上ListView上下拖动的例子有,效果也很好,但是项目要横着拖的,只要硬着头皮自己写(主要是没找到合适的),参考文章1修改而来,分享一下. 声明 欢迎转载,但请保留文章原始出处:) 博客园 ...
- Ubuntu下面su初始密码设置
rcm@rcm:~$ sudo passwd 输入新的 UNIX 密码: 重新输入新的 UNIX 密码: passwd:已成功更新密码
- rsa && sha1 js code
jsbn.js /* * Copyright (c) 2003-2005 Tom Wu * All Rights Reserved. * * Permission is hereby granted, ...
- MicroStation VBA基础
实习笔记1 2016年8月1日 14:12 Option Explicit 缺省情况下,如果使用一个没有声明的变量,它将继承“Variant”类型.在模块.窗体和类的通用声明区使用“OptionExp ...