分析:一棵以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 ;
}

HDU5692 Snacks DFS+线段树的更多相关文章

  1. HDU 5877 dfs+ 线段树(或+树状树组)

    1.HDU 5877  Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...

  2. Codeforces1110F Nearest Leaf dfs + 线段树 + 询问离线

    Codeforces1110F dfs + 线段树 + 询问离线 F. Nearest Leaf Description: Let's define the Eulerian traversal of ...

  3. dfs+线段树 zhrt的数据结构课

    zhrt的数据结构课 这个题目我觉得是一个有一点点思维的dfs+线段树 虽然说看起来可以用树链剖分写,但是这个题目时间卡了树剖 因为之前用树剖一直在写这个,所以一直想的是区间更新,想dfs+线段树,有 ...

  4. HDU5692 Snacks DFS序 线段树

    去博客园看该题解 题目 HDU5692 Snacks Problem Description 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的 ...

  5. hdu-5692 Snacks(dfs序+线段树)

    题目链接: Snacks Problem Description   百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的 ...

  6. hdu 5692(dfs+线段树) Snacks

    题目http://acm.hdu.edu.cn/showproblem.php?pid=5692 题目说每个点至多经过一次,那么就是只能一条路线走到底的意思,看到这题的格式, 多个询问多个更新, 自然 ...

  7. 【Codeforces-707D】Persistent Bookcase DFS + 线段树

    D. Persistent Bookcase Recently in school Alina has learned what are the persistent data structures: ...

  8. Educational Codeforces Round 6 E. New Year Tree dfs+线段树

    题目链接:http://codeforces.com/contest/620/problem/E E. New Year Tree time limit per test 3 seconds memo ...

  9. 2014 Super Training #9 F A Simple Tree Problem --DFS+线段树

    原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 这题本来是一个比较水的线段树,结果一个ma ...

随机推荐

  1. Timeline

    Timeline面板   Chrome开发者工具详解(3)-Timeline面板 注: 这一篇主要讲解面板Timeline,参考了Google的相关文档,主要用于公司内部技术分享.. 更新时间:201 ...

  2. python学习笔记22(group)

    正则表达式中,group()用来提出分组截获的字符串,()用来分组 import re a = "123abc456" print re.search("([0-9]*) ...

  3. python学习笔记13(模块、包)

    在Python中有一个概念叫做模块(module),比如在Python中要调用sqrt函数,必须用import关键字引入math这个模块,下面就来了解一下Python中的模块. 模块文件以.py后缀结 ...

  4. 为什么要在<button>元素中添加type属性

    在HTML中<button> 标签定义一个按钮. <button type="button">Click Me!</button> 在 butt ...

  5. arguments.callee查询调用b函数的是哪个函数

    // function functionname(){ // function b(){ // console.log(arguments.callee.caller.name); // } // b ...

  6. uva 10608

    简单并查集  水水..... #include <cstdio> #include <cstring> #define maxn 30005 int fa[maxn],ans[ ...

  7. Spark 1.60的executor schedule

    第一次看源码还是Spark 1.02.这次看新源码发现调度方式有了一些新的特征,在这里随便写一下. 不变的是,master还是接收Appclient和worker的消息,并且在接收RegisterAp ...

  8. Unique Binary Search Trees II

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  9. 动态链接库中分配内存引起的问题-- windows已在XX.exe中触发一个断点

    动态链接库中分配内存引起的 本文主要是探讨关于在动态链接库分配的内存在主程序中释放所产生的问题,该问题是我在刚做的PJP工程中所遇到的,由于刚碰到之时感动比较诡异(这也是学识不够所致),所以将它写下来 ...

  10. source code analyzer 功能强大的C/C++源代码分析软件 Celerity CRACK 破解版

    特色 迅捷是一个功能强大的C/C++源代码分析软件.可以处理数百万行的源程序代码.支持标准及K&R风格的C/C++.对每一个打开的源代码工程,通过建立一个包含丰富交叉引用关系的数据库,显示其所 ...