【BZOJ 3224】 普通平衡树
【题目链接】
【算法】
本题是Splay模板题,值得一做!
【代码】
#include<bits/stdc++.h>
using namespace std;
#define MAXN 100000 int N,opt,x; template <typename T> inline void read(T &x) {
int f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
} template <typename T> inline void write(T x) {
if (x < ) { putchar('-'); x = -x; }
if (x > ) write(x/);
putchar(x%+'');
} template <typename T> inline void writeln(T x) {
write(x);
puts("");
} struct Splay {
int root,total;
struct Node {
int fa,son[],val,cnt,size;
} Tree[MAXN+];
bool get(int x) {
return Tree[Tree[x].fa].son[] == x;
}
inline void new_node(int index,int f,int x) {
Tree[index].fa = f;
Tree[index].son[] = Tree[index].son[] = ;
Tree[index].val = x;
Tree[index].cnt = Tree[index].size = ;
}
inline void update(int index) {
Tree[index].size = Tree[index].cnt;
Tree[index].size += Tree[Tree[index].son[]].size;
Tree[index].size += Tree[Tree[index].son[]].size;
}
inline void rotate(int x) {
int f = Tree[x].fa,g = Tree[f].fa,
tmpx = get(x),tmpf = get(f);
if (!f) return;
Tree[f].son[tmpx] = Tree[x].son[tmpx^];
if (Tree[x].son[tmpx^]) Tree[Tree[x].son[tmpx^]].fa = f;
Tree[x].son[tmpx^] = f;
Tree[f].fa = x;
Tree[x].fa = g;
if (g) Tree[g].son[tmpf] = x;
update(f);
update(x);
}
inline void splay(int x) {
int f;
for (f = Tree[x].fa; (f = Tree[x].fa); rotate(x))
rotate((get(x) == get(f)) ? (f) : (x));
root = x;
}
inline void Insert(int x) {
int index = root;
bool tmp;
if (!root) {
new_node(++total,,x);
root = total;
return;
}
while (true) {
if (Tree[index].val == x) {
++Tree[index].cnt;
splay(index);
return;
}
tmp = Tree[index].val < x;
if (!Tree[index].son[tmp]) {
new_node(++total,index,x);
Tree[index].son[tmp] = total;
splay(total);
return;
} else index = Tree[index].son[tmp];
}
}
inline int query_max(int index) {
while (true) {
if (!Tree[index].son[]) return index;
index = Tree[index].son[];
}
}
inline int query_min(int index) {
while (true) {
if (!Tree[index].son[]) return index;
index = Tree[index].son[];
}
}
inline void join(int x,int y) {
int pos = query_max(x);
splay(pos);
Tree[pos].son[] = y;
Tree[y].fa = pos;
}
inline void erase(int x) {
int index = root;
bool tmp;
while (true) {
if (Tree[index].val == x) {
if (Tree[index].cnt > ) {
--Tree[index].cnt;
splay(index);
return;
}
splay(index);
break;
}
tmp = Tree[index].val < x;
index = Tree[index].son[tmp];
}
if ((!Tree[index].son[]) && (!Tree[index].son[])) {
root = ;
return;
}
if (!Tree[index].son[]) {
root = Tree[index].son[];
Tree[root].fa = ;
return;
}
if (!Tree[index].son[]) {
root = Tree[index].son[];
Tree[root].fa = ;
return;
}
join(Tree[index].son[],Tree[index].son[]);
}
inline int query_rank(int x) {
int index = root,ans=;
while (true) {
if (Tree[index].val <= x) {
ans += Tree[Tree[index].son[]].size;
if (Tree[index].val == x) {
splay(index);
return ans;
}
ans += Tree[index].cnt;
index = Tree[index].son[];
} else index = Tree[index].son[];
}
}
inline int rank(int x) {
int index = root;
while (true) {
if (x <= Tree[Tree[index].son[]].size) index = Tree[index].son[];
else {
x -= Tree[Tree[index].son[]].size;
if (x <= Tree[index].cnt) {
splay(index);
return Tree[index].val;
}
x -= Tree[index].cnt;
index = Tree[index].son[];
}
}
}
inline int pred(int x) {
int ans;
Insert(x);
ans = Tree[query_max(Tree[root].son[])].val;
erase(x);
return ans;
}
inline int succ(int x) {
int ans;
Insert(x);
ans = Tree[query_min(Tree[root].son[])].val;
erase(x);
return ans;
}
} T; int main() {
read(N);
while (N--) {
read(opt); read(x);
if (opt == ) T.Insert(x);
else if (opt == ) T.erase(x);
else if (opt == ) writeln(T.query_rank(x));
else if (opt == ) writeln(T.rank(x));
else if (opt == ) writeln(T.pred(x));
else if (opt == ) writeln(T.succ(x));
} return ; }
【BZOJ 3224】 普通平衡树的更多相关文章
- Luogu 3369 / BZOJ 3224 - 普通平衡树 - [无旋Treap]
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 https://www.luogu.org/problemnew/show/P3 ...
- BZOJ 3224 普通平衡树(Treap模板题)
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 14301 Solved: 6208 [Submit][ ...
- Luogu 3369 / BZOJ 3224 - 普通平衡树 - [替罪羊树]
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 https://www.luogu.org/problemnew/show/P3 ...
- BZOJ 3224 - 普通平衡树 - [Treap][Splay]
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3224 Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中 ...
- bzoj 3224 普通平衡树 vactor的妙用
3224: Tyvj 1728 普通平衡树 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/ ...
- BZOJ 3224 普通平衡树
这个是第一份完整的treap代码.嗯...虽然抄的百度的版,但还是不错的. !bzoj上不能用srand. #include<iostream>#include<cstdio> ...
- BZOJ 3224 普通平衡树(树状数组)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3224 题意:维护以下操作:(1)插入x:(2)删除x(若有多个相同的数,只删除一个)(3 ...
- BZOJ 3224 普通平衡树 | 平衡树模板
#include <cstdio> #include <cmath> #include <cstring> #include <algorithm> # ...
- bzoj 3224 裸平衡树
裸的平衡树,可以熟悉模板用,写题写不出来的时候可以A以下缓解下心情. /************************************************************** P ...
- BZOJ 3224: Tyvj 1728 普通平衡树
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 9629 Solved: 4091[Submit][Sta ...
随机推荐
- windows安装SUSE Linux Enterprise Server 12
一:打开“开发人员模式” 点击开始菜单按钮,选择“设置” 在设置中选择“更新和安全” 在菜单中选择“针对开发人员”,在三个选项中,选中“开发人员模式” 在弹出的警告框中点击“是” 这样开发人员模式就打 ...
- Codeforces 123 E Maze
Discription A maze is represented by a tree (an undirected graph, where exactly one way exists betwe ...
- Kafka windows下的安装
1. 安装JDK 1.1 安装文件:http://www.oracle.com/technetwork/java/javase/downloads/index.html 下载JDK1.2 安装完成后需 ...
- Ubuntu 16.04安装JAD反编译工具(Java)
JAD反编译工具有个好处,就是字节码和源代码一起输出. 官网:https://varaneckas.com/jad/ 安装步骤: 1.下载: 离线版本:(链接: https://pan.baidu.c ...
- iOS WKWebView添加网页加载进度条(转)
一.效果展示 WKWebProgressViewDemo.gif 二.主要步骤 1.添加UIProgressView属性 @property (nonatomic, strong) WKWebView ...
- Android6.0权限管理以及使用权限该注意的地方
Android 6.0 Marshmallow首次增加了执行时权限管理,这对用户来说,能够更好的了解.控 制 app 涉及到的权限.然而对开发人员来说却是一件比較蛋疼的事情.须要兼容适配,并保证程序功 ...
- Camtasia Studio如何添加画中画
将录像文件和其他视频文件拖放到剪辑箱,右击录像文件(camrec文件)添加到时间轴,一般这个就是主要的视频文件,我们会在这个基础上添加字幕,配音,画中画等,拖进去之后可以发现多出来了一个视频1和音频1 ...
- 设计模式之命令模式(Command)摘录
23种GOF设计模式一般分为三大类:创建型模式.结构型模式.行为模式. 创建型模式抽象了实例化过程,它们帮助一个系统独立于怎样创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而 ...
- java中简单字符替换
在网络编程中,假设URL含有特殊字符,如空格.'#'等,server将无法识别导致无法获得正确的參数值.我们须要将这些特殊字符转换成server能够识别的字符,比如将空格转换成'%20'.给定一个字符 ...
- 如何快速上手一款新的嵌入式CPU芯片(记录CC2540开发经历)
新换了工作,需要熟悉新公司的产品开发项目,更新博客就懈怠了,不过环境的不同,也让我对嵌入式开发有了更深刻的理解.在原公司我主要负责在STM32F207芯片平台上, 利用UCOS+LWIP进行嵌入式服务 ...