这是补的知识点,按先序遍历的顺序建立dfs序,用左右两个值代表整个区间,因为dfs序最重要的特点就是子树的区间是连续的

建立线段树时,需要用重新标过的 下标来建立

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define pil pair<int,ll>
#define C 0.5772156649
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1 using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f3f; struct edge{
int to,Next;
}e[N*];
int cnt,head[N];
void add(int u,int v)
{
e[cnt].to=v;
e[cnt].Next=head[u];
head[u]=cnt++;
}
int l[N],r[N],id[N],num;
ll a[N],d[N];
void dfs(int u,int f)
{
num++;
l[u]=num;
id[num]=u;
for(int i=head[u]; ~i; i=e[i].Next)
{
int x=e[i].to;
if(x==f)continue;
d[x]=d[u]+a[x];
dfs(x,u);
}
r[u]=num;
}
ll lazy[N*],sum[N*];
void pushup(int rt)
{
sum[rt]=max(sum[rt<<],sum[rt<<|]);
}
void pushdown(int rt)
{
if(lazy[rt])
{
lazy[rt<<]+=lazy[rt];
lazy[rt<<|]+=lazy[rt];
sum[rt<<]+=lazy[rt];
sum[rt<<|]+=lazy[rt];
lazy[rt]=;
}
}
void build(int l,int r,int rt)
{
lazy[rt]=;
if(l==r)
{
sum[rt]=d[id[l]];
return ;
}
int m=(l+r)>>;
build(ls);
build(rs);
pushup(rt);
}
void update(int L,int R,int v,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
sum[rt]+=v;
lazy[rt]+=v;
return ;
}
//if(l==r)return ;
pushdown(rt);
int m=(l+r)>>;
if(L<=m)update(L,R,v,ls);
if(R>m)update(L,R,v,rs);
pushup(rt);
}
ll query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)return sum[rt];
//cout<<L<<" "<<l<<" "<<r<<" "<<R<<endl;
// if(l==r)return -1e18;
pushdown(rt);
int m=(l+r)>>;
ll ans=-1e18;
if(L<=m)ans=max(ans,query(L,R,ls));
if(R>m)ans=max(ans,query(L,R,rs));
return ans;
}
int main()
{
/* ios::sync_with_stdio(false);
cin.tie(0);*/
int t,res=;
scanf("%d",&t);
while(t--)
{
printf("Case #%d:\n",++res);
int n,m;
scanf("%d%d",&n,&m);
cnt=;
memset(head,-,sizeof head);
for(int i=; i<n; i++)
{
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
for(int i=; i<n; i++)scanf("%I64d",&a[i]);
d[]=a[],num=;
dfs(,-);
build(,n,);
while(m--)
{
int op;
scanf("%d",&op);
if(op==)
{
int x,y;
scanf("%d%d",&x,&y);
update(l[x],r[x],y-a[x],,n,);
a[x]=y;
}
else
{
int x;
scanf("%d",&x);
// cout<<l[x]<<" "<<r[x]<<endl;
printf("%I64d\n",query(l[x],r[x],,n,));
}
// for(int i=0;i<n;i++)cout<<l[i]<<"*****"<<r[i]<<endl;
}
}
return ;
}
/******************** ********************/

hdu5692 dfs序线段树的更多相关文章

  1. HDU5692(dfs序+线段树)

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

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

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

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

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

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

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

  5. BZOJ2434 [Noi2011]阿狸的打字机(AC自动机 + fail树 + DFS序 + 线段树)

    题目这么说的: 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现,这个打字机是这样工作的: 输入小 ...

  6. POJ 3321 DFS序+线段树

    单点修改树中某个节点,查询子树的性质.DFS序 子树序列一定在父节点的DFS序列之内,所以可以用线段树维护. 1: /* 2: DFS序 +线段树 3: */ 4:   5: #include < ...

  7. 【XSY2667】摧毁图状树 贪心 堆 DFS序 线段树

    题目大意 给你一棵有根树,有\(n\)个点.还有一个参数\(k\).你每次要删除一条长度为\(k\)(\(k\)个点)的祖先-后代链,问你最少几次删完.现在有\(q\)个询问,每次给你一个\(k\), ...

  8. F - Change FZU - 2277 (DFS序+线段树)

    题目链接: F - Change FZU - 2277 题目大意: 题意: 给定一棵根为1, n个结点的树. 有q个操作,有两种不同的操作 (1) 1 v k x : a[v] += x, a[v ' ...

  9. BZOJ4551[Tjoi2016&Heoi2016]树——dfs序+线段树/树链剖分+线段树

    题目描述 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为1),有以下 两种操作:1. 标记操作:对某个结点打上标记(在最开始,只有结点1有标记,其他结点均 ...

随机推荐

  1. What are DESC and ASC Keywords?

    What are DESC and ASC Keywords? ASC is the short form for ascending DESC is the short form for desce ...

  2. pmd 使用笔记

    pmd是一块开源的代码静态分析工具,使用java编写,可以自定义规则来进行自己想要的分析.pmd可以单独使用,也可以作为idea.eclipse的插件使用.它的规则分为xpath规则,和java规则. ...

  3. Linux中的系统挂载文件/etc/fstab

    [root@localhost ~]# cat /etc/fstab ## /etc/fstab# Created by anaconda on Wed Oct 5 15:21:46 2016## A ...

  4. 20170401 ABAP调用CIS webservice

    问题: SAP  abap SRM java  调webservice 不通, CIS java  这边的webservice 可以通, WHY? key:请求头,系统框架的问题, LF:因为请求头的 ...

  5. 剑指offer 面试38题

    面试38题: 题:字符串的排列 题目:输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,ca ...

  6. 利用onresize使得div可以随着屏幕大小而自适应的代码

    原文:http://www.jb51.net/article/21831.htm 当我们让div居中时候,一般有两种方法,一种是固定左右宽度,也就是使用像素绝对定位:另一种是用百分比来相对定位,在这种 ...

  7. 类百度DOC编辑区域

    .mainarea{ position:absolute; top:151px; width:100%; bottom:0px; } .edit_wrap{ background:#fcfcfc; p ...

  8. 大数据架构之:Storm

         Storm是一个免费开源.分布式.高容错的实时计算系统,Twitter开发贡献给社区的.Storm令持续不断的流计算变得容易,弥补了Hadoop批处理所不能满足的实时要求. Storm经常用 ...

  9. How to Google

    程序员的基础生存技能 -- 关于搜索引擎的小贴士 如果票选近二十年最伟大的发明,我相信搜索引擎肯定会占据一个不容小觑的位置,它不单是一项发明,更是一项成就,最大程度消灭了信息的不平等.既然人人都可以接 ...

  10. 同类型元素,只有一个被选中js

    <div class="wrap-box flex_row"> <div class="wrap-block"> <div cla ...