题目链接:点这里!!!!
题意:中文题。
题解:
我们记录dis[x]表示0~x的路径长度。
如果我们修改x的权值,x的子树全部都要修改,我们利用dfs序的话,我们相当于给一个区间的值加上某个值;然后询问的x的子树谁的权值最大,相当于区间询问最大值,我们用线段树维护下就可以了。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<vector>
#include<bitset>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cstdlib>
#include<cmath>
#define LL long long
#define pb push_back
#define pa pair<int,int>
#define clr(a,b) memset(a,b,sizeof(a))
#define lson lr<<1,l,mid
#define rson lr<<1|1,mid+1,r
#define bug(x) printf("%d++++++++++++++++++++%d\n",x,x)
#define key_value ch[ch[root][1]][0]
#pragma comment(linker, "/STACK:102400000000,102400000000")
const LL MOD = 1000000007;
const int N = 1e5+15;
const int maxn = 8e3+15;
const int letter = 130;
const LL INF = 1e18;
const double pi=acos(-1.0);
const double eps=1e-10;
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,q;
int tot,head[N],num_node;
int l[N],r[N],dfn[N],a[N];
LL ans[N<<2],mark[N<<2];
LL dis[N];
struct edges{
int to,next;
}e[N<<1];
void init(){
num_node=tot=0,clr(head,-1);
}
void addedges(int u,int v){
e[tot].to=v,e[tot].next=head[u],head[u]=tot++;
}
void dfs(int x,int fa){
l[x]=++num_node;
dfn[num_node]=x;
for(int i=head[x];i!=-1;i=e[i].next){
int to=e[i].to;
if(to==fa) continue;
dis[to]+=dis[x];
dfs(to,x);
}
r[x]=num_node;
}
void pushup(int lr){
ans[lr]=max(ans[lr<<1],ans[lr<<1|1]);
}
void pushdown(int lr){
if(mark[lr]){
ans[lr<<1]+=mark[lr],ans[lr<<1|1]+=mark[lr];
mark[lr<<1]+=mark[lr],mark[lr<<1|1]+=mark[lr];
mark[lr]=0;
}
}
void build(int lr,int l,int r){
mark[lr]=0;
if(l==r) {
ans[lr]=dis[dfn[l]];
return;
}
int mid=(l+r)>>1;
build(lson);
build(rson);
pushup(lr);
}
void update(int ll,int rr,int v,int lr,int l,int r){
if(ll<=l&&r<=rr){
ans[lr]+=(LL)v;
mark[lr]+=(LL)v;
return;
}
pushdown(lr);
int mid=(l+r)>>1;
if(ll<=mid) update(ll,rr,v,lson);
if(rr>mid) update(ll,rr,v,rson);
pushup(lr);
}
LL query(int ll,int rr,int lr,int l,int r){
if(ll<=l&&r<=rr){
return ans[lr];
}
pushdown(lr);
int mid=(l+r)>>1;
LL max1=-INF;
if(ll<=mid) max1=max(max1,query(ll,rr,lson));
if(rr>mid) max1=max(max1,query(ll,rr,rson));
return max1;
}
int main(){
int T,cas=0,id,x,y;
scanf("%d",&T);
while(T--){
printf("Case #%d:\n",++cas);
init();
scanf("%d%d",&n,&q);
for(int i=0;i<n-1;i++){
scanf("%d%d",&x,&y);
addedges(x,y);
addedges(y,x);
}
for(int i=0;i<n;i++) scanf("%I64d",dis+i),a[i]=(int)dis[i];
dfs(0,-1);
build(1,1,n);
while(q--){
scanf("%d%d",&id,&x);
if(id==1){
printf("%I64d\n",query(l[x],r[x],1,1,n));
}
else {
scanf("%d",&y);
update(l[x],r[x],y-a[x],1,1,n);
a[x]=y;
}
}
}
return 0;
}