Splay 模板
Splay 模板
struct SplayTree{
const static int maxn = 1e5 + 15;
int ch[maxn][2] , key[maxn] , s[maxn] , tot , root , fa[maxn];
void init( int x , int val = 0 , int par = 0 ){
ch[x][0]=ch[x][1]=0 , fa[x]= par , key[x] = val , s[x] = 1;
}
void init(){
init( 0 , 0 , 0 ); s[0] = 0;
tot = root = 0 ;
}
inline void up(int x){
s[x] = s[ch[x][0]] + s[ch[x][1]] + 1;
}
void rotate( int x, int d ){
int y = fa[x];
ch[y][d ^ 1] = ch[x][d];
if ( ch[x][d]) fa[ch[x][d]] = y;
fa[x] = fa[y];
if (fa[y]){
if (y == ch[fa[y]][d]) ch[fa[y]][d] = x;
else ch[fa[y]][d ^ 1] = x;
}
ch[x][d] = y , fa[y] = x;
up( y ) , up( x );
}
// Splay x to target's son
void Splay( int x , int target ){
while( fa[x] != target ){
int y = fa[x];
if( x == ch[y][0] ){
if( fa[y] != target && y == ch[fa[y]][0])
rotate( y , 1 );
rotate( x , 1 );
}else{
if( fa[y] != target && y == ch[fa[y]][1])
rotate( y , 0 );
rotate( x , 0 );
}
}
if( !target ) root = x;
}
void Insert( int & t , int val , int par = 0 ){
if( t == 0 ){
t = ++ tot;
init( t , val , par );
Splay( tot , 0 );
}else{
int cur = t;
if( val < key[t] ) Insert( ch[t][0] , val , cur );
else Insert( ch[t][1] , val , cur );
up( cur );
}
}
// Return point
int find( int t , int v ){
if( t == 0 ) return 0;
else if( key[t] == v ){
Splay( t , 0 );
return t;
}
else if( v < key[t] ) return find( ch[t][0] , v );
return find( ch[t][1] , v );
}
// Delete Root
void Delete(){
if( !ch[root][0] ){
fa[ ch[root][1] ] = 0 ;
root = ch[root][1];
}else{
int cur = ch[root][0];
while( ch[cur][1] ) cur = ch[cur][1];
Splay( cur , root );
ch[cur][1] = ch[root][1];
root = cur , fa[cur] = 0 , fa[ch[root][1]] = root;
up( root );
}
}
int size(){
return s[root];
}
// 查第 k 小 , 必须保证合法
int kth( int x , int k ){
if( k == s[ch[x][0]] + 1 ){
Splay( x , 0 );
return key[x];
}
else if( k <= s[ch[x][0]] ) return kth( ch[x][0] , k );
else return kth( ch[x][1] , k - s[ch[x][0]] - 1 );
}
//找前驱
int pred( int t , int v ){
if( t == 0 ) return v;
else{
if( v <= key[t] ) return pred( ch[t][0] , v );
else{
int ans = pred( ch[t][1] , v );
if( ans == v ){
ans = key[t];
Splay( t , 0 );
}
return ans;
}
}
}
/*int less( int t , int v ){
if( t == 0 ) return 0;
int rs = 0;
if( v <= key[t] ) rs = less( ch[t][0] , v );
else rs = s[ch[t][0]] + 1 + less( ch[t][1] , v );
if( Tl ){
Splay( t , 0 );
Tl = 0;
}
return rs;
}*/
//找后继
int succ( int t , int v ){
if( t == 0 ) return v;
else{
if( v >= key[t] ) return succ( ch[t][1] , v );
else{
int ans = succ( ch[t][0] , v );
if( ans == v ){
ans = key[t];
Splay( t , 0 );
}
return ans;
}
}
}
void Preorder( int t ){
if( !t ) return;
Preorder( ch[t][0] );
printf("%d " , key[t] );
Preorder( ch[t][1] );
}
}splay;
Splay 模板的更多相关文章
- bzoj 1588 splay模板题
用晚自习学了一下splay模板,没想象中那么难,主要是左旋和右旋可以简化到一个函数里边,减少代码长度... #include<iostream> #include<cstdio> ...
- COJ 1002 WZJ的数据结构(二)(splay模板)
我的LCC,LCT,Splay格式终于统一起来了... 另外..这个形式的Splay是标准的Splay(怎么鉴别呢?看Splay函数是否只传了一个变量node就行),刘汝佳小白书的Splay写的真是不 ...
- [luogu3369/bzoj3224]普通平衡树(splay模板、平衡树初探)
解题关键:splay模板题整理. 如何不加入极大极小值?(待思考) #include<cstdio> #include<cstring> #include<algorit ...
- BZOJ1588 [HNOI2002]营业额统计 splay模板
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Submit: 16189 Solved: 6482 [Submit][S ...
- 文艺平衡树(splay模板)
题干:splay模板,要求维护区间反转. splay是一种码量小于treap,但支持排名,前驱后继等treap可求的东西,也支持区间反转的平衡树. 但是有两个坏处: 1.splay常数远远大于trea ...
- [洛谷P3391] 文艺平衡树 (Splay模板)
初识splay 学splay有一段时间了,一直没写...... 本题是splay模板题,维护一个1~n的序列,支持区间翻转(比如1 2 3 4 5 6变成1 2 3 6 5 4),最后输出结果序列. ...
- bzoj 1208 splay模板题2
自己yy了找前驱和后继,学了学怎么删除...(反正就是练模板) #include<iostream> #include<cstdio> #include<cstring& ...
- 【BZOJ 3196】二逼平衡树 线段树套splay 模板题
我写的是线段树套splay,网上很多人写的都是套treap,然而本蒟蒻并不会treap 奉上sth神犇的模板: //bzoj3196 二逼平衡树,支持修改某个点的值,查询区间第k小值,查询区间某个值排 ...
- 【BZOJ 3188】【Coci 2011】Upit Splay模板题
转啊转终于转出来了,然而我的模板跟陈竞潇学长的模板一模一样,还是太弱啊,第一次用指针. #include<cstdio> #include<cstring> #include& ...
随机推荐
- redis安装(linux)
一.redis安装步骤 1.yum install gcc 如果你机器已经安装了编译环境请忽略,否则在使用make编译源码时会报错. 报错信息:make: *** [adlist.o] 2.使用w ...
- STM32 IAP升级
STM32 IAP在线升级,用Jlink设置读保护后前5K字节是默认加了写保护的,导致IAP升级时擦除和写入FLASH不成功,可以做两个boot,前5k为第一个boot程序,上电时负责跳转到APP还是 ...
- linux查看内存、CPU占用资源最多的进程
[内存占用] #利用ps命令,默认使用ps参数会显示的结果 ps -aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 ...
- Linux网络综合命令——IP
1.作用 ip是iproute2软件包里面的一个强大的网络配置工具,它能够替代一些传统的网络管理工具,例如ifconfig.route等,使用权限为超级用户.几乎所有的Linux发行版本都支持该命令. ...
- Java OOM学习
转载自原文: 什么是java OOM?如何分析及解决oom问题? 什么是OOM? OOM,全称"Out Of Memory",翻译成中文就是"内存用完了",表现 ...
- Failed to create the Java Virtual Machine
启动Zend Studio时出现Failed to create the Java VIrtual Machine 解决办法如下.打开安装目录下的ZendStudio.ini配置文件,作如下修改: 说 ...
- Python装饰器讲解
Python装饰器讲解 定义:本质是函数,就是为其他函数添加附加功能.原则:1.不能修改被装饰的函数的源代码 2.不能修改被装饰的函数的调用方式 import time def timmer(func ...
- hdu 5137 去掉一个点 使得最短路最大(2014广州现场赛 K题)
题意:从2~n-1这几个点中任意去掉一个点,使得从1到n的最短路径最大,如果任意去掉一个点1~n无通路输出Inf. Sample Input4 51 2 31 3 71 4 502 3 43 4 23 ...
- day9--回顾
线程 vs 进程 进程:一堆资源集的集合.线程:操作系统能够调度的最小单位. 进程和线程的谁快是误区,进程至少包含一个线程,是没有可比性的. 线程:共享内存,两个线程同时操作一个数据,要加锁.全 ...
- javascript json字符串转json对象方法
/* * @method 将拼接好字符串格式的json 转成json对象 * @param jsonData param fomart: * var jsonData = "{name1:' ...