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& ...
随机推荐
- 【LinuxC】GCC编译C程序,关闭随机基址
1.编译.链接和运行程序 C代码示例: #include <stdio.h> #include <stdlib.h> int main() { printf("hel ...
- Oracle11g用户、权限、角色、概要文件管理及审计
第10章 安全管理 1 用户管理 2 权限管理 3 角色管理 : 4 概要文件管理 5 审计 操作系统:win7 Oracle安装目录:E盘 数据库名字:orcl 密码:123456 先 ...
- linux文件管理 -> 系统压缩打包
如果希望windows和Linux互相能使用的压缩工具, 建议.zip格式 压缩的好处主要有: 节省磁盘空间占用率 节省网络传输带宽消耗 网络传输更加快捷 Linux系统常见的后缀名所对应的压缩工具 ...
- urllib2使用初探
在入门urllib2之前,我想应该先调研一下urllib与urllib2的区别[1].首先我们要明白的是,这两个模块不可以相互替代.两者都是接受URL请求的模块,但是提供了不同的功能,两个显著的区别是 ...
- 在Eclipse中建立Maven工程
- JSP和Servlet那些事儿系列--HTTPS
原文:http://qingkangxu.iteye.com/blog/1614053 <JSP和Servlet那些事儿 >系列文章旨在阐述Servlet(Struts和Spring的MV ...
- vi/vim基本使用方法(转)
转自:http://www.cnblogs.com/itech/archive/2009/04/17/1438439.html vi/vim 基本使用方法 本文介绍了vi (vim)的基本使用方法,但 ...
- Python decorator装饰器
问题: 定义了一个新函数 想在运行时动态增加功能 又不想改动函数本身的代码 通过高阶段函数返回一个新函数 def f1(x): return x*2 def new_fn(f): #装饰器函数 def ...
- 关于Fuzz——peach的学习
最近在搞漏洞挖掘,之前写过一个文件格式漏洞挖掘的博文,使用的是光刃牛写的Alpha Fuzz工具.感觉样本生成的质量不是很好,这次打算使用一下老牌的Fuzz工具peach.学长介绍了一下说peach的 ...
- C语言:奇偶归一猜想
1.奇偶归一猜想——求多少步归一.(10分) 题目内容: 奇偶归一猜想——对于每一个正整数,如果它是奇数,则对它乘3再加1,如果它是偶数,则对它除以2,如此循环,最终都能够得到1. 如n = 11,得 ...