# undef isrs
# undef isroot
inline void access ( node* p ) {
for ( node* pre = null ; p != null ; pre = p, p = p -》 fa )
splay ( p ), p -》 ch [1] = pre, p -》 update ( ) ;
}
inline void makeroot ( node* p ) {
access ( p ) ; splay ( p ) ; p -》 rev_flag ^= 1 ;
}
inline void cut ( node* p, node* q ) {
makeroot ( p ) ; access ( q ) ; splay ( q ) ;
q -》 ch [0] = null, p -》 fa = null ;
q -》 update ( ) ;
}
inline void link ( node* p, node* q ) {
makeroot ( p ) ; p -》 fa = q ;
}
inline int dep ( node* p ) {
makeroot ( root [n + 1] ) ;
access ( p ) ;
splay ( p ) ;
return p -》 ch [0] -》 siz ;
}
public :
LinkCutTree ( ) {
null = pool ;
null -》 siz = 0 ; null -》 rev_flag = 0 ;
null -》 ch [0] = null -》 ch [1] = null -》 fa = null ;
}
LinkCutTree ( int n ) : n ( n ) {
null = pool ;
null -》 siz = 0 ; null -》 rev_flag = 0 ;
null -》 ch [0] = null -》 ch [1] = null -》 fa = null ;
for ( register int i = 1 ; i 《= n + 5 ; ++ i ) root [i] = newnode ( null ) ;
}
inline void access ( int u ) { access ( root [u] ) ; }
inline void makeroot ( int u ) { makeroot ( root [u] ) ; }
inline void cut ( int u, int v ) { cut ( root [u], root [v] ) ; }
inline void link ( int u, int v ) { link ( root [u], root [v] ) ; }
inline int dep ( int u ) { return dep ( root [u] ) ; }
} T ;
int a [N] ;
# undef N
//# define ZJC_LOCAL
int main ( ) {
# ifdef ZJC_LOCAL
freopen ( “in.txt”, “r”, stdin ) ;
freopen ( “out.txt”, “w”, stdout ) ;
# endif
int n ( read ( ) ) ;
T = LinkCutTree ( n ) ;
for ( int i = 1 ; i 《= n ; ++ i ) {
T.link ( i, min ( i + ( a [i] = read ( ) ), n + 1 ) ) ;
}
for ( int m = read ( ) ; m ; -- m ) {
int opt ( read ( ) ) ;
if ( opt == 1 ) {
int u ( read ( ) + 1 ) ; // + 1 ?。。?/p>
printf ( “%d\n”, T.dep ( u ) ) ;
} else {
int u ( read ( ) + 1 ), k ( read ( ) ) ;
T.cut ( u, min ( u + a [u], n + 1 ) ) ;
T.link ( u, min ( u + ( a [u] = k ), n + 1 ) ) ;
}
}
}
數據生成器(隨機)
# include 《bits/stdc++.h》
inline int rd ( ) {
return rand ( ) 《《 15 | rand ( ) ;
}
int main ( ) {
srand ( time ( 0 ) ) ;
freopen ( “in.txt”, “w”, stdout ) ;
int n = 200000, m = 100000 ;
printf ( “%d\n”, n ) ;
for ( int i = 1 ; i 《= n ; ++ i ) printf ( “%d ”, rd ( ) % 150 + 1 ) ;
printf ( “\n%d\n”, m ) ;
while ( m -- ) {
int opt ( rand ( ) % 2 + 1 ) ;
printf ( “%d ”, opt ) ;
if ( opt == 1 ) {
printf ( “%d\n”, rd ( ) % n ) ;
} else {
printf ( “%d %d\n”, rd ( ) % n, rd ( ) % 150 + 1 ) ;
}
}
}
評論
查看更多