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

题意:中文题就不解释了。

解题思路:对于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 y = fa[rt] , z = fa[y] , c = rt == son[0][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==son[0][y]) == (y==son[0][z]) ? y : rt ) ;
				rot ( rt ) ;
        }
        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 ) ) {
                change_root ( 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 ;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值