Treap模板
平衡树总是有用的,set由于过度封装没有办法实现找比x小的元素有多少个,这就显得很不方便了,所以封装了个Treap,万一以后用的着呢- -01
#pragma warning(disable:4996)
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std; #define maxn 420000
const int inf = ~0U >> 1;
struct Node
{
int val, key, size; // value stored,priority key,size of total,number of current value
Node *ch[2];
Node(){
val = size = 0;
key = inf;
}
void upd(){
size = ch[0]->size + ch[1]->size + 1;
}
}; Node mem[maxn], *C = mem; Node *make(int v,Node *p){
C->ch[0] = C->ch[1] = p;
C->val = v; C->key = rand() - 1;
C->size = 1;
return C++;
} Node *make_null(){
C->ch[0] = C->ch[1] = 0;
C->val = 0; C->key = inf;
C->size = 0;
return C++;
} struct Treap
{
private:
Node *root, *null;
void rot(Node *&u, int d){
Node *v = u->ch[d];
u->ch[d] = v->ch[!d];
v->ch[!d] = u;
u->upd(); v->upd();
u = v;
}
void insert(Node *&u, int k){
if (u == null) u = make(k, null);
else if (u->val == k) return;
else{
int d = k > u->val;
Node *&v = u->ch[d];
insert(v, k);
if (v->key < u->key) rot(u, d);
}
u->upd();
}
void erase(Node *&u, int k){
if (u == null) return;
if (u->val == k){
int d = u->ch[1]->key < u->ch[0]->key;
if (u->ch[d] == null) {
u = null; return;
}
rot(u, d);
erase(u->ch[!d], k);
}
else erase(u->ch[k>u->val], k);
u->upd();
}
// left side has size of k
Node *select(Node *u, int k){
int r = u->ch[0]->size;
if (k == r)
return u;
if (k < r) return select(u->ch[0], k);
return select(u->ch[1], k - r - 1);
}
// return the number of elements smaller than x
int rank(Node *u, int x){
if (u == null) return 0;
int r = u->ch[0]->size;
if (x == u->val) return r;
else if (x < u->val) return rank(u->ch[0], x);
else return r + 1 + rank(u->ch[1], x);
}
bool find(Node *u, int x){
if (u == null) return false;
if (x == u->val) return true;
else return find(u->ch[x>u->val], x);
}
public:
Treap(){
null = make_null();
root = null;
}
void init(){
null = make_null();
root = null;
}
void insert(int x){
insert(root, x);
}
void erase(int x){
erase(root, x);
}
int select(int k){
if (k > root->size) return -inf;
else return select(root, k - 1)->val;
}
// return the element that is smaller than x
int rank(int x){
return rank(root, x);
}
// return whether x exist
bool find(int x){
return find(root, x);
}
}treap; int main()
{
int m; scanf("%d\n", &m);
char cmd;
int x;
while (m--){
scanf("%c %d\n", &cmd, &x);
if (cmd == 'I') treap.insert(x);
else if (cmd == 'D') treap.erase(x);
else if (cmd == 'K') {
int ans = treap.select(x);
if (ans == -inf) printf("invalid\n");
else printf("%d\n", ans);
}
else{
printf("%d\n", treap.rank(x));
}
}
return 0;
}
Treap模板的更多相关文章
- BZOJ 1588: Treap 模板
		
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 12171 Solved: 4352 Description ...
 - [luogu3369]普通平衡树(treap模板)
		
解题关键:treap模板保存. #include<cstdio> #include<cstring> #include<algorithm> #include< ...
 - Treap 模板 poj1442&hdu4557
		
原理可以看hihocoder上面的讲解,很清楚,不多说了. 模板抄lrj训练指南上面的. /** Treap 实现 名次树 功能: 1.找到排名为k的元素 2.值为x的元素的名次 初始化:Node* ...
 - 平衡树Treap模板与原理
		
这次我们来讲一讲Treap(splay以后再更) 平衡树是一种排序二叉树(或二叉搜索树),所以排序二叉树可以迅速地判断两个值的大小,当然操作肯定不止那么多(不然我们还学什么). 而平衡树在排序二叉树的 ...
 - POJ1442-查询第K大-Treap模板题
		
模板题,以后要学splay,大概看一下treap就好了. #include <cstdio> #include <algorithm> #include <cstring ...
 - Treap 模板
		
感觉平衡树也没有以前想的那么玄乎,(其实set超好用的),非旋式Treap挺好理解,和可并堆,二叉搜索树有很大联系 推荐博客:http://memphis.is-programmer.com/post ...
 - 【Treap模板详细注释】BZOJ3224-普通平衡树
		
模板题:D错因见注释 #include<iostream> #include<cstdio> #include<cstring> #include<algor ...
 - 非旋treap模板
		
bzoj3580 非旋转treap 在大神教导下发现split一段区间时先split右边再split左边比较好写 #include <cstdio> #include <cstdli ...
 - codevs 4543 treap 模板
		
type rec=record lc,rc,v,rnd,size,w,fa:longint; end; var n,root,tot,ans,opt,x,i,po:longint; tr:array[ ...
 
随机推荐
- SRF之权限控制
			
框架目前提供url访问.菜单和页面元素的权限控制和数据权限,权限基于角色来分配,1个用户可以属于多个角色,权限项分模块.页面.操作3级别,其中模块.页面用于url和菜单的控制,操作是对页面元素的控制. ...
 - Python学习教程(learning Python)--3.3.1 Python下的布尔表达式
			
简单的说就是if要判断condition是真是假,Python和C语言一样非0即真,所以如果if的condition是布尔表达式我们可以用True或者非0数(不可是浮点数)表示真,用False或者0表 ...
 - equals函数
			
equals函数在Object类当中,而Object类是所有类的父类,所以所有的类里面都有equals函数. “==”操作符之前用于比较两个基本数据类型的值是否相等,而对于引用数据类型,“==”操作符 ...
 - DB2查询结果显示n行
			
在SQLserver中语法是这样的:select top n * from staff ,即可查询显示n行数据 但是在DB2中语法是这样的,感觉比较接近英语. select * from STAFF ...
 - 网站网页生成.shtml访问无法显示
			
网站换了服务器后发现shtml网页无法访问,原因是没有注册.shtml扩展名,解决方法如下 IIS6.0解析shtm,shtml文件由于IIS6.0的安全性较以前有特别大的改进,所以在很多功能默认情况 ...
 - Excel日期格式提取year
			
Excel日期格式提取year =TEXT(YEAR(C2),"0000")
 - eclipse创建android项目失败的问题 [ android support library ]
			
有根筋搭错了,想起来android应用开发???? 放下两年的手机应用开发,昨天有更新了android SDK, 重新搭建开发环境. 这两年android 变化真TM的大............... ...
 - ASP.NET2.0中对TextBox的Enable和ReadOnly属性的限制
			
在以前的ASP.NET 1.x版本中,设置为ReadOnly的TextBox控件在客户端更改了值后,在服务器端仍然可以得到修改后的值,但在ASP.NET 2.0中,这种做法已经限制.这是为了提高应用程 ...
 - linux之mysqlimport的哪些变态事儿
			
mysqlimport是MySQL导入数据的工具,高效易用. 但掌握不透彻就会有一些变态事情.mysqlimport --host='laswebapp.mdb.game.yy.com' --port ...
 - GNU make 总结 (五)
			
一.使用make更新静态库 静态库文件是一些.o文件的集合,在Linux中使用ar工具对它进行维护管理.一个静态库通常由多个.o文件组成,这些.o文件可独立的被作为一个规则的目标,库成员作为目标时需要 ...