毒瘤结构/树链剖分

· · 个人记录

树链剖分

学习Link

AC Code

#include<iostream>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<queue>
#include<algorithm>
#define L x << 1
#define R x << 1 | 1
#define int long long
#define maxn 1170000
using namespace std;
const int inf = 1e9+7 ;
int read( )
{
    int k = 0 , h = 1; char c = getchar( );
    while( c < '0' || c > '9' ){ if( c == '-' ) h = -1; c = getchar( ); }
    while( c >= '0' && c <= '9' ){ k = ( k << 1 ) + ( k << 3 ) + ( c ^ 48 ); c = getchar( ); }
    return k * h;
}
int n,m,r,mod,a[ maxn ],cnt,head[ maxn ];
int dep[ maxn ],size[ maxn ];
// 每个点的深度 , 每个节点的子树大小
int son[ maxn ],f[ maxn ]; 
//非叶子节点的重儿子编号,父亲节点
int tot,id[ maxn ],rak[ maxn ],top[ maxn ];
struct node{ int u , v , ne ; }e[ maxn ];//存图
struct tree{ int l , r , lazy , sum ; }tr[ maxn ];//线段树

void add( int u , int v )
{
    e[ ++cnt ] = ( node ){ u , v , head[ u ] };
    head[ u ] = cnt;
}

/* 线段树 */

void update( int x )
{ 
    tr[ x ].sum = ( tr[ L ].sum + tr[ R ].sum ) % mod; 
}

void build( int x , int l , int r )
{
    tr[ x ].l = l , tr[ x ].r = r;
    if( l == r ) 
    { 
        tr[ x ].sum = a[ rak[ l ] ] % mod; 
        return; 
    }
    int mid = ( tr[ x ].l + tr[ x ].r ) >> 1;
    build( L , l , mid ); build( R , mid + 1 , r );
    update( x );
}

void pushdown( int x )
{
    if( tr[ x ].lazy )
    {
        tr[ L ].lazy = ( tr[ L ].lazy + tr[ x ].lazy ) % mod;
        tr[ R ].lazy = ( tr[ R ].lazy + tr[ x ].lazy ) % mod;
        tr[ L ].sum = ( tr[ L ].sum + tr[ x ].lazy * ( tr[ L ].r - tr[ L ].l + 1 ) ) % mod;
        tr[ R ].sum = ( tr[ R ].sum + tr[ x ].lazy * ( tr[ R ].r - tr[ R ].l + 1 ) ) % mod;
        tr[ x ].lazy = 0;
    }
}

void change( int x , int l , int r , int k )
{
    if( l <= tr[ x ].l and tr[ x ].r <= r )
    {
        tr[ x ].lazy += k , tr[ x ].lazy %= mod;
        tr[ x ].sum += k * ( tr[ x ].r - tr[ x ].l + 1 ) , tr[ x ].sum %= mod;
        return;
    }
    pushdown( x );
    int mid = ( tr[ x ].l + tr[ x ].r ) >> 1;
    if( l <= mid ) change( L , l , r , k );
    if( r > mid ) change( R , l , r , k );
    update( x );
}

int query( int x , int l , int r )
{
    int ans = 0;
    if( l <= tr[ x ].l and tr[ x ].r <= r ) return tr[ x ].sum % mod;
    pushdown( x );
    int mid = ( tr[ x ].l + tr[ x ].r ) >> 1;
    if( l <= mid ) ans += query( L , l , r ) , ans %= mod;
    if( r > mid ) ans += query( R , l , r ) , ans %= mod;
    return ans % mod;
}

/*树链剖分*/

void dfs1( int x , int fa )
{
    dep[ x ] = dep[ fa ] + 1; //节点深度
    size[ x ] = 1; //子树大小
    f[ x ] = fa;//标记父亲
    for( int i = head[ x ] ; i ; i = e[ i ].ne )
    {
        int y = e[ i ].v ; 
        if( y == fa ) continue;//如果是父亲
        dfs1( y , x ); 
        size[ x ] += size[ y ];//更新子树大小
        if( size[ y ] > size[ son[ x ] ] ) son[ x ] = y;//记录非叶子节点的重儿子编号
    }
}

void dfs2( int x , int tp )//tp当前链最顶端的节点
{
    top[ x ] = tp; //点所在链的顶点
    id[ x ] = ++tot; //每个点的新编号
    rak[ tot ] = x;//赋值
    if( son[ x ] ) dfs2( son[ x ] , tp ); //先处理重儿子
    else return;
    for( int i = head[ x ] ; i ; i = e[ i ].ne )
    {
        int y = e[ i ].v; 
        if( y == son[ x ] or y == f[ x ] ) continue;
        dfs2( y , y );//每一个轻儿子都有一条从自己开始的链
    }
}

//区间求和
int Linkquery( int x , int y )
{
    int ans = 0;
    while( top[ x ] != top[ y ] ) //两个点不在同一条链上时
    {
        if( dep[ top[ x ] ] < dep[ top[ y ] ] ) swap( x , y );//把x改为深度更深的那个点
        ans += query( 1 , id[ top[ x ] ] , id[ x ] ) , ans %= mod;//加上x点到x点所在链顶端这一段区间的点权和
        x = f[ top[ x ] ];//x跳到x链顶点的父节点
    }
    if( id[ x ] > id[ y ] ) swap( x , y );//深度更深
    return ( ans + query( 1 , id[ x ] , id[ y ] ) ) % mod;//再加上点f[ top[ x ] ] 到 y 的区间和
}

//区间修改
void Linkchange( int x , int y , int k )
{
    while( top[ x ] != top[ y ] )
    {
        if( dep[ top[ x ] ] < dep[ top[ y ] ] ) swap( x , y );//使x为深度更深的点
        change( 1 , id[ top[ x ] ] , id[ x ] , k );//修改x到x所在链的顶点
        x = f[ top[ x ] ];//x链顶点的父亲节点
    }
    if( id[ x ] > id[ y ] ) swap( x , y );//使x成为更深的
    change( 1 , id[ x ] , id[ y ] , k );//修改f[ top[ x ] ] 到y节点
}

signed main( )
{
    n = read( ) , m = read( ) , r = read( ) , mod = read( );
    for( int i = 1 ; i <= n ; ++i ) a[ i ] = read( );
    for( int i = 1 ; i < n ; ++i )
    {
        int u = read( ) , v = read( );
        add( u , v ) ; 
        add( v , u );
    }
    dfs1( r , 0 ); 
    dfs2( r , r );
    build( 1 , 1 , n );
    for( int i = 1 ; i <= m ; ++i )
    {
        int op = read( );
        if( op == 1 )
        {
            int x = read( ) , y = read( ) , k = read( );
            Linkchange( x , y , k );
        }
        if( op == 2 )
        {
            int x = read( ) , y = read( );
            cout << Linkquery( x , y ) << "\n";
        }
        if( op == 3 )
        {
            int x = read( ) , k = read( );
            change( 1 , id[ x ] , id[ x ] + size[ x ] - 1 , k );
        }
        if( op == 4 )
        {
            int x = read( );
            cout << query( 1 , id[ x ] , id[ x ] + size[ x ] - 1 ) << "\n";
        }
    }
    return 0;
}