【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 ...
随机推荐
- Easy sssp(spfa)(负环)
vijos 1053 Easy sssp 方法:用spfa判断是否存在负环 描述 输入数据给出一个有N(2 <= N <= 1,000)个节点,M(M <= 100,00 ...
- Ubuntu 16.04安装微信
微信没有出Linux的版本,但是可以通过以下方式解决: 1.使用网页版,除了没有公众号之后,一切都没问题,包括传文件等. 网页登录地址:https://wx.qq.com/ 2.使用第三方版本,只不过 ...
- aSmack连接server异常smack.SmackException$ ConnectionException thrown by XMPPConnection.connect();
以下是我在研究asmack4.0出现的异常 06-17 12:02:56.924: W/System.err(10622): org.jivesoftware.smack.SmackException ...
- C#如何实现挂机锁
首先在主窗体中设置一个子窗体的实例,然后当点击挂机之后,隐藏当前窗体,同时显示子窗体. 把子窗体的背景窗体设置如下属性(主要是背景随便改成一个图片,然后FormBorderStyle改成None, ...
- GridView的经常使用属性
1.android:numColumns="auto_fit" //GridView的列数设置为自己主动 2.android:columnWidth="90dp &q ...
- ng-options bug解决方案(示例)
情况: 无法获取 ng-model 的值 解决方案: 绑定到对象的属性值上 1.页面 <ion-view hide-nav-bar="true"> <ion-co ...
- BusHelper
https://github.com/eltld/-BusHelper https://github.com/eltld/BusLineSAX
- centos 安装php7 memcached扩展
在使用下面这种方式安装php7 memcached扩展时,先要安装好memcached服务器yum -y install php70w-pecl-memcached
- 2016/06/13 phpexcel 未完待续
①准备工作: 1,php版本不能太低 2,去官网下载PHPExcel插件 http://phpexcel.codeplex.com/ 3,解压后提取classes文件夹到工作目录,并重命名为PH ...
- String池与iterator对集合的迭代
一.静态导入 1. 导入类的静态属性 import static java.lang.System.out; out.println("haha"); 2. ...