题目大意:给你一棵树,每个点都有点权
有3种操作,修改某节点的权值,求树链上节点的权值的最大值,求树链上节点的权值和
树剖裸题,搜一个树链剖分序,用线段树维护一下即可,总时间
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#define inf 0x3f3f3f3f
#define ll long long
#define N 30100
using namespace std;
char str[10];
int n,num,cte;
ll a[N],sum[N<<2],ma[N<<2];
int dep[N],head[N],son[N],sz[N],que[N],pos[N],tp[N],fa[N];
struct Chain{
int up,dw;
Chain(int up,int dw):up(up),dw(dw){}
};
struct EDGE{
int to,nxt;
}edge[N<<1];
void edge_add(int u,int v)
{
cte++;
edge[cte].to = v;
edge[cte].nxt=head[u];
head[u]=cte;
}
void dfs1(int x,int pre)
{
for(int j=head[x];j!=-1;j=edge[j].nxt)
{
int v=edge[j].to;
if(v==pre) continue;
dep[v]=dep[x]+1;
fa[v]=x;
dfs1(v,x);
son[x]=(sz[v]>sz[son[x]])?v:son[x];
sz[x]+=sz[v];
}
sz[x]++;
}
void dfs2(int x)
{
que[++num]=x,pos[x]=num;
if(son[x]) tp[son[x]]=tp[x],dfs2(son[x]);
for(int j=head[x];j!=-1;j=edge[j].nxt)
{
int v=edge[j].to;
if(v==fa[x]||v==son[x]) continue;
tp[v]=v,dfs2(v);
}
}
void pushup(int rt){
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
ma[rt]=max(ma[rt<<1],ma[rt<<1|1]);
}
void build(int l,int r,int rt)
{
if(l==r){sum[rt]=ma[rt]=a[que[l]];return;}
int mid=(l+r)>>1;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
pushup(rt);
}
void update(int x,int l,int r,int rt,ll w)
{
if(l==r){sum[rt]=ma[rt]=a[que[l]];return;}
int mid=(l+r)>>1;
if(x<=mid) update(x,l,mid,rt<<1,w);
else update(x,mid+1,r,rt<<1|1,w);
pushup(rt);
}
ll qmax1(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R) return ma[rt];
int mid=(l+r)>>1;ll ans=-inf;
if(L<=mid) ans=max(ans,qmax1(L,R,l,mid,rt<<1));
if(R>mid) ans=max(ans,qmax1(L,R,mid+1,r,rt<<1|1));
return ans;
}
ll qsum1(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R) return sum[rt];
int mid=(l+r)>>1;ll ans=0;
if(L<=mid) ans+=qsum1(L,R,l,mid,rt<<1);
if(R>mid) ans+=qsum1(L,R,mid+1,r,rt<<1|1);
return ans;
}
int LCA(int x,int y)
{
while(tp[x]!=tp[y]){
if(dep[tp[x]]<dep[tp[y]]) swap(x,y);
x=fa[tp[x]];
}return dep[x]<dep[y]?x:y;
}
queue<Chain>q;
ll Qmax(int x,int y)
{
while(tp[x]!=tp[y]){
if(dep[tp[x]]<dep[tp[y]]) swap(x,y);
q.push(Chain(tp[x],x));
x=fa[tp[x]];
} if(dep[x]<dep[y]) swap(x,y);
q.push(Chain(y,x));ll ans=-inf;
while(!q.empty()){
Chain k=q.front();q.pop();
ans=max(ans,qmax1(pos[k.up],pos[k.dw],1,n,1));
}return ans;
}
ll Qsum(int x,int y)
{
while(tp[x]!=tp[y]){
if(dep[tp[x]]<dep[tp[y]]) swap(x,y);
q.push(Chain(tp[x],x));
x=fa[tp[x]];
} if(dep[x]<dep[y]) swap(x,y);
q.push(Chain(y,x));ll ans=0;
while(!q.empty()){
Chain k=q.front();q.pop();
ans+=qsum1(pos[k.up],pos[k.dw],1,n,1);
}return ans;
}
int main()
{
//freopen("data.in","r",stdin);
scanf("%d",&n);
int x,y;ll z;
memset(head,-1,sizeof(head));
for(int i=1;i<n;i++)
{
scanf("%d%d",&x,&y);
edge_add(x,y);
edge_add(y,x);
}
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
dep[1]=1,dfs1(1,-1);
tp[1]=1,dfs2(1);
build(1,n,1);
int q=0;
scanf("%d",&q);
for(int i=1;i<=q;i++)
{
scanf("%s",str);
if(str[1]=='H'){
scanf("%d%lld",&x,&z),a[x]=z;
update(pos[x],1,n,1,z);
}else if(str[1]=='M'){
scanf("%d%d",&x,&y);
printf("%lld\n",Qmax(x,y));
}else{
scanf("%d%d",&x,&y);
printf("%lld\n",Qsum(x,y));
}
}
return 0;
}