# Snacks

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1660    Accepted Submission(s): 403

Problem Description

Input

Output

Sample Input
1 6 5 0 1 1 2 0 3 3 4 5 3 7 -5 100 20 -5 -7 1 1 1 3 0 2 -1 1 1 1 5

Sample Output
Case #1: 102 27 2 20

Source

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <bits/stdc++.h>
using namespace std;
typedef long long  LL;
const int N =100005;
const LL INF = 1e18;
struct Edge{
int v,next;
}edge[N<<2];
int in[N],out[N];
LL lazy[N<<2];
LL sum[N<<2],cost[N]; ///sum 存前缀和
LL dis[N],b[N];
int n,q;
void init(){
tot = cnt = 0;
}
}
void dfs(int u,int pre){
in[u] = ++cnt;
b[cnt] = dis[u]; ///原来的树的下标对应线段树的下标
int v = edge[k].v;
if(v==pre) continue;
dis[v] = dis[u] + cost[v];
dfs(v,u);
}
out[u] = cnt;
}
void pushup(int idx){
sum[idx] = max(sum[idx<<1],sum[idx<<1|1]);
}
void pushdown(int idx)
{
if(lazy[idx])
{
sum[idx << 1] += lazy[idx];
sum[idx << 1 | 1] += lazy[idx];
lazy[idx << 1] += lazy[idx];
lazy[idx << 1 | 1] += lazy[idx];
lazy[idx] = 0;
}
return;
}
void build(int l,int r,int idx){
lazy[idx] = 0;
if(l==r){
sum[idx] =  b[l];
return ;
}
int mid = (l+r)>>1;
build(l,mid,idx<<1);
build(mid+1,r,idx<<1|1);
pushup(idx);
}
void update(int l,int r,int L,int R,int idx,int val){
if(l>=L&&r<=R){
sum[idx] =sum[idx] + val;
lazy[idx] =lazy[idx] + val;
return;
}
int mid = (l+r)>>1;
pushdown(idx);
if(mid>=L) update(l,mid,L,R,idx<<1,val);
if(mid<R)  update(mid+1,r,L,R,idx<<1|1,val);
pushup(idx);
}
LL MAX = -1;
void query(int l,int r,int L,int R,int idx){
if(l >= L&& r <= R){
MAX = max(MAX,sum[idx]);
return ;
}
int mid = (l+r)>>1;
pushdown(idx);
if(mid>=L)  query(l,mid,L,R,idx<<1);
if(mid<R)   query(mid+1,r,L,R,idx<<1|1);
}
int main()
{
int tcase,t=1;
scanf("%d",&tcase);
while(tcase--){
init();
scanf("%d%d",&n,&q);
for(int i=0;i<n-1;i++){
int u,v;
scanf("%d%d",&u,&v);
}
for(int i=0;i<n;i++){
scanf("%lld",&cost[i]);
}
dis[0] = cost[0];
dfs(0,-1);
build(1,n,1);
printf("Case #%d:\n",t++);
while(q--){
int opr ,x, y;
scanf("%d",&opr);
if(opr==1){
scanf("%d",&x);
MAX = -INF;
query(1,n,in[x],out[x],1);
printf("%lld\n",MAX);
}
else{
scanf("%d%d",&x,&y);
LL change = (LL)y-cost[x];  ///这里累加变化量
update(1,n,in[x],out[x],1,change);
cost[x] = y;
}
}
}
return 0;
}

posted @ 2016-09-30 23:29  樱花庄的龙之介大人  阅读(...)  评论(...编辑  收藏