这里是评测记录
还有两个点TLE了
#include<bits/stdc++.h>
#define INF 1000000000
using namespace std;
const int N=3e4+10;
int n,h[N],tot,w[N],Q,depth[N],fa[N],son[N],size[N],lg[N],id[N],label,wi[N],top[N],maxx,res;
struct Node{
int ver,next;
}l[N*2];
struct node{
int maxi,sum,l,r;
}t[N*4];
void add(int x,int y){
l[++tot].ver=y;
l[tot].next=h[x];
h[x]=tot;
}
void dfs1(int x,int f,int dep){
depth[x]=dep;
fa[x]=f;
size[x]=1;
int maxson=-1;
for(int i=h[x];i;i=l[i].next){
int y=l[i].ver;
if(y==f) continue;
dfs1(y,x,dep+1);
size[x]+=size[y];
if(son[y]>maxson) son[x]=y,maxson=size[y];
}
}
void dfs2(int x,int topf){
id[x]=++label;
wi[label]=w[x];
top[x]=topf;
if(!son[x]) return ;
dfs2(son[x],topf);
for(int i=h[x];i;i=l[i].next){
int y=l[i].ver;
if(y==fa[x]||y==son[x]) continue;
dfs2(y,y);
}
}
void pushup(int p){
t[p].sum=t[p<<1].sum+t[p<<1|1].sum;
t[p].maxi=max(t[p<<1].maxi,t[p<<1|1].maxi);
}
void build(int p,int l,int r){
t[p].l=l;
t[p].r=r;
if(l==r) {
t[p].sum=wi[l];
t[p].maxi=wi[l];
return ;
}
int mid=(l+r)>>1;
build(p<<1,l,mid);
build(p<<1|1,mid+1,r);
pushup(p);
}
void update(int p,int x,int y){
if(t[p].l==t[p].r){
t[p].sum=y;
t[p].maxi=y;
return ;
}
int mid=(t[p].l+t[p].r)>>1;
if(x<=mid) update(p<<1,x,y);
else update(p<<1|1,x,y);
pushup(p);
}
void querymaxi(int p,int l,int r){
if(l<=t[p].l&&r>=t[p].r){
maxx=max(maxx,t[p].maxi);
return ;
}
int mid=(t[p].l+t[p].r)>>1;
if(l<=mid) querymaxi(p<<1,l,r);
if(r>mid) querymaxi(p<<1|1,l,r);
}
int pathmaxi(int x,int y){
maxx=-INF;
while(top[x]!=top[y]){
if(depth[top[x]]<depth[top[y]]) swap(x,y);
querymaxi(1,id[top[x]],id[x]);
x=fa[top[x]];
}
if(depth[x]<depth[y]) swap(x,y);
querymaxi(1,id[y],id[x]);
return maxx;
}
void querysum(int p,int l,int r){
if(l<=t[p].l&&r>=t[p].r){
res+=t[p].sum;
return ;
}
int mid=(t[p].l+t[p].r)>>1;
if(l<=mid) querysum(p<<1,l,r);
if(r>mid) querysum(p<<1|1,l,r);
}
int pathsum(int x,int y){
int ans=0;
while(top[x]!=top[y]){
if(depth[top[x]]<depth[top[y]]) swap(x,y);
res=0;
querysum(1,id[top[x]],id[x]);
ans+=res;
x=fa[top[x]];
}
if(depth[x]<depth[y]) swap(x,y);
res=0;
querysum(1,id[y],id[x]);
ans+=res;
return ans;
}
void init()
{
cin>>n;
for(int i=1;i<n;i++){
int x,y;
cin>>x>>y;
add(x,y);
add(y,x);
}
for(int i=1;i<=n;i++) cin>>w[i];
cin>>Q;
dfs1(1,0,1);
dfs2(1,1);
build(1,1,n);
}
int main()
{
freopen("test.in","r",stdin);
freopen("write.out","w",stdout);
init();
while(Q--){
string s;
cin>>s;
if(s[1]=='M') {
int u,v;
cin>>u>>v;
cout<<pathmaxi(u,v)<<endl;
}
if(s[1]=='S') {
int u,v;
cin>>u>>v;
cout<<pathsum(u,v)<<endl;
}
if(s[1]=='H') {
int u,t;
cin>>u>>t;
update(1,u,t);
}
}
}