数据结构之伸展树
http://dongxicheng.org/structure/splay-tree/
伸展树
http://www.cnblogs.com/kernel_hcy/archive/2010/03/17/1688360.html
BZOJ 1500: [NOI2005]维修数列 (splay tree)
http://www.cnblogs.com/kuangbin/p/3287822.html
1.伸展树简介
伸展树,或者叫自适应查找树,是一种用于保存有序集合的简单高效的数据结构。伸展树实质上是一个二叉查找树。允许查找,插入,删除,删除最小,删除最大,分割,合并等许多操作,这些操作的时间复杂度为O(logN)。由于伸展树可以适应需求序列,因此他们的性能在实际应用中更优秀。
伸展树支持所有的二叉树操作。伸展树不保证最坏情况下的时间复杂度为O(logN)。伸展树的时间复杂度边界是均摊的。尽管一个单独的操作可能很耗时,但对于一个任意的操作序列,时间复杂度可以保证为O(logN)。
2.基本操作
伸展树的出发点是这样的:考虑到局部性原理(刚被访问的内容下次可能仍会被访问,查找次数多的内容可能下一次会被访问),为了使整个查找时间更小,被查频率高的那些节点应当经常处于靠近树根的位置。这样,很容易得想到以下这个方案:每次查找节点之后对树进行重构,把被查找的节点搬移到树根,这种自调整形式的二叉查找树就是伸展树。每次对伸展树进行操作后,它均会通过旋转的方法把被访问节点旋转到树根的位置。
为了将当前被访问节点旋转到树根,我们通常将节点自底向上旋转,直至该节点成为树根为止。“旋转”的巧妙之处就是在不打乱数列中数据大小关系(指中序遍历结果是全序的)情况下,所有基本操作的平摊复杂度仍为O(log n)。
伸展树操作分为三种,分别是zig(右旋转),zag(左旋转),和zig-zig,zag-zag,zig-zag,zag-zig。其中后四种可以根据前两种得到。
这是两种基本的旋转操作的示意图
实际上zig-zig的操作是分了两步,如上图中,实际上我们需要先将y和z进行一个右旋转,然后再将y和x进行一个右旋转。而zig-zag的操作也是分了两步,不过和zig-zig操作不一样的是,他是先将x和y进行一个左旋转,再将x和z进行一个右旋转。
所以其伪代码如下
P(X) : 获得X的父节点,G(X) : 获得X的祖父节点(=P(P(X)))。
Function Buttom-up-splay:
Do
If X 是 P(X) 的左子结点 Then
If G(X) 为空 Then
X 绕 P(X)右旋
Else If P(X)是G(X)的左子结点
P(X) 绕G(X)右旋
X 绕P(X)右旋
Else
X绕P(X)右旋
X绕P(X)左旋 (P(X)和上面一句的不同,是原来的G(X))
Endif
Else If X 是 P(X) 的右子结点 Then
If G(X) 为空 Then
X 绕 P(X)左旋
Else If P(X)是G(X)的右子结点
P(X) 绕G(X)左旋
X 绕P(X)左旋
Else
X绕P(X)左旋
X绕P(X)右旋 (P(X)和上面一句的不同,是原来的G(X))
Endif
Endif
While (P(X) != NULL)
EndFunction
但是我们发现这种操作还有另一种理解的方式,这里我们要引入三种操作的概念。分别是左中右树合并和左连接,右连接。
所以他的伪代码可以如下
Function Top-Down-Splay
Do
If X 小于 T Then
If X 等于 T 的左子结点 Then
右连接
ElseIf X 小于 T 的左子结点 Then
T的左子节点绕T右旋
右连接
Else X大于 T 的左子结点 Then
右连接
左连接
EndIf
ElseIf X大于 T Then
IF X 等于 T 的右子结点 Then
左连接
ElseIf X 大于 T 的右子结点 Then
T的右子节点绕T左旋
左连接
Else X小于 T 的右子结点‘ Then
左连接
右连接
EndIf
EndIf
While !(找到 X或遇到空节点)
组合左中右树
EndFunction
但是我们发现其实这些代码可以简化成下面的形式
Function Buttom-up-splay:
Do
If X 是 P(X) 的左子结点 Then
If P(X)是G(X)的左子结点
P(X) 绕G(X)右旋
Endif
X 绕P(X)右旋
Else If X 是 P(X) 的右子结点 Then
If P(X)是G(X)的右子结点
P(X) 绕G(X)左旋
Endif
X 绕P(X)左旋
Endif
While (P(X) != NULL)
EndFunction
下面给出两个运用上面简化后的代码进行splay的实例
3.在伸展树中对区间进行操作
在伸展树中 对区间进行操作 首先我们认为伸展树的中序遍历即为我们维护的数列,那么很重要的一个操作就是怎么在伸展 树中表示任意一个区间。比如我们要提取区间a,b,那么我们将 a 前面一个数对应的结点转到树 根,将 b 后面一个结点对应的结点转到树根的右边,那么根右边的左子树就对应了区间a,b其中的道理也是很简单的,将 a 前面一个数对应的结点转到树根后, a 及 a 后面的数就在根的右 子树上,然后又将b后面一个结点对应的结点转到树根的右边,那么a,b这个
区间就是图三中 * 所示的子树。
4.伸展树可以实现的功能
以维修数列题目为例:维护一个数列,支持以下几种操作:
- 插入:在当前数列第posi 个数字后面插入tot 个数字;若在数列首位插入,则posi 为0。
思路:先将这tot个数字构成一个排序树,将第posi个数字转到树根,将第posi+1个数字转到树根的右子树的根上。然后将这tot个数的树根挂到树根的右子树的左子树的位置上即可。 - 删除:从当前数列第posi 个数字开始连续删除tot 个数字。
思路: 将posi-1位置的数字移动到树根,将posi+tot位置的数字移动到树根的右子树的树根处,然后删除该右子树的树根处的左子树即可 - 修改:从当前数列第posi 个数字开始连续tot 个数字统一修改为c 。
思路:还是同样的区间操作 - 翻转:取出从当前数列第posi 个数字开始的tot 个数字,翻转后放入原来的位置。
- 求和:计算从当前数列第posi 个数字开始连续tot 个数字的和并输出。
- 求和最大子序列:求出当前数列中和最大的一段子序列,并输出最大和。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define Key_value ch[ch[root][1]][0]
const int MAXN = 500010;
const int INF = 0x3f3f3f3f;
int pre[MAXN],ch[MAXN][2],key[MAXN],size[MAXN];
int root,tot1;
int sum[MAXN],rev[MAXN],same[MAXN];
int lx[MAXN],rx[MAXN],mx[MAXN];
int s[MAXN],tot2;//内存池和容量
int a[MAXN];
int n,q;
//debug部分**********************************
void Treavel(int x)
{
if(x)
{
Treavel(ch[x][0]);
printf("结点:%2d: 左儿子 %2d 右儿子 %2d 父结点 %2d size = %2d\n",x,ch[x][0],ch[x][1],pre[x],size[x]);
Treavel(ch[x][1]);
}
}
void debug()
{
printf("root:%d\n",root);
Treavel(root);
}
//以上是debug部分**************************************
void NewNode(int &r,int father,int k)
{
if(tot2) r = s[tot2--];//取的时候是tot2--,存的时候就是++tot2
else r = ++tot1;
pre[r] = father;
ch[r][0] = ch[r][1] = 0;
key[r] = k;
sum[r] = k;
rev[r] = same[r] = 0;
lx[r] = rx[r] = mx[r] = k;
size[r] = 1;
}
void Update_Rev(int r)
{
if(!r)return;
swap(ch[r][0],ch[r][1]);
swap(lx[r],rx[r]);
rev[r] ^= 1;
}
void Update_Same(int r,int v)
{
if(!r)return;
key[r] = v;
sum[r] = v*size[r];
lx[r] = rx[r] = mx[r] = max(v,v*size[r]);
same[r] = 1;
}
void push_up(int r)
{
int lson = ch[r][0], rson = ch[r][1];
size[r] = size[lson] + size[rson] + 1;
sum[r] = sum[lson] + sum[rson] + key[r];
lx[r] = max(lx[lson],sum[lson] + key[r] + max(0,lx[rson]));
rx[r] = max(rx[rson],sum[rson] + key[r] + max(0,rx[lson]));
mx[r] = max(0,rx[lson]) + key[r] + max(0,lx[rson]);
mx[r] = max(mx[r],max(mx[lson],mx[rson]));
}
void push_down(int r)
{
if(same[r])
{
Update_Same(ch[r][0],key[r]);
Update_Same(ch[r][1],key[r]);
same[r] = 0;
}
if(rev[r])
{
Update_Rev(ch[r][0]);
Update_Rev(ch[r][1]);
rev[r] = 0;
}
}
void Build(int &x,int l,int r,int father)
{
if(l > r)return;
int mid = (l+r)/2;
NewNode(x,father,a[mid]);
Build(ch[x][0],l,mid-1,x);
Build(ch[x][1],mid+1,r,x);
push_up(x);
}
void Init()
{
root = tot1 = tot2 = 0;
ch[root][0] = ch[root][1] = size[root] = pre[root] = 0;
same[root] = rev[root] = sum[root] = key[root] = 0;
lx[root] = rx[root] = mx[root] = -INF;
NewNode(root,0,-1);
NewNode(ch[root][1],root,-1);
for(int i = 0;i < n;i++)
scanf("%d",&a[i]);
Build(Key_value,0,n-1,ch[root][1]);
push_up(ch[root][1]);
push_up(root);
}
//旋转,0为左旋,1为右旋
void Rotate(int x,int kind)
{
int y = pre[x];
push_down(y);
push_down(x);
ch[y][!kind] = ch[x][kind];
pre[ch[x][kind]] = y;
if(pre[y])
ch[pre[y]][ch[pre[y]][1]==y] = x;
pre[x] = pre[y];
ch[x][kind] = y;
pre[y] = x;
push_up(y);
}
//Splay调整,将r结点调整到goal下面
void Splay(int r,int goal)
{
push_down(r);
while(pre[r] != goal)
{
if(pre[pre[r]] == goal)
{
push_down(pre[r]);
push_down(r);
Rotate(r,ch[pre[r]][0] == r);
}
else
{
push_down(pre[pre[r]]);
push_down(pre[r]);
push_down(r);
int y = pre[r];
int kind = ch[pre[y]][0]==y;
if(ch[y][kind] == r)
{
Rotate(r,!kind);
Rotate(r,kind);
}
else
{
Rotate(y,kind);
Rotate(r,kind);
}
}
}
push_up(r);
if(goal == 0) root = r;
}
int Get_kth(int r,int k)
{
push_down(r);
int t = size[ch[r][0]] + 1;
if(t == k)return r;
if(t > k)return Get_kth(ch[r][0],k);
else return Get_kth(ch[r][1],k-t);
}
//在第pos个数后面插入tot个数
void Insert(int pos,int tot)
{
for(int i = 0;i < tot;i++)scanf("%d",&a[i]);
Splay(Get_kth(root,pos+1),0);
Splay(Get_kth(root,pos+2),root);
Build(Key_value,0,tot-1,ch[root][1]);
push_up(ch[root][1]);
push_up(root);
}
//删除子树
void erase(int r)
{
if(!r)return;
s[++tot2] = r;
erase(ch[r][0]);
erase(ch[r][1]);
}
//从第pos个数开始连续删除tot个数
void Delete(int pos,int tot)
{
Splay(Get_kth(root,pos),0);
Splay(Get_kth(root,pos+tot+1),root);
erase(Key_value);
pre[Key_value] = 0;
Key_value = 0;
push_up(ch[root][1]);
push_up(root);
}
//将从第pos个数开始的连续的tot个数修改为c
void Make_Same(int pos,int tot,int c)
{
Splay(Get_kth(root,pos),0);
Splay(Get_kth(root,pos+tot+1),root);
Update_Same(Key_value,c);
push_up(ch[root][1]);
push_up(root);
}
//将第pos个数开始的连续tot个数进行反转
void Reverse(int pos,int tot)
{
Splay(Get_kth(root,pos),0);
Splay(Get_kth(root,pos+tot+1),root);
Update_Rev(Key_value);
push_up(ch[root][1]);
push_up(root);
}
//得到第pos个数开始的tot个数的和
int Get_Sum(int pos,int tot)
{
Splay(Get_kth(root,pos),0);
Splay(Get_kth(root,pos+tot+1),root);
return sum[Key_value];
}
//得到第pos个数开始的tot个数中最大的子段和
int Get_MaxSum(int pos,int tot)
{
Splay(Get_kth(root,pos),0);
Splay(Get_kth(root,pos+tot+1),root);
return mx[Key_value];
}
void InOrder(int r)
{
if(!r)return;
push_down(r);
InOrder(ch[r][0]);
printf("%d ",key[r]);
InOrder(ch[r][1]);
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d%d",&n,&q) == 2)
{
Init();
char op[20];
int x,y,z;
while(q--)
{
scanf("%s",op);
if(strcmp(op,"INSERT") == 0)
{
scanf("%d%d",&x,&y);
Insert(x,y);
}
else if(strcmp(op,"DELETE") == 0)
{
scanf("%d%d",&x,&y);
Delete(x,y);
}
else if(strcmp(op,"MAKE-SAME") == 0)
{
scanf("%d%d%d",&x,&y,&z);
Make_Same(x,y,z);
}
else if(strcmp(op,"REVERSE") == 0)
{
scanf("%d%d",&x,&y);
Reverse(x,y);
}
else if(strcmp(op,"GET-SUM") == 0)
{
scanf("%d%d",&x,&y);
printf("%d\n",Get_Sum(x,y));
}
else if(strcmp(op,"MAX-SUM") == 0)
printf("%d\n",Get_MaxSum(1,size[root]-2));
}
}
return 0;
}
然后伸展树还可以实现区间第k大/小的效果,具体就是找到区间,然后遍历的第k个元素即是所求