记录编号 585348 评测结果 AAAAAAAAAA
题目名称 [HAOI 2015]树上操作 最终得分 100
用户昵称 Gravatar┭┮﹏┭┮ 是否通过 通过
代码语言 C++ 运行时间 0.895 s
提交时间 2023-12-06 18:09:24 内存使用 39.26 MiB
显示代码纯文本
#include <bits/stdc++.h> 
using namespace std;
#define ll long long
const int N = 2e5+10;
int n,m;
struct made{
    int nx,ver;
}e[N<<2];
int hd[N],tot,cnt;
int fa[N],size[N],son[N],dep[N];
int top[N],dfn[N],rnk[N];
ll a[N];
struct Tree{
    int l,r;
    ll sum,la;
    #define l(x) t[x].l
    #define la(x) t[x].la
    #define r(x) t[x].r
    #define sum(x) t[x].sum
}t[N<<3];
void build(int x,int l,int r){
    l(x) = l,r(x) = r;
    if(l == r){sum(x) = a[rnk[l]];return;}
    int mid = l + r >> 1;
    build(x<<1,l,mid);
    build(x<<1|1,mid+1,r);
    sum(x) = sum(x<<1) + sum(x<<1|1);
}
void pushdown(int x){
    if(!la(x))return;
    la(x<<1) += la(x),la(x<<1|1) += la(x);
    sum(x<<1) += la(x) * (r(x<<1) - l(x<<1) + 1);
    sum(x<<1|1) += la(x) * (r(x<<1|1) - l(x<<1|1) + 1);
    la(x) = 0;
}
void change(int x,int l,int r,ll z){
    if(l <= l(x) && r(x) <= r){
        la(x) += z;
        sum(x) += z * (r(x) - l(x) + 1);//错误 
        pushdown(x);
        return;
    }
    pushdown(x);
    int mid = l(x) + r(x) >> 1;
    if(l <= mid)change(x<<1,l,r,z);
    if(r > mid)change(x<<1|1,l,r,z);
    sum(x) = sum(x<<1) + sum(x<<1|1);
}
ll ask(int x,int l,int r){
    if(l <= l(x) && r(x) <= r)return sum(x);
    int mid = l(x) + r(x) >> 1;ll ans = 0;
    pushdown(x);
    if(l <= mid)ans += ask(x<<1,l,r);
    if(r > mid)ans += ask(x<<1|1,l,r);
    return ans;
}
//------------------------
void add(int x,int y){
    tot++;
    e[tot].nx = hd[x],e[tot].ver = y,hd[x] = tot;
}
void dfs1(int x){
    size[x] = 1;
    for(int i = hd[x];i > 0;i = e[i].nx){
        int y = e[i].ver;
        if(dep[y])continue;
        dep[y] = dep[x] + 1;
        fa[y] = x;
        dfs1(y);
        size[x] += size[y];
        if(!son[x] || size[y] > size[son[x]])son[x] = y;
    }
//    if(x==1)cout<<1<<endl;
}
void dfs2(int x,int t){
    dfn[x] = ++cnt;
    rnk[cnt] = x;
    top[x] = t;
    if(!son[x])return;
    dfs2(son[x],t);
    for(int i = hd[x];i;i = e[i].nx){
        int y = e[i].ver;
        if(y != fa[x] && y != son[x])dfs2(y,y);
    }
}
ll cal(int x,int y){
    int fx = top[x],fy = top[y];ll ans = 0;
    while(fx != fy){
        if(dep[fx] < dep[fy])swap(x,y),swap(fx,fy);
        ans += ask(1,dfn[fx],dfn[x]);
        x = fa[fx];fx = top[x];
    }
    if(dep[x] > dep[y])swap(x,y);
    ans += ask(1,dfn[x],dfn[y]);
    return ans;
}
int main(){
    freopen("haoi2015_t2.in","r",stdin);
    freopen("haoi2015_t2.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= n;i++)scanf("%lld",&a[i]);
    for(int i = 1;i < n;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        add(x,y),add(y,x);
    }
    dep[1] = 1;
//    cout<<1<<endl;
    dfs1(1);
//    cout<<1<<endl;
    dfs2(1,1);
    build(1,1,n);
    for(int i = 1;i <= m;i++){
        int op,x,y;
        scanf("%d%d",&op,&x);
        if(op == 1){
            scanf("%d",&y);
            change(1,dfn[x],dfn[x],y);
        }
        else if(op == 2){
            scanf("%d",&y);
            change(1,dfn[x],dfn[x]+size[x]-1,y);
        }
        else printf("%lld\n",cal(1,x));
    }
    
    return 0;
    
}