![luoguP2590 [ZJOI2008]树的统计(树链剖分) luoguP2590 [ZJOI2008]树的统计(树链剖分)](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<algorithm>
#include<ctime>
#include<queue>
#define rg register
#define lst long long
#define N 30050
#define ls (now<<1)
#define rs (now<<1|1)
using namespace std; int n,Q,cnt,ss,root,ans;
struct EDGE{
int to,nxt;
}edge[N<<];
struct TREE{
int l,r,Max,sum;
}ljl[N<<];
int v[N],head[N];
int fa[N],deep[N],size[N],num[N];
int vv[N],top[N],son[N]; inline void add(rg int p,rg int q){edge[++cnt]=(EDGE){q,head[p]};head[p]=cnt;}
inline int read()
{
rg int s=,m=;rg char ch=getchar();
while(ch!='-'&&(ch<''||ch>''))ch=getchar();
if(ch=='-')m=-,ch=getchar();
while(ch>=''&&ch<='')s=(s<<)+(s<<)+ch-'',ch=getchar();
return s*m;
} inline void init()
{
n=read();root=;
for(rg int i=;i<n;++i)
{
rg int p=read(),q=read();
add(p,q),add(q,p);
}
for(rg int i=;i<=n;++i)v[i]=read();
} void dfs_1(rg int now,rg int fm,rg int dep)
{
rg int kk=;
fa[now]=fm,deep[now]=dep,size[now]=;
for(rg int i=head[now];i;i=edge[i].nxt)
{
rg int qw=edge[i].to;
if(qw!=fm)
{
dfs_1(qw,now,dep+);
size[now]+=size[qw];
if(size[qw]>kk)kk=size[qw],son[now]=qw;
}
}
} void dfs_2(rg int now,rg int up)
{
num[now]=++ss,vv[ss]=v[now],top[now]=up;
if(son[now])dfs_2(son[now],top[now]);
for(rg int i=head[now];i;i=edge[i].nxt)
{
rg int qw=edge[i].to;
if(qw!=fa[now]&&qw!=son[now])
dfs_2(qw,qw);
}
} inline void Pushup(rg int now)
{
ljl[now].Max=max(ljl[ls].Max,ljl[rs].Max);
ljl[now].sum=ljl[ls].sum+ljl[rs].sum;
} void build(rg int now,rg int ll,rg int rr)
{
ljl[now]=(TREE){ll,rr};
if(ll==rr)
{
ljl[now].Max=ljl[now].sum=vv[ll];
return;
}
rg int mid=(ll+rr)>>;
build(ls,ll,mid),build(rs,mid+,rr);
Pushup(now);
} void change(rg int now,rg int kk,rg int x)
{
if(ljl[now].l==kk&&ljl[now].r==kk)
{
ljl[now].Max=ljl[now].sum=x;
return;
}
rg int mid=(ljl[now].l+ljl[now].r)>>;
if(kk<=mid)change(ls,kk,x);
else change(rs,kk,x);
Pushup(now);
} int get_max(rg int now,rg int ll,rg int rr)
{
rg int ss=-1e9;
if(ljl[now].l>=ll&&ljl[now].r<=rr)return ljl[now].Max;
rg int mid=(ljl[now].l+ljl[now].r)>>;
if(rr<=mid)ss=max(ss,get_max(ls,ll,rr));
if(ll>mid) ss=max(ss,get_max(rs,ll,rr));
if(ll<=mid&&rr>mid)ss=max(ss,max(get_max(ls,ll,mid),get_max(rs,mid+,rr)));
return ss;
} int get_sum(rg int now,rg int ll,rg int rr)
{
rg int ss=;
if(ljl[now].l>=ll&&ljl[now].r<=rr)return ljl[now].sum;
rg int mid=(ljl[now].l+ljl[now].r)>>;
if(rr<=mid)ss+=get_sum(ls,ll,rr);
if(ll>mid) ss+=get_sum(rs,ll,rr);
if(ll<=mid&&rr>mid)ss+=get_sum(ls,ll,mid)+get_sum(rs,mid+,rr);
return ss;
} inline void work(rg int x,rg int y,rg int op)
{
if(num[x]>num[y])swap(x,y);
if(!op)ans=max(ans,get_max(,num[x],num[y]));
if(op) ans+=get_sum(,num[x],num[y]);
} inline void Work(rg int x,rg int y,rg int op)
{
while(top[x]!=top[y])
{
if(deep[top[x]]<deep[top[y]])swap(x,y);
work(top[x],x,op);
x=fa[top[x]];
}
work(x,y,op);
} inline void Ans()
{
Q=read();
for(rg int i=;i<=Q;++i)
{
char type[];cin>>type;
rg int x=read(),y=read();
if(type[]=='N'){ change(,num[x],y);}//change
if(type[]=='X'){ans=-1e9;Work(x,y,);printf("%d\n",ans);}//qmax
if(type[]=='M'){ans=; Work(x,y,);printf("%d\n",ans);}//qsum
}
} int main()
{
init();
dfs_1(root,,);
dfs_2(root,root);
build(,,n);
Ans();
return ;
}