分析:一棵以1为根的有根树,然后每个点维护从根到当前节点的路径和,当修改一个点时
只会影响的子树的和,最优值也是子树最大的值
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int N=1e5+;
const LL INF=1ll*1e11;
struct Edge{
int v,next;
}edge[N<<];
int head[N],tot;
void add(int u,int v){
edge[tot].v=v;
edge[tot].next=head[u];
head[u]=tot++;
}
int s[N],t[N],clk,match[N],n,m,a[N];
LL sum[N],c[N<<],lz[N<<];
void dfs(int u,int f){
s[u]=++clk;match[s[u]]=u;
sum[u]+=sum[f];
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].v;
if(v==f)continue;
dfs(v,u);
}
t[u]=clk;
}
void up(int rt){
c[rt]=max(c[rt<<],c[rt<<|]);
}
void down(int rt){
if(lz[rt]){
c[rt<<]+=lz[rt];
c[rt<<|]+=lz[rt];
lz[rt<<]+=lz[rt];
lz[rt<<|]+=lz[rt];
lz[rt]=;
}
}
void build(int rt,int l,int r){
lz[rt]=;
if(l==r){c[rt]=sum[match[l]];return;}
int m=(l+r)>>;
build(rt<<,l,m);
build(rt<<|,m+,r);
up(rt);
}
int tmp;
void modify(int rt,int l,int r,int x,int y){
if(x<=l&&r<=y){
lz[rt]+=1ll*tmp;
c[rt]+=1ll*tmp;
return;
}
int m=(l+r)>>;
down(rt);
if(x<=m)modify(rt<<,l,m,x,y);
if(y>m)modify(rt<<|,m+,r,x,y);
up(rt);
}
LL ask(int rt,int l,int r,int x,int y){
if(x<=l&&r<=y)return c[rt];
LL ans=-INF;
int m=(l+r)>>;
down(rt);
if(x<=m)ans=max(ans,ask(rt<<,l,m,x,y));
if(y>m)ans=max(ans,ask(rt<<|,m+,r,x,y));
return ans;
}
int main()
{
int T,cas=;
scanf("%d",&T);
while(T--){
printf("Case #%d:\n",++cas);
scanf("%d%d",&n,&m);
for(int i=;i<=n;++i)head[i]=-;
clk=tot=;
for(int i=;i<n;++i){
int u,v;
scanf("%d%d",&u,&v);
++u,++v;
add(u,v),add(v,u);
}
for(int i=;i<=n;++i)
scanf("%I64d",&sum[i]),a[i]=(int)sum[i];
dfs(,);
build(,,n);
for(int i=;i<m;++i){
int op,x,y;
scanf("%d%d",&op,&x);
++x;
if(!op){
scanf("%d",&y);
tmp=y-a[x];
a[x]=y;
if(tmp)modify(,,n,s[x],t[x]);
}
else{
printf("%I64d\n",ask(,,n,s[x],t[x]));
}
}
}
return ;
}