树链剖分模板(点)

#include <bits/stdc++.h>
#define lc l,mid,x<<1
#define rc mid+1,r,x<<1|1
#define ls x<<1
#define rs x<<1|1
using namespace std;
typedef unsigned long long LL;
const int maxn = 100005;
const int maxm = 200005;
//int n;
int a[maxn];
int tot,ver[maxm],ne[maxm],he[maxn];
void add( int x,int y ){
    ver[++tot] = y;
    ne[tot] = he[x];
    he[x] = tot;
}
// 数据结构维护的下标是 重新标号之后的下标
int d[maxn], // 深度,下标为初始下标
f[maxn];     // 父节点,下标为初始下标
int sz[maxn],
son[maxn];  // 重儿子,下标为初始下标
void dfs1(int x ){
    int mm = 0;
    sz[x] = 1;son[x] = 0;
    for( int cure = he[x];cure;cure = ne[cure] ){
        int y = ver[cure];
        if( y == f[x] ) continue;
        f[y] = x;
        d[y] = d[x]+1;
        dfs1(y);
        if( sz[y] > mm ){
            mm = sz[y];
            son[x] = y;
        }
        sz[x]+=sz[y];
    }
}
int top[maxn],  // 沿着重链向上走,到达的终点
h[maxn],        // 每个点重新标号之后的值
num,            // 重新标号的下标总数
id[maxn];       // h数组的逆函数
void dfs2(int x){
    h[x]=++num;
    id[num] = x;
    if( son[ f[x] ] == x ){
        top[x] = top[f[x] ];
    }else{
        top[x] = x;
    }
    if(son[x])dfs2(son[x]);
    for( int cure = he[x];cure;cure = ne[cure] ){
        int y =ver[cure];
        if( y ==f[x]||y == son[x] ) continue;
        dfs2(y);
    }
}
int tree[4*maxn],L[4*maxn],R[4*maxn],laz[4*maxn];
void push_up( int x ){
    tree[x] = tree[ls] + tree[rs];
    if(  R[ls]==L[rs] ) tree[x]--;
    L[x] = L[ls];
    R[x] = R[rs];
}
void push_down( int x ){
    if( laz[x] ){
        tree[ls] = tree[rs] = 1;
        L[rs] = R[rs] = L[ls] = R[ls] = laz[x];
        laz[ls] = laz[rs] = laz[x];
        laz[x] = 0;
    }
}
void build( int l,int r,int x ){
    if( l == r ){
        L[x] = R[x] = a[  id[l] ];
        tree[x] = 1;
        return;
    }
    int mid = l+r>>1;
    build(lc);
    build(rc);
    push_up(x);
}
void update( int left,int right,int v,int l,int r,int x ){
    if( left <= l&&right >= r ){
        laz[x] = v;
        tree[x] = 1;
        L[x] = v;
        R[x] = v;
        return;
    }
    push_down(x);
    int mid = l+r>>1;
    if( left <= mid ){
        update( left,right,v,lc );
    }
    if( right > mid ){
        update( left,right,v,rc );
    }
    push_up(x);
}
int query( int left,int right,int l,int r,int x ){
    if( left <= l && right >= r ){
        return tree[x];
    }
    push_down(x);
    int mid = l+r>>1;
    int res = 0;
    if( left <= mid ){
        res += query( left,right,lc );
    }
    if( right > mid ){
        res += query( left,right,rc );
    }
    if( left <= mid && right > mid &&R[ls]==L[rs] ) res--;
    return res;
}
void solvechange( int x,int y,int v ){
    while( top[x] != top[ y ] ){
        if( d[ top[x] ] < d[ top[y] ] ) swap( x,y );
        //change1( h[ top[x] ],h[x],v,1,num,1 );
        update( h[top[x]],h[x],v,1,num,1 );
        x = f[ top[x] ];
    }
    if( d[x] < d[y] ){
        swap( x,y );
    }
    //change1( h[ y ],h[x],v,1,num,1 );
    update( h[ y ],h[x],v,1,num,1 );
}
int ask( int left,int right,int l,int r,int x ){
    if( left <= l && right >= r ){
        return L[x];
    }
    push_down(x);
    int mid = l+r>>1;
    if( left <= mid ){
        return ask( left,right,lc );
    }else return ask( left,right,rc );
    push_up(x);
}
int solveask( int x,int y ){
    int res = 0;
    while( top[x] != top[ y ] ){
        if( d[ top[x] ] < d[ top[y] ] ) swap( x,y );
        //change1( h[ top[x] ],h[x],v,1,num,1 );
        //update( h[top[x]],h[x],v,1,num,1 );
        res += query( h[ top[x] ],h[x],1,num,1 );
        int c1 = ask( h[ top[x] ],h[  top[x]  ],1,num,1 );
        int c2 = ask( h[ f[ top[x] ] ],h[ f[ top[x] ] ],1,num,1 );
        if( c1 == c2 ) res--;
        x = f[ top[x] ];
    }
    if( d[x] < d[y] ){
        swap( x,y );
    }
    //change1( h[ y ],h[x],v,1,num,1 );
    //update( h[ y ],h[x],v,1,num,1 );
    res += query( h[y],h[x],1,num,1 );
    return res;
}
void init( int n ){
    tot = 1;
    num = 0;
    d[1] = 0;
    for( int i = 0;i <= n;i++ ){
        he[i] = 0;
    }
}
int main(){
   int n,m,x,y;
   scanf("%d%d",&n,&m);
   init(n);
   for( int i = 1;i <= n;i++ ) scanf("%d",&a[i]);
   for( int i = 1;i <= n-1;i++ ){
       scanf("%d%d",&x,&y);
       add(x,y);add(y,x);
   }
   dfs1(1);
   dfs2(1);
   build( 1,n,1 );
   char str[10];
   for( int i= 1;i <= m;i++ ){
        scanf("%s%d%d",str,&x,&y);
        if( str[0] == 'C' ){
            int c;scanf("%d",&c);
            solvechange ( x,y,c );
        }else{
            int res = solveask( x,y );
            printf("%d\n",res);
        }
   }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值