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. The Django Book - 第四章 模板

    使用模板的最基本方式:1.根据原始模板代码字符串创建一个Template对象2. 使用字典创建一套Context变量3. 调用Template对象的render方法,传入Context变量参数 In ...

  2. Java代理设计模式(Proxy)的四种具体实现:静态代理和动态代理

    面试问题:Java里的代理设计模式(Proxy Design Pattern)一共有几种实现方式?这个题目很像孔乙己问"茴香豆的茴字有哪几种写法?" 所谓代理模式,是指客户端(Cl ...

  3. 多进程Queue

    进程间通讯 不同进程间内存是不共享的,要想实现两个进程间的数据交换,可以用以下方法: Queues 使用方法跟threading里的queue差不多 from multiprocessing impo ...

  4. GYM 101604 || 20181010

    看着前面咕咕咕的国庆集训 难受 十月十日要萌一天哇www A.字符串 题意:给定一个字符串 问能否交换两个字符或者不交换字符,使其成为回文串 之前写的太丑 重写一遍加一堆 if 竟然过了w 思路:求出 ...

  5. 两个input标签之间间隙问题的解决

    <input type="text"> <input type="button" value="搜索"> 代码显示效 ...

  6. shell脚本,tee小工具的用法。

    解释: tee是个工具 , 它的作用就是把标准输出,复制一份,扔文件里 ,原标准输出还输出,-a就相当于 >> 追加到文件里的意思.  不加就是 > 重定向到文件里去.

  7. Shell脚本调用SQL文格式

    Shell脚本调用SQL文格式 1. 定义需要执行的SQL文,以及需要输出文件 OUTFILE=\${DATADIR}/\${FILENAME} SQLFILE=\${DATADIR}/check_t ...

  8. linux内核启动修复

    linux内核启动修复 首先看一下linux内核重要文件grub.conf 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 # gru ...

  9. Perl学习之四:语句(续)

    循环控制:1.last 退出标签的语句块2.next 3.redo不推荐,循环次数不可控 4.goto不推荐.***************************************标签: 先 ...

  10. python 有4个数字1234,能组成多少个互不相同且无重复的三位数数字。

    def output(): count = 0 for i in range(1,5): for j in range(1, 5): for k in range(1, 5): if i==j or ...