传送门:【SPOJ】2798 Query on a tree again!
题目分析:水水的。。树链剖分,然后以点1为根剖分,这样每次查询点1总在最高点,满足了极大的特殊性,这样我们就可以将每个区间都保存下来,然后从靠近点1的区间搜起,存在一个就直接返回。如果搜完了还没有就返回-1。
代码如下:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std ; typedef long long LL ; #define travel( e , H , u ) for ( Edge* e = H[u] ; e ; e = e -> next ) #define rep( i , a , b ) for ( int i = ( a ) ; i < ( b ) ; ++ i ) #define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i ) #define FOR( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i ) #define clr( a , x ) memset ( a , x , sizeof a ) #define cpy( a , x ) memcpy ( a , x , sizeof a ) #define ls ( o << 1 ) #define rs ( o << 1 | 1 ) #define lson ls , l , m #define rson rs , m + 1 , r #define root 1 , 1 , n #define rt o , l , r #define mid ( ( l + r ) >> 1 ) const int MAXN = 100005 ; const int MAXE = 200005 ; struct Edge { int v ; Edge* next ; } E[MAXE] , *H[MAXN] , *edge ; struct Stack { int L , R ; Stack () {} Stack ( int L , int R ) : L ( L ) , R ( R ) {} } S[MAXN] ; int sum[MAXN << 2] ; int siz[MAXN] ; int pos[MAXN] ; int pre[MAXN] ; int top[MAXN] ; int son[MAXN] ; int dep[MAXN] ; int val[MAXN] ; int idx[MAXN] ; int tree_idx ; int point ; int n , q ; void clear () { edge = E ; siz[0] = 0 ; dep[0] = 0 ; clr ( H , 0 ) ; clr ( sum , 0 ) ; } void addedge ( int u , int v ) { edge -> v = v ; edge -> next = H[u] ; H[u] = edge ++ ; } void dfs ( int u ) { siz[u] = 1 ; son[u] = 0 ; travel ( e , H , u ) { int v = e -> v ; if ( v != pre[u] ) { pre[v] = u ; dep[v] = dep[u] + 1 ; dfs ( v ) ; siz[u] += siz[v] ; if ( siz[v] > siz[son[u]] ) son[u] = v ; } } } void rewrite ( int u , int top_element ) { top[u] = top_element ; pos[u] = ++ tree_idx ; idx[tree_idx] = u ; if ( son[u] ) rewrite ( son[u] , top_element ) ; travel ( e , H , u ) { int v = e -> v ; if ( v != pre[u] && v != son[u] ) rewrite ( v , v ) ; } } void update ( int x , int o , int l , int r ) { if ( l == r ) { sum[o] ^= 1 ; return ; } int m = mid ; if ( x <= m ) update ( x , lson ) ; else update ( x , rson ) ; sum[o] = sum[ls] + sum[rs] ; } int ask_idx ( int L , int R , int o , int l , int r ) { if ( !sum[o] ) return -1 ; if ( l == r ) return idx[l] ; int m = mid ; if ( R <= m ) return ask_idx ( L , R , lson ) ; if ( m < L ) return ask_idx ( L , R , rson ) ; int index = ask_idx ( L , R , lson ) ; if ( index == -1 ) index = ask_idx ( L , R , rson ) ; return index ; } int query ( int x , int y ) { point = 0 ; while ( top[x] != top[y] ) { S[point ++] = Stack ( pos[top[y]] , pos[y] ) ; y = pre[top[y]] ; } S[point ++] = Stack ( pos[x] , pos[y] ) ; rev ( i , point - 1 , 0 ) { int index = ask_idx ( S[i].L , S[i].R , root ) ; if ( ~index ) return index ; } return -1 ; } void solve () { int x , y ; clear () ; rep ( i , 1 , n ) { scanf ( "%d%d" , &x , &y ) ; addedge ( x , y ) ; addedge ( y , x ) ; } dfs ( 1 ) ; rewrite ( 1 , 1 ) ; while ( q -- ) { scanf ( "%d%d" , &x , &y ) ; if ( x == 0 ) update ( pos[y] , root ) ; else printf ( "%d\n" , query ( 1 , y ) ) ; } } int main () { while ( ~scanf ( "%d%d" , &n , &q ) ) solve () ; return 0 ; }