POJ 3468 A Simple Problem with Integers Splay
来源:互联网 发布:巴黎 知乎 编辑:程序博客网 时间:2024/05/21 08:47
题目链接:http://poj.org/problem?id=3468
思路:这个题目很明显可以用线段树来做,但是这篇文章主要是讲一下Splay的做法。
Splay :
Splay其实就是一种平衡树,它具有BST的特性,但是因为BST最坏情况下建树复杂度会达到O(n),Splay就是一种建立在BST之上,加入了一些旋转,使得退化成链的BST树的均摊复杂度为O(logn)。Splay最重要的就是伸展操作,这种操作可以将树中的任何一个结点通过旋转,旋转到树根,这个结论显然的。旋转可以分成三种:
Case1、当前结点的祖先就是根结点,那么我们只需要进行一次简单的单旋转就可以将当前结点选择到根下面;
Case2、当前结点相对于其父节点的方向(是左孩子、还是右孩子) 和 当前结点父节点相对于当前结点的祖先结点的方向相同,这时候只需要对当前结点进行两次方向相同的单旋转就可以;
Case3、当前结点相对于其父节点的方向(是左孩子、还是右孩子) 和 当前结点父节点相对于当前结点的祖先结点的方向相反,这时候需要对当前结点进行两次方向相反的单旋转就可以。
下面是Splay的关键代码:
void Rotate(int x, int f){ // 对x结点进行一次f旋转; f==0:左旋 ; f==1:右旋 int y = pre[x] ; push_down( y ) ; push_down( x ) ; ch[y][!f] = ch[x][f] ; pre[ ch[x][f] ] = y ; pre[x] = pre[y] ; if( pre[x] ){ ch[ pre[y] ][ ch[pre[y]][1]==y ] = x ; } ch[x][f] = y ; pre[y] = x ; push_up( y ) ;}void Splay(int x, int goal){ /* 将x结点旋转到goal下面;这里是不用出来旋转到左孩子还是右孩子的, 这是因为一旦树建成之后,x在goal的哪个子树中就已经确定,因为 旋转操作不会改变x在goal哪个子树中的位置,因此最终x还是会在原来 的哪个子树中。 */ push_down( x ) ; //在旋转x之前需要将x的延迟标记down掉,因为旋转之后x的孩子有可能会改变; while( pre[x] != goal ){ if( pre[pre[x]] == goal ){ // Case 1 Rotate( x , x == ch[ pre[x] ][0] ) ; } else { int y = pre[x] , z = pre[y] ; int f = ( ch[z][0] == y ) ; if( ch[y][f] == x ){ // Case 3 Rotate( x , !f ) ; Rotate( x , f ) ; } else{ // Case 2 Rotate( x , f ) ; Rotate( x , f ) ; } } } push_up( x ) ; /* 这个地方一开始看可能会有个疑惑:为什么不在Rotate中push_up( x ),而是只在这里进行一次 push_up。首先在Rotate中按理说在push_up( y )之后是应该进行一次push_up(x),因为这时候x 的sum信息和size信息已经不正确了。但是我们在Splay中进行旋转的都是x结点,x结点信息的不 正确并不会在后面的旋转造成影响,我们只需要保证x的左右子树的信息是正确的就可以(这里自 己画个图好好想想就可以了),过早地更新x结点的信息反而是多余的,因此这里我们只是在最后 进行一次更新。 */ if( goal == 0 ) T = x ;}
下图所示是Splay树的初始化树形,其中keyTree就是插入的信息树。R为Root,就是在所有元素之前插入的结点,ch[R][1]是所有元素之后的结点。
下面讲解一下如何获取区间[L , R],我们只需要将L-1结点旋转到0结点的下面,也就是上图R的位置,将R+1旋转到ch[R][1]的位置,这时候keyTree子树中的所有结点就是[L , R]区间内的所有结点了。
完整代码:
#include <stdio.h>#include <string.h>#define keyTree ( ch[ ch[T][1] ][0] ) #define Debug debug(T); printf("\n");typedef long long LL ;const int NN = 100010 ;int N , M ;int a[NN] , ch[NN][2] , size[NN] , pre[NN] ;LL sum[NN] , val[NN] , add[NN];int node , T ;void NewNode(int &T , int c ){ //创建新结点,并初始化 T = ++node ; ch[T][0] = ch[T][1] = pre[T] = 0 ; size[T] = 1 ; val[T] = sum[T] = c ; add[T] = 0 ;}void push_up(int x){ //类似于线段树 size[x] = size[ ch[x][0] ] + size[ ch[x][1] ] + 1 ; sum[x] = sum[ ch[x][0] ] + sum[ ch[x][1] ] + val[x] ;}void push_down(int x){ //类似于线段树中的延迟标记down if( add[x] ){ add[ ch[x][0] ] += add[x] ; add[ ch[x][1] ] += add[x] ; val[ ch[x][0] ] += add[x] ; val[ ch[x][1] ] += add[x] ; sum[ ch[x][0] ] += (LL)add[x] * size[ ch[x][0] ] ; sum[ ch[x][1] ] += (LL)add[x] * size[ ch[x][1] ] ; add[x] = 0 ; }}void build(int &T, int l , int r, int f ){ if( l > r ) return ; int mid = (l + r) >> 1 ; NewNode(T , a[mid]) ; build( ch[T][0] , l , mid-1 , T ) ; build( ch[T][1] , mid+1 , r , T ) ; pre[T] = f ; push_up( T );}void init(){ ch[0][0] = ch[0][1] = pre[0] = size[0] = add[0] = sum[0] = 0 ; T = node = 0 ; NewNode( T , -1 ) ; //增加第一个无关阶段,增加在所有元素之前 NewNode( ch[T][1] , -1 ) ; //增加第二个无关结点,增加在所有元素之后 pre[ ch[T][1] ] = T ; size[T] = 2 ; for(int i=0;i<N;i++) scanf("%d",&a[i]); build( keyTree , 0 , N-1 , ch[T][1] ) ; //建树 push_up( ch[T][1] ) ; push_up( T ) ;}void Rotate(int x, int f){ int y = pre[x] ; push_down( y ) ; push_down( x ) ; ch[y][!f] = ch[x][f] ; pre[ ch[x][f] ] = y ; pre[x] = pre[y] ; if( pre[x] ){ ch[ pre[y] ][ ch[pre[y]][1]==y ] = x ; } ch[x][f] = y ; pre[y] = x ; push_up( y ) ;}void Splay(int x, int goal){ /* 将x结点旋转到goal下面;这里是不用出来旋转到左孩子还是右孩子的, 这是因为一旦树建成之后,x在goal的哪个子树中就已经确定,因为 旋转操作不会改变x在goal哪个子树中的位置,因此最终x还是会在原来 的哪个子树中。 */ push_down( x ) ; //在旋转x之前需要将x的延迟标记down掉,因为旋转之后x的孩子有可能会改变; while( pre[x] != goal ){ if( pre[pre[x]] == goal ){ // Case 1 Rotate( x , x == ch[ pre[x] ][0] ) ; } else { int y = pre[x] , z = pre[y] ; int f = ( ch[z][0] == y ) ; if( ch[y][f] == x ){ // Case 3 Rotate( x , !f ) ; Rotate( x , f ) ; } else{ // Case 2 Rotate( x , f ) ; Rotate( x , f ) ; } } } push_up( x ) ; /* 这个地方一开始看可能会有个疑惑:为什么不在Rotate中push_up( x ),而是只在这里进行一次 push_up。首先在Rotate中按理说在push_up( y )之后是应该进行一次push_up(x),因为这时候x 的sum信息和size信息已经不正确了。但是我们在Splay中进行旋转的都是x结点,x结点信息的不 正确并不会在后面的旋转造成影响,我们只需要保证x的左右子树的信息是正确的就可以(这里自 己画个图好好想想就可以了),过早地更新x结点的信息反而是多余的,因此这里我们只是在最后 进行一次更新。 */ if( goal == 0 ) T = x ;}void RotateTo(int k ,int goal){ //将中序遍历的第k个元素旋转到goal的下面 int x = T ; push_down( x ) ; while( size[ch[x][0]] != k ){ // 这个地方其实应该是找到第k+1个元素,因此在所有元素之前加上了一个无关元素 if( k < size[ ch[x][0] ] ){ x = ch[x][0] ; } else k -= ( 1 + size[ ch[x][0] ] ) , x = ch[x][1] ; push_down( x ) ; } Splay( x , goal ) ;}void update(){ int l , r , aa ; scanf("%d%d%d",&l,&r,&aa); RotateTo( l-1 , 0 ) ; RotateTo( r+1 , T ) ; add[ keyTree ] += aa ; sum[ keyTree ] += (LL)aa*size[ keyTree ] ; val[ keyTree ] += aa ;}LL query(){ int l ,r ; scanf("%d%d",&l,&r); RotateTo(l-1 , 0) ; RotateTo(r+1, T ) ; return sum[keyTree] ;}int main(){ char op[3] ; while( scanf("%d %d",&N,&M)==2 ){ init() ; for(int i=0;i<M;i++){ scanf("%s",op); if( op[0] == 'Q' ){ printf("%lld\n",query() ); } else{ update() ; } } } return 0 ;}
- POJ 3468 A Simple Problem with Integers Splay
- poj 3468 A Simple Problem with Integers (Splay树)
- POJ 3468 A Simple Problem with Integers (Splay树练习)
- poj-3468-A Simple Problem with Integers-splay树
- POJ 3468 A Simple Problem with Integers ( Splay )
- 【Splay】POJ 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers splay
- poj 3468 A Simple Problem with Integers splay tree
- [POJ 3468] A Simple Problem with Integers Splay树
- POJ 3468 A Simple Problem with Integers (splay)
- poj 3468 A Simple Problem with Integers - Splay
- POJ-3468 A Simple Problem with Integers(Splay实现)
- poj 3468 A Simple Problem with Integers splay
- A Simple Problem with Integers+poj+splay树区间操作
- [Splay] POJ3468 A Simple Problem with Integers
- POJ 3468 A Simple Problem With Integers
- poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- 在VS2005中安装BOOST库
- eclipse编译器错误、警告设置
- mac 下实现双击打开leo文件
- javascript 中的location.pathname
- Tomcat服务器
- POJ 3468 A Simple Problem with Integers Splay
- 3.10/11--java的日子
- Eclipse开发环境搭建
- 20130310
- 润乾报表设计器使用心得
- java面向对象
- 实用的Log打印类封装,助尼快速定位源码(android)
- 简明 Vim 练级攻略(Vim新手入门,进阶必读)by-陈皓
- 看透自己的老板一---性格篇