HDU5692 Snacks DFS+线段树
分析:一棵以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+线段树的更多相关文章
- HDU 5877 dfs+ 线段树(或+树状树组)
1.HDU 5877 Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...
- Codeforces1110F Nearest Leaf dfs + 线段树 + 询问离线
Codeforces1110F dfs + 线段树 + 询问离线 F. Nearest Leaf Description: Let's define the Eulerian traversal of ...
- dfs+线段树 zhrt的数据结构课
zhrt的数据结构课 这个题目我觉得是一个有一点点思维的dfs+线段树 虽然说看起来可以用树链剖分写,但是这个题目时间卡了树剖 因为之前用树剖一直在写这个,所以一直想的是区间更新,想dfs+线段树,有 ...
- HDU5692 Snacks DFS序 线段树
去博客园看该题解 题目 HDU5692 Snacks Problem Description 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的 ...
- hdu-5692 Snacks(dfs序+线段树)
题目链接: Snacks Problem Description 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的 ...
- hdu 5692(dfs+线段树) Snacks
题目http://acm.hdu.edu.cn/showproblem.php?pid=5692 题目说每个点至多经过一次,那么就是只能一条路线走到底的意思,看到这题的格式, 多个询问多个更新, 自然 ...
- 【Codeforces-707D】Persistent Bookcase DFS + 线段树
D. Persistent Bookcase Recently in school Alina has learned what are the persistent data structures: ...
- 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 ...
- 2014 Super Training #9 F A Simple Tree Problem --DFS+线段树
原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 这题本来是一个比较水的线段树,结果一个ma ...
随机推荐
- 【BZOJ3524】 [Poi2014]Couriers
Description 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. ...
- java定时任务
java定时任务实现方法: public class TimingTask { private static int count = 0; private static SpiderService s ...
- 远程登陆MS azure Linux 虚拟机
http://blogs.technet.com/b/uktechnet/archive/2013/11/12/running-a-remote-desktop-on-a-windows-azure- ...
- PAT-乙级-1008. 数组元素循环右移问题 (20)
1008. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数,在不允 ...
- 如何编写敏捷开发中的user story
http://blog.csdn.net/chengyb74/article/details/4762247 对于敏捷开发来说,User Story是开发的基础,它不同于传统的瀑布式开发方式,而是把原 ...
- docker 通过commit方法创建镜像(Tomcat+Java+Scala)
前一阵试了试写Dockerfile创建docker image,但有时全靠Dockerfile写实在有些难度,退而求其次试一试使用commit来创建镜像: 想了想干脆创建一个Java+Scala+To ...
- nginx上用fastcgi配置python环境
费了2天的功夫,翻阅了无数的中文.英文资料,终于搞定.写下此文留待以后翻阅用 本文环境,centOS 5.4 ,Nignx-0.8.49, Python 2.6.5 ========== ...
- POJ3282+模拟
模拟题 /* 模拟 注意:相同一边的车有先后顺序! */ #include<stdio.h> #include<string.h> #include<stdlib.h&g ...
- android webview网页控件
一个WebView的简单例子 .在开发过程中应该注意几点: 1.AndroidManifest.xml中必须使用许可"android.permission.INTERNET",否则 ...
- Qt: 界面中使用中文(三种方法,QApplication::translate可指定编码)
界面中的字符串, 尽量的使用QObject::tr(text); 以便以后转换界面语言, 即使现在你还不考虑这个问题. 方法一: 每次设置时都使用: button->setText(QAppl ...