【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 ...
随机推荐
- HDU 4341 Gold miner(分组背包)
题目链接 Gold miner 目标是要在规定时间内获得的价值总和要尽可能大. 我们先用并查集把斜率相同的物品分在同一个组. 这些组里的物品按照y坐标的大小升序排序. 如果组内的一个物品被选取了,那该 ...
- oracle存储过程中使用字符串拼接
1.使用拼接符号“||” v_sql := 'SELECT * FROM UserInfo WHERE ISDELETED = 0 AND ACCOUNT =''' || vAccount || '' ...
- 使用Google浏览器做真机页面调试
步骤1: 从Windows,Mac或Linux计算机远程调试Android设备上的实时内容.本教程将教您如何: 设置您的Android设备进行远程调试,并从开发机器中发现它.从您的开发机器检查和调试A ...
- Mysql数据库中CURRENT_TIMESTAMP和ON UPDATE CURRENT_TIMESTAMP区别
如图所示,mysql数据库中,当字段类型为timestamp时,如果默认值取CURRENT_TIMESTAMP,则在insert一条记录时,end_time的值自动设置为系统当前时间,如果勾选了 ON ...
- [转]Go基础之锁的初识
当我们的程序就一个线程的时候是不需要用到锁的,但是通常我们实际的代码不会是单个线程的,所有这个时候就需要用到锁了,那么关于锁的使用场景主要涉及到哪些呢? 当我们多个线程在读相同的数据的时候则是需要加锁 ...
- 【转】How to Change File Ownership & Groups in Linux
有关linux下 文件权限的问题,一直不是很清楚.(参考菜鸟教程: http://www.runoob.com/linux/linux-file-attr-permission.html) 像上面这样 ...
- HDU 2009 整除的尾数 题解
Problem Description 一个整数,仅仅知道前几位,不知道末二位.被还有一个整数除尽了.那么该数的末二位该是什么呢? Input 输入数据有若干组,每组数据包括二个整数a,b(0&l ...
- linux系统之shell编程-正則表達式
shell编程正則表達式: 1:元字符 [ ] . * ? + ( ) | { } ^ $ 2 : [a-z0-9] 表示匹配随意数字和字母的一个 3 : [^a-z] ...
- STL algorithm算法max,max_elements(33)
max原型: std::max C++98 C++11 C++14 default (1) template <class T> const T& max (const T& ...
- npm ERR! code EINTEGRITY npm ERR! sha1- 报错解决办法
npm ERR! code EINTEGRITY npm ERR! sha1- 报错日志 npm ERR! code EINTEGRITY npm ERR! sha1-OGchPo3Xm/Ho8jAM ...