记录编号 585358 评测结果 AAAAAAAAAA
题目名称 [ZJOI 2008]树的统计Count 最终得分 100
用户昵称 Gravatar┭┮﹏┭┮ 是否通过 通过
代码语言 C++ 运行时间 1.510 s
提交时间 2023-12-06 20:52:11 内存使用 23.03 MiB
显示代码纯文本
#include <bits/stdc++.h> 
using namespace std;
#define ll long long
const int N = 1e5+10; 
int n,root,q;
struct made{
    int ver,nx;
}e[N<<1];
int hd[N],tot,cnt;
int fa[N],size[N],son[N],dep[N];
int a[N],rnk[N],dfn[N],top[N];
struct Tree{
    int l,r,sum,ma;
    #define l(x) t[x].l
    #define r(x) t[x].r
    #define sum(x) t[x].sum
    #define ma(x) t[x].ma
}t[N<<3];
void pushup(int x){
    sum(x) = sum(x<<1) + sum(x<<1|1);
    ma(x) = max(ma(x<<1),ma(x<<1|1));
}
void build(int x,int l,int r){
    l(x) = l,r(x) = r,ma(x) = INT_MIN;
    if(l == r){
        ma(x) = sum(x) = a[rnk[l]];
        return;
    }
    int mid = l + r >> 1;
    build(x<<1,l,mid);
    build(x<<1|1,mid+1,r);
    pushup(x);
}
void change(int x,int l,int z){
    if(l(x) == r(x)){
        sum(x) = ma(x) = z;
        return;
    }
    int mid = l(x) + r(x) >> 1;
    if(l <= mid)change(x<<1,l,z);
    if(l > mid)change(x<<1|1,l,z);
    pushup(x);
}
int asksum(int x,int l,int r){
    if(l <= l(x) && r(x) <= r)return sum(x);
    int mid = l(x) + r(x) >> 1,ans = 0;
    if(l <= mid)ans += asksum(x<<1,l,r);
    if(r > mid)ans += asksum(x<<1|1,l,r);
    return ans;
}
int askmax(int x,int l,int r){
    if(l <= l(x) && r(x) <= r)return ma(x);
    int mid = l(x) + r(x) >> 1,ans = INT_MIN;
    if(l <= mid)ans = max(ans,askmax(x<<1,l,r));
    if(r > mid)ans = max(ans,askmax(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;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;
    }
}
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);
    }
}
int calmax(int x,int y){
    int fx = top[x],fy = top[y],ans = INT_MIN;
    while(fx != fy){
        if(dep[fx] < dep[fy])swap(x,y),swap(fx,fy);
        ans = max(ans,askmax(1,dfn[fx],dfn[x]));
        x = fa[fx];fx = top[x];
    }
    if(dep[x] > dep[y])swap(x,y);
    ans = max(ans,askmax(1,dfn[x],dfn[y]));
    return ans;
}
int calsum(int x,int y){
    int fx = top[x],fy = top[y],ans = 0;
    while(fx != fy){
        if(dep[fx] < dep[fy])swap(x,y),swap(fx,fy);
        ans += asksum(1,dfn[fx],dfn[x]);
        x = fa[fx];fx = top[x];
    }
    if(dep[x] > dep[y])swap(x,y);
    ans += asksum(1,dfn[x],dfn[y]);
    return ans;
}
int main(){
    freopen("bzoj_1036.in","r",stdin);
    freopen("bzoj_1036.out","w",stdout);
    scanf("%d",&n);
    for(int i = 1;i < n;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        add(x,y),add(y,x);
    }
    for(int i = 1;i <= n;i++)scanf("%d",&a[i]);
    root = 1;
    dep[root] = 1;
    dfs1(root);
    dfs2(root,root);
    build(1,1,n);
    scanf("%d",&q);
    for(int i = 1;i <= q;i++){
        char c[10];int x,y;
        scanf("%s%d%d",c,&x,&y);
        if(c[1] == 'H')change(1,dfn[x],y);
        else if(c[1] == 'M')printf("%d\n",calmax(x,y));
        else printf("%d\n",calsum(x,y));
    }
    
    return 0;
    
}