Snacks

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

Problem Description
百度科技园内有n个零食机,零食机之间通过n−1条路相互连通。每个零食机都有一个值v,表示为小度熊提供零食的价值。
由于零食被频繁的消耗和补充,零食机的价值v会时常发生变化。小度熊只能从编号为0的零食机出发,并且每个零食机至多经过一次。另外,小度熊会对某个零食机的零食有所偏爱,要求路线上必须有那个零食机。
为小度熊规划一个路线,使得路线上的价值总和最大。

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
中文题  题意不解释
修改每个节点的权值  改变的是这个点的子树中的点到根节点的价值和
求根节点路过这个点的路径的最大和  就是求根节点到这个点已经这个点的子树中的那个最大价值和
我们就可以用DFS序 得到每个点的时间戳  将这个时间戳转化为线段树 修改一个点的权值 就相当他区间修改 修改这个区间的权值
查询也就是查询区间的最大值
 
 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string.h>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const double PI=acos(-1.0);
const double eps=0.0000000001;
const int INF=1e9;
const int N=+;
int head[N];
int tot;
ll a[N];
ll b[N];
int n,m;
struct NOde{
int l,r;
ll val;
ll lazy;
}tree[N*];
struct node{
int to,next;
}edge[N<<];
int L[N],R[N];
int time;
ll dis[N];
void init(){
memset(head,-,sizeof(head));
tot=;
time=;
}
void add(int u,int v){
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
}
void DFS(int x,int fa){
L[x]=++time;
b[time]=x;
for(int i=head[x];i!=-;i=edge[i].next){
int v=edge[i].to;
if(v==fa)continue;
a[v]=a[x]+a[v];
DFS(v,x);
}
R[x]=time;
}
void pushdown(int pos){
if(tree[pos].lazy){
tree[pos<<].val+=tree[pos].lazy;
tree[pos<<|].val+=tree[pos].lazy;
tree[pos<<].lazy+=tree[pos].lazy;
tree[pos<<|].lazy+=tree[pos].lazy;
tree[pos].lazy=; }
}
void build(int left,int right,int pos){
// cout<<55<<endl;
tree[pos].l=left;
tree[pos].r=right;
tree[pos].lazy=;
int mid=tree[pos].l+tree[pos].r>>;
if(tree[pos].l==tree[pos].r){
tree[pos].val=a[b[left]];
return ;
}
build(left,mid,pos<<);
build(mid+,right,pos<<|);
tree[pos].val=max(tree[pos<<].val,tree[pos<<|].val); }
void update(int left,int right,int pos,ll x){
if(tree[pos].l==left&&tree[pos].r==right){
tree[pos].lazy+=x;
tree[pos].val+=x;
return ;
}
pushdown(pos);
int mid=(tree[pos].l+tree[pos].r)>>;
if(mid>=right)update(left,right,pos<<,x);
else if(mid<left)update(left,right,pos<<|,x);
else{
update(left,mid,pos<<,x);
update(mid+,right,pos<<|,x);
}
tree[pos].val=max(tree[pos<<].val,tree[pos<<|].val);
}
ll query(int left,int right,int pos){
if(tree[pos].l==left&&tree[pos].r==right){
return tree[pos].val;
}
pushdown(pos);
int mid=(tree[pos].l+tree[pos].r)>>;
if(mid>=right){
return query(left,right,pos<<);
}
else if(left>mid){
return query(left,right,pos<<|);
}
else{
return max(query(left,mid,pos<<),query(mid+,right,pos<<|));
}
}
int main(){
int t;
scanf("%d",&t);
int Case=;
while(t--){
init();
scanf("%d%d",&n,&m);
int u,v;
for(int i=;i<n;i++){
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
for(int i=;i<n;i++){
scanf("%I64d",&a[i]);
dis[i]=a[i];
}
DFS(,-);
build(,time,);
int flag;
int x;
ll y;
printf("Case #%d:\n",Case++);
while(m--){
scanf("%d",&flag);
if(flag==){
scanf("%d%I64d",&x,&y);
update(L[x],R[x],,(ll)y-dis[x]);
dis[x]=y;
}
else{
scanf("%d",&x);
ll ans=query(L[x],R[x],);
printf("%I64d\n",ans);
} } }
}

HDU 5692 Snacks(DFS序+线段树)的更多相关文章

  1. HDU.5692 Snacks ( DFS序 线段树维护最大值 )

    HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...

  2. hdu 5692 Snacks(dfs时间戳+线段树)

    Snacks Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  3. HDU5692 Snacks DFS序 线段树

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

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

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

  5. Assign the task HDU - 3974(dfs序+线段树)

    There is a company that has N employees(numbered from 1 to N),every employee in the company has a im ...

  6. HDU 4366 Successor( DFS序+ 线段树 )

    Successor Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  7. Educational Codeforces Round 6 E dfs序+线段树

    题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 1 : 给定点的子树群体涂色 2 : 求给定点的子树中有多少种颜色 比较容易想到dfs序+线段树去做 dfs序是很久以前看的bilibili ...

  8. 【BZOJ-3252】攻略 DFS序 + 线段树 + 贪心

    3252: 攻略 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 339  Solved: 130[Submit][Status][Discuss] D ...

  9. Codeforces 343D Water Tree(DFS序 + 线段树)

    题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...

随机推荐

  1. CSS——img

    img标签初始化:在低版本的ie浏览器会自带边框,所以建议border:0px.

  2. html——meta标签、link标签

    <meta> 元素可提供有关页面的元信息(meta-information),比如针对搜索引擎和更新频度的描述和关键词. <meta> 标签位于文档的头部,不包含任何内容.&l ...

  3. 8、scala面向对象编程之对象

    1.  Object 2.伴生对象 3.让object继承抽象类 4.apply方法 5.main方法 6.用object实现枚举功能 1.  Object Object,相当于class的单个实例, ...

  4. Cesium学习笔记(九):导入3D模型(obj转gltf)

    在用cesium的过程中难免需要导入别人做好的3D模型,这时候就需要将这些模型转成gltf格式了 当然,官方也给了我们一个网页版的转换器,但是毕竟是网页版的,效率极其低下,文件还不能太大,所以我们就需 ...

  5. 微信小程序音频长度获取的问题

    小程序推荐使用wx.createInnerAudioContext()创建的innerAudioContext,我们也通过这个接口创建音频.音频的长度可以通过属性获取: 但是,给innerAudioC ...

  6. centos添加永久的环境变量

    cd /etc/profile.d/ 创建一个sh文件 vi dotnetpath.sh 内容如下: export PATH=$PATH:/opt/dotnet 保存,重启,这就有了一个永久的环境变量

  7. 考试T1总结(又CE?!)

    考试T1CE... 最近不适合考试 T1 扶苏是个喜欢一边听古风歌一边写数学题的人,所以这道题其实是五三原题.歌曲中的主人公看着墙边的海棠花,想起当年他其实和自己沿着墙边种了一排海棠,但是如今都已枯萎 ...

  8. LINUX C: 获取本地指定网卡的IP地址

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> ...

  9. Python----递归------Eight Queens 八皇后问题

    递归思想是算法编程中的重要思想. 作为初学者,对递归编程表示很蒙逼,每次遇到需要递归的问题,心里就有一万头草泥马飞过~~~~~~(此处略去一万头草泥马) 在B站看数据结构与算法的视频时,视频中给了两个 ...

  10. jquery制作动态添加表单行与删除表单行

    <script type="text/javascript" src="js/jquery1.7.js"></script> <s ...