http://acm.hdu.edu.cn/showproblem.php?pid=5692

【思路】

  • 每更新一个点,子树的所有结点都要更新,所以是区间更新
  • 每查询一个点,子树的所有结点都要查询,所以是区间查询最值
  • 线段树上结点的值是每个点到根这条链的权值和

【AC】

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
typedef long long ll;
int n,m;
const int maxn=1e5+;
const int maxm=*maxn;
const ll linf=1e18;
struct edge
{
int to;
int nxt;
}e[maxm];
int head[maxn];
int tot;
int cid;
int st[maxn],ed[maxn];
ll tree[maxn];
ll a[maxn];
ll lazy[maxn*];
ll sum[maxn*];
int que[maxn];
void init()
{
memset(head,-,sizeof(head));
tot=;
cid=;
memset(tree,,sizeof(tree));
memset(lazy,,sizeof(lazy));
}
void addedge(int u,int v)
{
e[tot].to=v;
e[tot].nxt=head[u];
head[u]=tot++;
}
void dfs(int u,int pa)
{
st[u]=++cid;
que[cid]=u;
for(int i=head[u];i!=-;i=e[i].nxt)
{
int v=e[i].to;
if(v==pa) continue;
tree[v]=tree[u]+a[v];
dfs(v,u);
}
ed[u]=cid;
}
void pushup(int rt)
{
sum[rt]=max(sum[rt<<],sum[rt<<|]);
return;
}
void pushdown(int rt)
{
if(lazy[rt])
{
sum[rt<<]+=lazy[rt];
sum[rt<<|]+=lazy[rt];
lazy[rt<<]+=lazy[rt];
lazy[rt<<|]+=lazy[rt];
lazy[rt]=;
}
}
void build(int l,int r,int rt)
{
lazy[rt]=;
if(l==r)
{
sum[rt]=tree[que[l]];
return;
}
int mid=(l+r)>>;
build(lson);
build(rson);
pushup(rt);
} void update(int L,int R,ll val,int l,int r,int rt)
{
if(L<=l && r<=R)
{
lazy[rt]+=val;
sum[rt]+=val;
return;
}
int mid=(l+r)>>;
pushdown(rt);
if(L<=mid)
{
update(L,R,val,lson);
}
if(mid<R)
{
update(L,R,val,rson);
}
pushup(rt);
}
ll query(int L,int R,int l,int r,int rt)
{
if(L<=l && r<=R)
{
return sum[rt];
}
pushdown(rt);
int mid=(l+r)>>;
ll ans=-linf;
if(L<=mid)
{
ans=max(ans,query(L,R,lson));
}
if(mid<R)
{
ans=max(ans,query(L,R,rson));
}
return ans;
}
int main()
{
int T;
scanf("%d",&T);
int cas=;
while(T--)
{
scanf("%d%d",&n,&m);
init();
int u,v;
for(int i=;i<=n-;i++)
{
scanf("%d%d",&u,&v);
u++;v++;
addedge(u,v);
addedge(v,u);
}
for(int i=;i<=n;i++)
{
scanf("%I64d",&a[i]);
}
tree[]=a[];
dfs(,-);
build(,cid,);
printf("Case #%d:\n",++cas);
while(m--)
{
int tp;
scanf("%d",&tp);
if(tp==)
{
int x;ll val;
scanf("%d%I64d",&x,&val);
x++;
update(st[x],ed[x],val-a[x],,cid,);
a[x]=val;
}
else
{
int x;scanf("%d",&x);x++;
ll ans=query(st[x],ed[x],,cid,);
printf("%I64d\n",ans);
}
}
}
return ;
}

【注意】

线段树要开4倍空间,因为这个WA了好久

【DFS序+线段树区间更新区间求最值】HDU 5692 Snacks的更多相关文章

  1. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  2. cdoj 574 High-level ancients dfs序+线段树 每个点所加权值不同

    High-level ancients Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/s ...

  3. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  4. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  5. hdu 1166线段树 单点更新 区间求和

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  6. 洛谷P3178 [HAOI2015]树上操作(dfs序+线段树)

    P3178 [HAOI2015]树上操作 题目链接:https://www.luogu.org/problemnew/show/P3178 题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边 ...

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

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

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

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

  9. DFS序+线段树(bzoj 4034)

    题目链接 题目就不多说了. 本题目,可以用dfs序+线段树做:题目给定了一棵树,树上节点告诉了权值.我们可以先将这棵树进行dfs将一棵树变成线性结构:如图 变成这样后,然后就可以用线段树. 操作1:也 ...

  10. 牛客wannafly 挑战赛14 B 前缀查询(trie树上dfs序+线段树)

    牛客wannafly 挑战赛14 B 前缀查询(trie树上dfs序+线段树) 链接:https://ac.nowcoder.com/acm/problem/15706 现在需要您来帮忙维护这个名册, ...

随机推荐

  1. Codeforces Round #317 (Div. 2) C Lengthening Sticks (组合,数学)

    一个合法的三角形的充要条件是a<b+c,其中a为最长的一边,可以考虑找出所有不满足的情况然后用总方案减去不合法的情况. 对于一个给定的总长度tl(一定要分完,因为是枚举tl,不分配的长度已经考虑 ...

  2. robotframe处理日志中文问题

    unicode('${addr1.text}',"utf-8")

  3. Java形式参数和返回值的问题

    形式参数和返回值的问题 (1).形式参数: A.类名:需要该类的对象. B.抽象类名:需要该类的子类对象. C.接口名:需要该接口的实现类对象. A.类名作为形式参数 class Student { ...

  4. 2017年网络空间安全技术大赛部分writeup

    作为一个bin小子,这次一个bin都没做出来,我很羞愧. 0x00 拯救鲁班七号 具体操作不多说,直接进入反编译源码阶段 可以看到,只要2处的str等于a就可以了,而str是由1处的checkPass ...

  5. numpy中tile函数

    tile函数位于python模块numpy.lib.shape_base中,他的功能是重复某个数组. 函数的形式是tile(A,reps) 函数参数说明中提到A和reps都是array_like的,什 ...

  6. 洛谷 P1120 小木棍[数据加强版]

    这道题可能是我做过的数据最不水的一道题-- 题目传送门 这题可以说是神剪枝,本身搜索并不算难,但剪枝是真不好想(好吧,我承认我看了题解)-- 剪枝: 用桶来存储木棍 在输入的时候记录下最长的木棍和最短 ...

  7. 【php】 php能做什么

    来源:php官方文档 网站和 web 应用程序(服务器端脚本) 命令行脚本 桌面(GUI)应用程序 相信大多数人都不知道第三种,用php竟然可以写GUI,当然是基于PHP-GTK扩展写的

  8. 格式化输出,基本运算符,流程控制主if

    5.5自我总结 一.格式化输出 1.占位符 a = 1 b = 2 print('%S %s'%(a,b)) #1 2 print('%s %s'%(1,2)) #1 2 2.format格式化 a ...

  9. python 实现计算器功能 输入字符串,输出相应结果

    import re formul='1 - 2 *( (6 0- 30+(0-40/5) * (9-2* 5/3 +7 /3*99/4*2998 +10 *568/14)) - (-4*3) / (1 ...

  10. 序列化 random模块应用

    序列化 我们今天学习下序列化,什么是序列化呢? 将原本的字典.列表等内容转换成一个字符串的过程就叫做序列化. 为什么要有序列化模块: 比如,我们在python代码中计算的一个数据需要给另外一段程序使用 ...