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[ ...
随机推荐
- SQL Server 一些关键字详解(二)
1.LEFT JOIN 容易让人误解的地方 背景:因为在网上搜了下 LEFT JOIN 和 OUTER APPLY 的区别,时发现,有的网友解释为: 1) A left join B 的连接 ...
- Oracle Database Concepts:介绍模式对象(Introduction to Schema Objects)
数据库模式(schema)是数据结构的逻辑容器,被称作模式对象(schema objects) 每一个数据库用户拥有一个和用户名相同的模式,例如hr用户拥有hr模式. 在一个产品数据库中,模式的拥有者 ...
- Helloworld模块之内核makefile详解
Hello World 模块以及对应的内核makefile详解 hello.c: #include <linux/module.h> //所有模块都需要的头文件 #include < ...
- Redbean:入门(四) - 反射机制 以及 事务
<?php //引入rb入口文件 include_once 'rb.php'; //定义dsn以及相关的数据 $dsn = 'mysql:host=localhost;dbname=hwibs_ ...
- Android-简单的sdcard文件浏览
功能:能够浏览手机里面的文件夹和文件,代码灰常简单 先看布局 <LinearLayout xmlns:android="http://schemas.android.com/apk/r ...
- openssl AES加密以及padding
好习惯,先上代码再说事 加密 void AesEncrypt(unsigned char* pchIn, int nInLen, unsigned char *ciphertext, int & ...
- WPF窗口长时间无人操作鼠标自动隐藏
在软件开发中有时会有等待一段时间无人操作后隐藏鼠标,可能原因大致如下: 1.为了安全性,特别是那些需要用到用户名和密码登录服务端的程序,常常考虑长期无人操作,程序自动跳转到用户登录界面: 2.软件为了 ...
- 005--VS C++ 加载位图
//全局变量 HDC mdc; //--------------------------------------------InitInstance() 函数--------------------- ...
- Qt:禁止qDebug的输出
Qt:禁止qDebug的输出 在工程的.pro文件里加上以下编译批令即可: DEFINES += QT_NO_DEBUG_OUTPUT
- Actionform
Actionform 2013年7月8日 15:08 Reset 用actionform是把数据恢复到初始状态 Getter/setter Validate 验证 已使用 Microsoft OneN ...