普通版本:

 struct SplayTree
{ const static int maxn = 1e5 + ; int tot,root,ch[maxn][], key[maxn], val[maxn], sz[maxn], lz[maxn], fa[maxn]; void init( int x, int ky, int v = , int par = )
{
ch[x][]=ch[x][]=, fa[x]= par, key[x] = ky, val[x] = v, sz[x] = ;
} void init()
{
init( , , );
sz[] = ;
tot = root = ;
} inline void push_up(int x)
{
sz[x] = sz[ch[x][]] + sz[ch[x][]] + ;
} inline void push_down(int x)
{ } void rotate( int x, int d )
{
int y = fa[x];
ch[y][d ^ ] = 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 ^ ] = x;
}
ch[x][d] = y, fa[y] = x;
push_up( y ), push_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][] )
{
if( fa[y] != target && y == ch[fa[y]][])
rotate( y, );
rotate( x, );
}
else
{
if( fa[y] != target && y == ch[fa[y]][])
rotate( y, );
rotate( x, );
}
}
if( !target ) root = x;
} void Insert( int & t, int ky, int v, int par = )
{
if( t == )
{
t = ++ tot;
init( t, ky, v, par );
Splay( tot, );
}
else
{
int cur = t;
if( v < key[t] ) Insert( ch[t][], ky, v, cur );
else Insert( ch[t][], ky, v, cur );
push_up( cur );
}
} // Return point
int find( int t, int v )
{
if( t == ) return ;
else if( key[t] == v )
{
Splay( t, );
return t;
}
else if( v < key[t] ) return find( ch[t][], v );
return find( ch[t][], v );
} // Delete Root
void Delete()
{
if( !ch[root][] )
{
fa[ ch[root][] ] = ;
root = ch[root][];
}
else
{
int cur = ch[root][];
while( ch[cur][] ) cur = ch[cur][];
Splay( cur, root );
ch[cur][] = ch[root][];
root = cur, fa[cur] = , fa[ch[root][]] = root;
push_up( root );
}
} int size()
{
return sz[root];
}
// 查第 k 小 , 必须保证合法
int kth( int x, int k )
{
if( k == sz[ch[x][]] + )
{
Splay( x, );
return key[x];
}
else if( k <= sz[ch[x][]] ) return kth( ch[x][], k );
else return kth( ch[x][], k - sz[ch[x][]] - );
} //找前驱
int pred( int t, int v )
{
if( t == ) return v;
else
{
if( v <= key[t] ) return pred( ch[t][], v );
else
{
int ans = pred( ch[t][], v );
if( ans == v )
{
ans = key[t];
Splay( t, );
}
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 = sz[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 == ) return v;
else
{
if( v >= key[t] ) return succ( ch[t][], v );
else
{
int ans = succ( ch[t][], v );
if( ans == v )
{
ans = key[t];
Splay( t, );
}
return ans;
}
}
} void Preorder( int t )
{
if( !t ) return;
Preorder( ch[t][] );
printf("%d ", key[t] );
Preorder( ch[t][] );
} } sp;

区间更新版本-

 #include <bits/stdc++.h>

 using namespace std;

 #define lc ch[x][0]
#define rc ch[x][1]
#define pr fa[x] struct SplayTree
{ const static int maxn = 1e5 + ; int tot,root,ch[maxn][], key[maxn], val[maxn], sz[maxn], rev[maxn], fa[maxn]; inline int wh(int x){ return ch[pr][] == x;} inline void init( int x, int ky, int v = , int par = )
{
lc=rc=, fa[x]= par, key[x] = ky, val[x] = v, sz[x] = , rev[x] = ;
} inline void init()
{
init( , , );
sz[] = ;
tot = root = ;
} inline void push_up(int x)
{
sz[x] = sz[lc] + sz[rc] + ;
} inline void reverse(int x)
{
rev[x] ^= , swap( lc, rc);
} inline void push_down(int x)
{
if(rev[x])
{
if(lc) reverse(lc);
if(rc) reverse(rc);
rev[x] = ;
}
} void rotate( int x)
{
int f = fa[x], gf = fa[f], t1 = wh(x);
if( gf ) ch[gf][wh(f)] = x;
fa[x] = gf, ch[f][t1] = ch[x][^t1], fa[ch[f][t1]] = f;
ch[x][t1^] = f, fa[f] = x;
push_up( f ), push_up( x );
} void splay( int x, int tar )
{
for(; pr != tar; rotate(x))
if(fa[pr] != tar)
rotate( wh(x) == wh(pr) ? pr: x);
if( !tar ) root = x;
} void insert( int ky, int v)
{
int x = root, ls = root;
while(x)
{
push_down(x);
sz[x] ++, ls = x;
x = ch[x][ky > key[x]];
}
init( ++tot, ky, v, ls);
ch[ls][ky > key[ls]] = tot;
splay( tot, );
} int find( int ky)
{
int x = root;
while(x)
{
push_down(x);
if(key[x] == ky) break;
x = ch[x][ky > key[x]];
}
if(x) splay(x,);
else x = -;
return x;
} // Delete Root
void Delete()
{
if( !ch[root][] )
{
fa[ ch[root][] ] = ;
root = ch[root][];
}
else
{
int cur = ch[root][];
while( ch[cur][] ) cur = ch[cur][];
splay( cur, root );
ch[cur][] = ch[root][];
root = cur, fa[cur] = , fa[ch[root][]] = root;
push_up( root );
}
} int kth( int k)
{
int x = root;
if(sz[x] < k) return -;
while(x)
{
push_down(x);
if(k == sz[lc] + ) break;
if(k > sz[lc])
k -= sz[lc] + , x = rc;
else
x = lc;
}
if(x) splay(x,);
else x = -;
return x;
} int pred( void)
{
int x = root;
if(!x || !lc) return -;
x = lc;
while(rc) push_down(x), x = rc;
splay( x, );
return x;
} int succ( void)
{
int x = root;
if(!x || !rc) return -;
x = rc;
while(lc) push_down(x), x = lc;
splay( x, );
return x;
} void debug( int x )
{
if( !x ) return;
if(lc) debug( lc );
printf("%d ", key[x] );
if(rc) debug( rc );
} } sp; int main(void)
{
sp.init();
for(int i=,x;i<=;i++)
scanf("%d",&x),sp.insert(x,);
sp.debug(sp.root);
return ;
}

启发式合并:

 /**************************************************************
Problem: 2733
User: weeping
Language: C++
Result: Accepted
Time:4908 ms
Memory:4436 kb
****************************************************************/ #include <bits/stdc++.h> using namespace std; #define lc ch[x][0]
#define rc ch[x][1] int n,m,q,f[]; int fd(int x)
{
return f[x]==x?x:f[x]=fd(f[x]);
} struct SplayTree
{ const static int maxn = 1e5 + ; int tot,root,ch[maxn][], key[maxn], val[maxn], sz[maxn], rev[maxn], fa[maxn]; inline void init( int x, int ky, int v = , int par = )
{
lc=rc=, fa[x]= par, key[x] = ky, val[x] = v, sz[x] = , rev[x] = ;
} inline void init()
{
init( , , );
sz[] = ;
tot = root = ;
} inline void push_up(int x)
{
sz[x] = sz[lc] + sz[rc] + ;
} inline void reverse(int x)
{
rev[x] ^= , swap( lc, rc);
} inline void push_down(int x)
{
if(rev[x])
{
if(lc) reverse(lc);
if(rc) reverse(rc);
rev[x] = ;
}
} void rotate( int x)
{
int f = fa[x], gf = fa[f];
int t1 = (ch[f][] == x), t2 = (ch[gf][] == f);
if( gf ) ch[gf][t2] = x;
fa[x] = gf, ch[f][t1] = ch[x][^t1], fa[ch[f][t1]] = f;
ch[x][t1^] = f, fa[f] = x;
push_up( f ), push_up( x );
} void splay( int x, int tar )
{
for(int f = fa[x], gf = fa[f]; f != tar; rotate(x), f = fa[x], gf = fa[f])
if(gf != tar)
rotate( ((ch[f][] == x) == (ch[gf][] == f) )? f: x);
if( !tar ) root = x;
} void insert( int ky, int v)
{
int x = root, ls = root;
while(x)
{
push_down(x);
sz[x] ++, ls = x;
x = ch[x][ky > key[x]];
}
init( ++tot, ky, v, ls);
ch[ls][ky > key[ls]] = tot;
splay( tot, );
} int find( int ky)
{
int x = root;
while(x)
{
push_down(x);
if(key[x] == ky) break;
x = ch[x][ky > key[x]];
}
if(x) splay(x,);
else x = -;
return x;
} // Delete Root
void Delete()
{
if( !ch[root][] )
{
fa[ ch[root][] ] = ;
root = ch[root][];
}
else
{
int cur = ch[root][];
while( ch[cur][] ) cur = ch[cur][];
splay( cur, root );
ch[cur][] = ch[root][];
root = cur, fa[cur] = , fa[ch[root][]] = root;
push_up( root );
}
} int kth( int k)
{
int x = root;
if(sz[x] < k) return -;
while(x)
{
push_down(x);
if(k == sz[lc] + ) break;
if(k > sz[lc])
k -= sz[lc] + , x = rc;
else
x = lc;
}
if(x) splay(x,);
else x = -;
return x;
} int pred( void)
{
int x = root;
if(!x || !lc) return -;
x = lc;
while(rc) push_down(x), x = rc;
splay( x, );
return x;
} int succ( void)
{
int x = root;
if(!x || !rc) return -;
x = rc;
while(lc) push_down(x), x = lc;
splay( x, );
return x;
} void debug( int x )
{
if( !x ) return;
if(lc) debug( lc );
printf("%d ", key[x] );
if(rc) debug( rc );
} void qinsert(int y)
{
int x = root, ls = root, ky = key[y];
while(x)
ls = x, x = ch[x][ky > key[x]];
x = ls;
ch[x][ky > key[x]] = y,fa[y] = x, sz[y] = ;
splay(y, );
} void qmerge(int x)
{
if(!x) return;
int tl = lc, tr = rc;
lc = rc = ;
qmerge(tl);
qinsert(x);
qmerge(tr);
}
void merge(int u,int v)
{
if(u == v) return ;
if(sz[u]>sz[v]) swap(u,v);
f[u] = v, splay( v, );
qmerge(u);
}
} sp; int main(void)
{
scanf("%d%d",&n,&m);
for(int i=,x;i<=n;i++)
scanf("%d",&x),f[i]=i,sp.key[i]=x,sp.sz[i]=;
for(int i=,u,v;i<=m;i++)
scanf("%d%d",&u,&v),sp.merge(fd(u),fd(v));
scanf("%d",&q);
char op[];
for(int i=,x,y;i<=q;i++)
{
scanf("%s%d%d",op,&x,&y);
if(op[]=='B')
sp.merge(fd(x),fd(y));
else
sp.splay(x,),printf("%d\n",sp.kth(y));
}
return ;
}

splay伸展树模板的更多相关文章

  1. 【学时总结】◆学时·VI◆ SPLAY伸展树

    ◆学时·VI◆ SPLAY伸展树 平衡树之多,学之不尽也…… ◇算法概述 二叉排序树的一种,自动平衡,由 Tarjan 提出并实现.得名于特有的 Splay 操作. Splay操作:将节点u通过单旋. ...

  2. Splay伸展树学习笔记

    Splay伸展树 有篇Splay入门必看文章 —— CSDN链接 经典引文 空间效率:O(n) 时间效率:O(log n)插入.查找.删除 创造者:Daniel Sleator 和 Robert Ta ...

  3. Splay伸展树入门(单点操作,区间维护)附例题模板

    Pps:终于学会了伸展树的区间操作,做一个完整的总结,总结一下自己的伸展树的单点操作和区间维护,顺便给未来的自己总结复习用. splay是一种平衡树,[平均]操作复杂度O(nlogn).首先平衡树先是 ...

  4. Splay 伸展树

    废话不说,有篇论文可供参考:杨思雨:<伸展树的基本操作与应用> Splay的好处可以快速分裂和合并. ===============================14.07.26更新== ...

  5. [Splay伸展树]splay树入门级教程

    首先声明,本教程的对象是完全没有接触过splay的OIer,大牛请右上角.. 首先引入一下splay的概念,他的中文名是伸展树,意思差不多就是可以随意翻转的二叉树 PS:百度百科中伸展树读作:BoGa ...

  6. Codeforces 675D Tree Construction Splay伸展树

    链接:https://codeforces.com/problemset/problem/675/D 题意: 给一个二叉搜索树,一开始为空,不断插入数字,每次插入之后,询问他的父亲节点的权值 题解: ...

  7. HYSBZ 1500 维修数列(伸展树模板)

    题意: 题解:典型伸展树的题,比较全面. 我理解的伸展树: 1 伸展操作:就是旋转,因为我们只需保证二叉树中序遍历的结果不变,所以我们可以旋转来保持树的平衡,且旋转有左旋与右旋.通过这种方式保证不会让 ...

  8. UVA 11922 Permutation Transformer —— splay伸展树

    题意:根据m条指令改变排列1 2 3 4 … n ,每条指令(a, b)表示取出第a~b个元素,反转后添加到排列尾部 分析:用一个可分裂合并的序列来表示整个序列,截取一段可以用两次分裂一次合并实现,粘 ...

  9. [算法] 数据结构 splay(伸展树)解析

    前言 splay学了已经很久了,只不过一直没有总结,鸽了好久来写一篇总结. 先介绍 splay:亦称伸展树,为二叉搜索树的一种,部分操作能在 \(O( \log n)\) 内完成,如插入.查找.删除. ...

随机推荐

  1. 【OpenWRT】网络配置

    cd /etc/config vim network vim wireless cd /etc/init.d/network

  2. MathType中有几种不同的省略号

    省略号是一个使用很广泛的符号,这个符号在很多方面都有应用,它一般表示列举的意思.文科方面的省略号跟数理中的省略号使用时有一些区别,前者是6个点,而后者只要3个点.当在用MathType数学公式编辑器时 ...

  3. VC++ 窗口拆分CSplitterWnd

    前言         当前许多优秀的软件都采用“多视”技术. 所谓“多视”,是指在同一个框架窗口里同时显示多个视图. 通过运用这种技术,可以在框架的有限控件内同时提供用户更大的信息量,并且使得用户界面 ...

  4. 剑指 offer set 24 扑克牌的顺子

    题目 从扑克牌中任意抽取出 5 张牌, 判断是不是顺子, 并且大小王可以看成任意的数字 思路 1. 把大小王当做 0 插入到数组中, 然后对数组排序 2. 统计相邻两个数之间的空隙数, 若空隙数大于 ...

  5. WPF进阶之接口(2):IDisposable,ICollectionView

    废话不多说,进入正题,先来说说IDisposable,看例子(来自MSDN): using System; using System.ComponentModel; // 下面的例子将展示一个实施了I ...

  6. linux 常用命令总结(tsg)

    1.解压tar.gz 第一步:gunzip filename.tar.gz --->会解压成.tar文件 第二步:tar xvf FileName.tar ---->会解压到当前文件夹2. ...

  7. Linux 文件夹含义(转)

    1./bin :获得最小的系统可操作性所需要的命令 2./boot :内核和加载内核所需的文件 3./dev :终端.磁盘.调制解调器等的设备项 4./etc :关键的启动文件和配置文件 5./hom ...

  8. HTML5游戏制作完全指南

    简介 创建画布 游戏循环 Hello world 创建player 键盘控制 a:使用jQuery Hotkeys b:移动player 添加更多游戏元素 炮弹 敌人 使用图片 碰撞检测 声音 简介 ...

  9. IOS实现打电话后回调

    本文转载至 http://blog.csdn.net/cerastes/article/details/38340687   UIWebView *callWebview =[[UIWebView a ...

  10. SpringMvc三大组件详解

    SpringMvc框架结构图 处理器映射器:用户请求路径到Controller方法的映射 处理器适配器:根据handler(controlelr类)的开发方式(注解开发/其他开发) 方式的不同区寻找不 ...