首页 > 其他 > 详细

2002: [Hnoi2010]Bounce 弹飞绵羊(link-cut-tree)

时间:2014-03-23 08:42:19      阅读:446      评论:0      收藏:0      [点我收藏+]

2002: [Hnoi2010]Bounce 弹飞绵羊

题意:中文题就不解释了。
解题思路:对于i,到i的绵羊会被弹到i+ki位置上,那么我们连一条(i,i+ki)的边,所有的关系建完之后,就是一个森林,而i位置被弹几次,自然就是其深度了。这里再做一个改进,设一个虚拟节点,将森林里,树的根都连在n+1这个节点上,那么所有的关系就是一颗树了。这样做是方便操作。在修改这个操作的时候,我是先将a和a的父亲断开,也就是cut操作,然后将a和a的新父亲连起来,而在这个过程中,会有换根的操作,所以,有了虚拟的根节点,那么我们在query的时候,再将根换回n+1,那么询问就方便多了。
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define ls son[0][rt]
#define rs son[1][rt]
using namespace std ;
 
const int maxn = 211111 ;
int son[2][maxn] , fa[maxn] , is[maxn] , size[maxn] , rev[maxn] ;
 
void push_up ( int rt ) {
        size[rt] = size[ls] + size[rs] + 1 ;
}
 
void reverse ( int rt ) {
        if ( !rt ) return ;
        swap ( ls , rs ) ;
        rev[rt] ^= 1 ;
}
 
void push_down ( int rt ) {
        if ( rev[rt] ) {
                reverse ( ls ) ;
                reverse ( rs ) ;
                rev[rt] = 0 ;
        }
}
 
void down ( int rt ) {
        if ( !is[rt] ) down ( fa[rt] ) ;
        push_down ( rt ) ;
}
 
void rot ( int rt , int c ) {
        int y = fa[rt] , z = fa[y] ;
        son[!c][y] = son[c][rt] ; fa[son[c][rt]] = y ;
        son[c][rt] = y ; fa[y] = rt ;
        fa[rt] = z ;
        if ( is[y] ) is[y] = 0 , is[rt] = 1 ;
        else son[y==son[1][z]][z] = rt ;
        push_up ( y ) ;
}
 
void splay ( int rt ) {
        down ( rt ) ;
        while ( !is[rt] ) {
                int y = fa[rt] , z = fa[y] ;
                if ( is[y] ) rot ( rt , rt == son[0][y] ) ;
                else {
                        int c = ( rt == son[0][y] ) , d = ( y == son[0][z] ) ;
                        if ( c == d ) rot ( y , c ) , rot ( rt , c ) ;
                        else rot ( rt , c ) , rot ( rt , d ) ;
                }
        }
        push_up ( rt ) ;
}
 
void access ( int rt ) {
        for ( int v = 0 ; rt ; rt = fa[rt] ) {
                splay ( rt ) ;
                is[rs] = 1 ; is[v] = 0 ;
                rs = v ;
                v = rt ;
                push_up ( rt ) ;
        }
}
 
void change_root ( int rt ) {
        access ( rt ) ;
        splay ( rt ) ;
        reverse ( rt ) ;
}
 
int query ( int a , int b ) {
        while ( fa[a] ) a = fa[a] ;
        while ( fa[b] ) b = fa[b] ;
        return a == b ;
}
 
void cut ( int a , int b ) {
        if ( query ( a , b ) ) {
                change_root ( a ) ;
                access ( a ) ;
                splay ( b ) ;
                fa[b] = 0 ;
        }
}
 
void add ( int a , int b ) {
        if ( !query ( a , b ) ) {
                splay ( b ) ;
                fa[b] = a ;
        }
}
 
void init ( int rt ) {
        size[rt] = is[rt] = 1 ;
        son[0][rt] = son[1][rt] = fa[rt] = rev[rt] = 0 ;
}
 
int num[maxn] ;
int main () {
        int n , m , i , a , b , c ;
        while ( scanf ( "%d" , &n ) != EOF ) {
                for ( i = 1 ; i <= n + 1 ; i ++ ) init ( i ) ;
                for ( i = 1 ; i <= n ; i ++ ) {
                        scanf ( "%d" , &a ) ;
                        num[i] = a ;
                        if ( i + a <= n ) add ( i + a , i ) ;
                        else add ( n + 1 , i ) ;
                }
                scanf ( "%d" , &m ) ;
                while ( m -- ) {
                        scanf ( "%d%d" , &c , &a ) ;
                        a ++ ;
                        if ( c == 1 ) {
                                change_root ( n + 1 ) ;
                                access ( a ) ;
                                splay ( a ) ;
                                printf ( "%d\n" , size[son[0][a]] ) ;
                        }
                        else {
                                scanf ( "%d" , &b ) ;
                                int d = a + num[a] ; num[a] = b ;
                                cut ( a , min ( d , n + 1 ) ) ;
                                add ( min ( num[a] + a , n + 1 ) , a ) ;
                        }
                }
        }
        return 0 ;
}


2002: [Hnoi2010]Bounce 弹飞绵羊(link-cut-tree),布布扣,bubuko.com

2002: [Hnoi2010]Bounce 弹飞绵羊(link-cut-tree)

原文:http://blog.csdn.net/no__stop/article/details/21833499

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!