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[ ...
随机推荐
- Ruby处理二进制(未完成)
https://practicingruby.com/articles/binary-file-formats http://stackoverflow.com/questions/16821435/ ...
- 算法系列9《MD5》
MD5即Message-Digest Algorithm 5(信息-摘要算法5),用于确保信息传输完整一致.是计算机广泛使用的杂凑算法之一(又译摘要算法.哈希算法),主流编程语言普遍已有MD5实现. ...
- oracle group 语句探究(笔记)
1.group by语句在oracle中没有排序功能,必须依靠order by才能实现按照预定结果的排序 2.group by 的cube扩展 with test as ( id, name from ...
- Iframe跨域Session丢失的问题
很久之前做的一个使用插件实现了图片批量上传,是通过IFrame加载上传面板的,使用google的chrome上传成功了就没怎么理了,最近同事测试时(使用的是360安全浏览器)老是出现上传不了图片的问题 ...
- iOS开发多线程篇—单例模式(ARC)
iOS开发多线程篇—单例模式(ARC) 一.简单说明: 设计模式:多年软件开发,总结出来的一套经验.方法和工具 java中有23种设计模式,在ios中最常用的是单例模式和代理模式. 二.单例模式说明 ...
- CentOs环境下PHP支持PDO_MYSQL
一.下载相应tgz包: http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz二.安装开始 解压# tar zxvf PDO_MYSQL-1.0.2.tgz # cd ...
- iOS8 超简单的设置圆角按钮 ImageView等UIView
button.layer.cornerRadius = // 这个值根据你想要的效果可以更改 button.clipsToBounds = true 这种方法不止可以设置按钮,UIView应该都可以设 ...
- ASP运行流程(主要的类笔记)
个人笔记:参考汤姆大叔的MVC之前那些事系列整理 client端发送页面请求,被IIS的某个进程截获,它根据申请的页面后缀(.aspx)不同,调用不同的页面处理程序(.asp->asp.dll ...
- Quartus13.0破解方法
一定要按照步骤顺序才能破解,这里很关键 1.下载和打开Quartus II破解器,选择“应用”,选择“是”,找到bin(64位系统是bin64)目录下的sys_cpt.dll,“打开” 2.然后将li ...
- 【转载】FPGA静态时序分析——IO口时序
转自:http://www.cnblogs.com/linjie-swust/archive/2012/03/01/FPGA.html 1.1 概述 在高速系统中FPGA时序约束不止包括内部时钟约束 ...