hdu5692 dfs序线段树
2017-11-25 01:42
225 查看
这是补的知识点,按先序遍历的顺序建立dfs序,用左右两个值代表整个区间,因为dfs序最重要的特点就是子树的区间是连续的
建立线段树时,需要用重新标过的 下标来建立
View Code
建立线段树时,需要用重新标过的 下标来建立
#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-12; const int N=100000+10,maxn=90000+10,inf=0x3f3f3f3f; struct edge{ int to,Next; }e[N*2]; int cnt,head ; void add(int u,int v) { e[cnt].to=v; e[cnt].Next=head[u]; head[u]=cnt++; } int l ,r ,id ,num; ll a ,d ; 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*4],sum[N*4]; void pushup(int rt) { sum[rt]=max(sum[rt<<1],sum[rt<<1|1]); } void pushdown(int rt) { if(lazy[rt]) { lazy[rt<<1]+=lazy[rt]; lazy[rt<<1|1]+=lazy[rt]; sum[rt<<1]+=lazy[rt]; sum[rt<<1|1]+=lazy[rt]; lazy[rt]=0; } } void build(int l,int r,int rt) { lazy[rt]=0; if(l==r) { sum[rt]=d[id[l]]; return ; } int m=(l+r)>>1; 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)>>1; 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)>>1; 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=0; scanf("%d",&t); while(t--) { printf("Case #%d:\n",++res); int n,m; scanf("%d%d",&n,&m); cnt=0; memset(head,-1,sizeof head); for(int i=1; i<n; i++) { int x,y; scanf("%d%d",&x,&y); add(x,y); add(y,x); } for(int i=0; i<n; i++)scanf("%I64d",&a[i]); d[0]=a[0],num=0; dfs(0,-1); build(1,n,1); while(m--) { int op; scanf("%d",&op); if(op==0) { int x,y; scanf("%d%d",&x,&y); update(l[x],r[x],y-a[x],1,n,1); a[x]=y; } else { int x; scanf("%d",&x); // cout<<l[x]<<" "<<r[x]<<endl; printf("%I64d\n",query(l[x],r[x],1,n,1)); } // for(int i=0;i<n;i++)cout<<l[i]<<"*****"<<r[i]<<endl; } } return 0; } /******************** ********************/
View Code
相关文章推荐
- HDU5692 Snacks DFS序 线段树
- HDU5692:Snacks(DFS序 & 线段树区间更新)
- [dfs序][线段树][模板]hdu5692 Snacks
- HDU5692(dfs + 线段树)
- HDU5692 Snacks(dfs序 + 线段树)
- HDU5692 Snacks (dfs序 + 线段树)
- 【dfs+线段树get/set】 编程之美 tree
- 【POJ 3321】【dfs序(讲解)+(树状数组或者线段树)】Apple Tree【给你一颗树,最初每个节点上都有一个苹果,有两种操作单点修改和查询子树的苹果个数】
- hdu多校第三场 Solve this interesting problem (5323 线段树模拟 + DFS)
- [NOIP模拟题][Catalan数][逆元][贪心][线段树][DFS][搜索顺序剪枝]
- Codeforces Round #381 (Div. 2) D. Alyona and a tree dfs+二分+线段树延迟操作、树形化线性
- 洛谷P2982 [USACO10FEB]慢下来Slowing down(线段树 DFS序 区间增减 单点查询)
- [LCT 线段树 dfs序] BZOJ 3779 重组病毒
- J - Assign the task - hdu 3974(DFS建树+简单线段树)
- POJ 3415 Common Substrings 后缀数组 + 线段树 + dfs
- Codeforces Round #442 (Div. 2) E. Danil and a Part-time Job (dfs序树型转线性 线段树区间修改区间查询)
- 【DFS序】【线段树】bzoj4034 [HAOI2015]T2
- HDU 3974 Assign the task(DFS建树+线段树)
- hdu5323 给出左右边界,求出线段树最小的大小(暴力dfs搜索)
- Codeforces Round #200 (Div. 1) D. Water Tree(dfs序加线段树)