[BZOJ3639]-QTREE7-LCT+set

說在前面

開始無腦碼起了代碼emmm


題目

BZOJ3639傳送門

題目大意

給出一個有 n 個節點的樹,每個節點有顏色(黑或白)和權值,現在需要支持以下三個操作:

  • 0 u :詢問 u 點所在的同色聯通塊的最大值
  • 1 u :將 u 的顏色取反
  • 2 u w :將節點 u 的權值改爲 w

範圍:操作總次數 m105n105 ,點權|w|109

輸入輸出格式

輸入格式:
第一行一個整數 n ,表示節點個數
接下來 n1 行,每行兩個整數 u,v 描述一條樹邊
接下來兩行,每行 n 個整數,第一行顏色,第二行權值

接下來一個整數 m ,表示操作次數
最後 m 行,每行一個操作,格式如題

輸出格式:
對於每一個詢問操作,輸出一行一個整數表示答案


解法

這道題和 QTREE6 差不多
在 QTREE6 中,維護的信息是 虛子樹大小以及鏈上大小
這道題維護的是 虛子樹的最大值以及鏈上最大值,虛子樹最大值用set即可

查詢set最大值時,調用begin()/end()是 Θ(1)
只有在虛實子樹轉變的時候,才需要插入/刪除,也就是說Access的均攤變成了log22n ,複雜度是ok的


下面是代碼

注意改權值操作,兩棵樹裏都得改
一開始me只改了一棵樹裏的,WA了幾發

#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;

int N , M , tp , head[100005] ;
int fa[100005] , col[100005] , val[100005] ;
struct Path{
    int pre , to ;
} p[200005] ;

struct Comp{
    bool operator () ( const int &A , const int &B ){ return A > B ; }
} ;
template < typename T > void smax( T &A , T B ){ if( A < B ) A = B ; }

struct Node{
    Node *ch[2] , *fa ;
    set<int,Comp> imax ; int rmax , cur ;
    int getMax(){ return max( rmax , *( imax.begin() ) ) ; }
    void update(){
        rmax = cur ;
        if( ch[0] ) smax( rmax , ch[0]->getMax() ) ;
        if( ch[1] ) smax( rmax , ch[1]->getMax() ) ;
    }
} ;

struct LCT{
    Node w[100005] ;

    void init(){
        for( int i = 1 ; i <= N ; i ++ ){
            w[i].ch[0] = w[i].ch[1] = w[i].fa = NULL ;
            w[i].cur = w[i].rmax = val[i] ;
            w[i].imax.clear() , w[i].imax.insert( -1234567890 ) ;
        }
    }

    bool isRoot( Node *nd ){
        if( !nd->fa ) return true ;
        return nd->fa->ch[0] != nd && nd->fa->ch[1] != nd ;
    }

    void Rotate( Node *nd , int k ){
        Node *x = nd->ch[k] ;
        if( !isRoot( nd ) ){
            if( nd->fa->ch[0] == nd ) nd->fa->ch[0] = x ;
            else nd->fa->ch[1] = x ;
        } x->fa = nd->fa ;
        if( x->ch[k^1] ) x->ch[k^1]->fa = nd ;
        nd->ch[k] = x->ch[k^1] ;
        x->ch[k^1] = nd , nd->fa = x ;
        nd->update() , x->update() ;
    }

    void Splay( Node *nd ){
        // pushdown here
        while( !isRoot( nd ) ){
            Node *fa = nd->fa , *gdfa = fa->fa ;
            int pn = ( fa->ch[1] == nd ) , pf ;
            if( !isRoot( fa ) ){
                pf = ( gdfa->ch[1] == fa ) ;
                if( pn == pf ) swap( fa , gdfa ) ;
                Rotate( fa , pn ) , Rotate( gdfa , pf ) ;
            } else Rotate( fa , pn ) ;
        }
    }

    void Access( Node *nd ){
        Node *tmp = NULL ;
        while( nd ){
            Splay( nd ) ;
            if( tmp ) nd->imax.erase( tmp->getMax() ) ;
            if( nd->ch[1] ) nd->imax.insert( nd->ch[1]->getMax() ) ;
            nd->ch[1] = tmp ; nd->update() ;
            tmp = nd , nd = nd->fa ;
        }
    }

    void link( int x , int y ){
        if( !y ) return ;
        Node *u = ( w + x ) , *v = ( w + y ) ; // link u to v 
        Access( v ) , Splay( v ) , Splay( u ) ;
        u->fa = v , v->imax.insert( u->getMax() ) ;
    }

    void cut( int x , int y ){
        if( !y ) return ;
        Node *u = ( w + x ) , *v = ( w + y ) ;// cut u from v
        Access( v ) , Splay( v ) , Splay( u ) ;
        v->imax.erase( u->getMax() ) , u->fa = NULL ;
    }

    Node *getLeft( Node *nd ){
        while( nd->ch[0] ) nd = nd->ch[0] ; return nd ;
    }

    int Query( int x ){
        Node *u = ( w + x ) , *tmp , *t2 ;
        Access( u ) , Splay( u ) ;
        tmp = getLeft( u ) , Splay( tmp ) ;
        if( col[ tmp - w ] != col[x] ){
            Access( t2 = getLeft( tmp->ch[1] ) ) , Splay( tmp ) ;
            return t2->getMax() ;
        } else return tmp->getMax() ;
    }

    void change( int x , int w ){
        Node *u = ( this->w + x ) ;
        Access( u ) , Splay( u ) ;
        u->cur = w , u->update() ;
    }
} Tree[2] ;

void In( int t1 , int t2 ){
    p[++tp] = ( Path ){ head[t1] , t2 } ; head[t1] = tp ;
    p[++tp] = ( Path ){ head[t2] , t1 } ; head[t2] = tp ;
}

void dfs( int u , int f ){
    for( int i = head[u] ; i ; i = p[i].pre ){
        int v = p[i].to ;
        if( v == f ) continue ;
        Tree[ col[v] ].link( v , u ) ;
        fa[v] = u , dfs( v , u ) ;
    }
}

void solve(){
    scanf( "%d" , &M ) ;
    for( int i = 1 , opt , u , w ; i <= M ; i ++ ){
        scanf( "%d%d" , &opt , &u ) ;
        if( opt == 2 ){ // change val
            scanf( "%d" , &w ) ;
            Tree[0].change( u , w ) , Tree[1].change( u , w ) ;
        } else if( opt == 1 ){ //change col
            Tree[ col[u] ].cut( u , fa[u] ) ;
            col[u] ^= 1 , Tree[ col[u] ].link( u , fa[u] ) ;
        } else printf( "%d\n" , Tree[ col[u] ].Query( u ) ) ;
    }
}

int main(){
    scanf( "%d" , &N ) ;
    for( int i = 1 , u , v ; i < N ; i ++ )
        scanf( "%d%d" , &u , &v ) , In( u , v ) ;
    for( int i = 1 ; i <= N ; i ++ ) scanf( "%d" , &col[i] ) ;
    for( int i = 1 ; i <= N ; i ++ ) scanf( "%d" , &val[i] ) ;
    Tree[0].init() , Tree[1].init() ;
    dfs( 1 , 1 ) ; solve() ;
}
發佈了268 篇原創文章 · 獲贊 29 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章