![](https://i-blog.csdnimg.cn/blog_migrate/de48d7f7e3583839e7eb2e386fd46689.png)
AC code
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define MAXN 100000
#define INF LONG_LONG_MAX
struct node{
LL l,r;
LL sum,maxn;
}tr[MAXN<<2+5];
vector<LL>a[MAXN+5];
LL n;
LL b[MAXN+5];
LL p[MAXN+5],fp[MAXN+5],pos;
LL dep[MAXN+5];
LL top[MAXN+5],fa[MAXN+5],siz[MAXN+5],son[MAXN+5];
void dfs1(LL x){
siz[x]=1;
dep[x]=dep[fa[x]]+1;
for(LL i=0;i<a[x].size();i++){
LL xx=a[x][i];
if(xx==fa[x])continue;
fa[xx]=x;
dfs1(xx);
siz[x]+=siz[xx];
if(!son[x]||siz[son[x]]<siz[xx]){
son[x]=xx;
}
}
}
void dfs2(LL x,LL xtop){
top[x]=xtop;
p[x]=++pos;fp[pos]=x;
if(son[x])dfs2(son[x],xtop);
for(LL i=0;i<a[x].size();i++){
LL xx=a[x][i];
if(xx==fa[x]||xx==son[x])continue;
dfs2(xx,xx);
}
}
void pushup(LL rt){
tr[rt].sum=tr[rt<<1].sum+tr[rt<<1|1].sum;
tr[rt].maxn=max(tr[rt<<1].maxn,tr[rt<<1|1].maxn);
}
void build(LL rt,LL l,LL r){
tr[rt].l=l;tr[rt].r=r;
if(l==r){
tr[rt].maxn=tr[rt].sum=b[fp[l]];
return ;
}
LL mid=(l+r)>>1;
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
pushup(rt);
}
void update(LL rt,LL l,LL r,LL val){
if(tr[rt].l>r||tr[rt].r<l){
return ;
}else if(tr[rt].l>=l&&tr[rt].r<=r){
tr[rt].sum=tr[rt].maxn=val;
return ;
}else{
update(rt<<1,l,r,val);
update(rt<<1|1,l,r,val);
}
pushup(rt);
}
LL querysum(LL rt,LL l,LL r){
if(tr[rt].l>r||tr[rt].r<l){
return 0;
}else if(tr[rt].l>=l&&tr[rt].r<=r){
return tr[rt].sum;
}else{
return querysum(rt<<1,l,r)+querysum(rt<<1|1,l,r);
}
}
LL querymax(LL rt,LL l,LL r){
if(tr[rt].l>r||tr[rt].r<l){
return -INF;
}else if(tr[rt].l>=l&&tr[rt].r<=r){
return tr[rt].maxn;
}else{
return max(querymax(rt<<1,l,r),querymax(rt<<1|1,l,r));
}
}
LL query_max(LL x,LL y){
LL f1=top[x],f2=top[y],tmp=-INF;
while(f1!=f2){
if(dep[f1]<dep[f2]){
swap(f1,f2);swap(x,y);
}
tmp=max(tmp,querymax(1,p[f1],p[x]));
x=fa[f1];f1=top[x];
}
if(dep[x]>dep[y])swap(x,y);
return max(tmp,querymax(1,p[x],p[y]));
}
LL query_sum(LL x,LL y){
LL f1=top[x],f2=top[y],tmp=0;
while(f1!=f2){
if(dep[f1]<dep[f2]){
swap(f1,f2);
swap(x,y);
}
tmp+=querysum(1,p[f1],p[x]);
x=fa[f1];f1=top[x];
}
if(dep[x]>dep[y])swap(x,y);
return tmp+querysum(1,p[x],p[y]);
}
int main(){
scanf("%lld",&n);
for(LL i=1;i<n;i++){
LL u,v;
scanf("%lld%lld",&u,&v);
a[u].push_back(v);
a[v].push_back(u);
}
for(LL i=1;i<=n;i++){
scanf("%lld",&b[i]);
}
dfs1(1);
dfs2(1,1);
build(1,1,n);
LL q;
scanf("%lld",&q);
while(q--){
char s[15];
scanf("%s",s);
if(s[0]=='C'){
LL u,t;
scanf("%lld%lld",&u,&t);
update(1,p[u],p[u],t);
}else if(s[1]=='M'){
LL u,v;
scanf("%lld%lld",&u,&v);
printf("%lld\n",query_max(u,v));
}else{
LL u,v;
scanf("%lld%lld",&u,&v);
printf("%lld\n",query_sum(u,v));
}
}
}