hdu5692 dfs序线段树
这是补的知识点,按先序遍历的顺序建立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序线段树的更多相关文章
- HDU5692(dfs序+线段树)
Snacks Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- Educational Codeforces Round 6 E dfs序+线段树
题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 1 : 给定点的子树群体涂色 2 : 求给定点的子树中有多少种颜色 比较容易想到dfs序+线段树去做 dfs序是很久以前看的bilibili ...
- 【BZOJ-3252】攻略 DFS序 + 线段树 + 贪心
3252: 攻略 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 339 Solved: 130[Submit][Status][Discuss] D ...
- Codeforces 343D Water Tree(DFS序 + 线段树)
题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...
- BZOJ2434 [Noi2011]阿狸的打字机(AC自动机 + fail树 + DFS序 + 线段树)
题目这么说的: 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现,这个打字机是这样工作的: 输入小 ...
- POJ 3321 DFS序+线段树
单点修改树中某个节点,查询子树的性质.DFS序 子树序列一定在父节点的DFS序列之内,所以可以用线段树维护. 1: /* 2: DFS序 +线段树 3: */ 4: 5: #include < ...
- 【XSY2667】摧毁图状树 贪心 堆 DFS序 线段树
题目大意 给你一棵有根树,有\(n\)个点.还有一个参数\(k\).你每次要删除一条长度为\(k\)(\(k\)个点)的祖先-后代链,问你最少几次删完.现在有\(q\)个询问,每次给你一个\(k\), ...
- F - Change FZU - 2277 (DFS序+线段树)
题目链接: F - Change FZU - 2277 题目大意: 题意: 给定一棵根为1, n个结点的树. 有q个操作,有两种不同的操作 (1) 1 v k x : a[v] += x, a[v ' ...
- BZOJ4551[Tjoi2016&Heoi2016]树——dfs序+线段树/树链剖分+线段树
题目描述 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为1),有以下 两种操作:1. 标记操作:对某个结点打上标记(在最开始,只有结点1有标记,其他结点均 ...
随机推荐
- python并发之IO模型(二)
blocking IO (阻塞IO) 在linux中,默认情况下所有的socket都是blocking,一个典型的读操作流程大概是这样: 当用户进程调用了recvfrom这个系统调用,kernel就开 ...
- MySQL 第三天
回顾 字段类型(列类型): 数值型, 时间日期型和字符串类型 数值型: 整型和小数型(浮点型和定点型) 时间日期型: datetime, date,time,timestamp, ye ...
- Clustered and Secondary Indexes
Clustered and Secondary Indexes secondary index A type of InnoDB index that represents a subset of t ...
- 实验一中的OOP思想
子类继承父类 父类中声明了接口变量 接口AB中声明了抽象方法 ab 在子类中 可以用这样通俗的语句写程序: while (!(this.termination.shouldTerminate ...
- C# emoji 表情如何插入mssql
如何将emoji表情存入mssql 呢? 在Windows显示emoji(win7需要安装补丁) 在MAC完美支持 步骤就是将显示不出来的emoji UrlEncode=>进入MSsql 然后拿 ...
- PyQt4 进度条和日历 代码
# -*- coding: utf-8 -*- """ ------------------------------------------------- File Na ...
- __init__和__new__
一.__init__方法是什么 __init__方法通常用在初始化一个类实例的时候, class Person(object): """Silly Person" ...
- 使用Axure生成网站结构图
使用Axure设计的各网站(产品)页面,生成网站(产品)结构图.这个对于了解网站整体结构很有帮助. 需要把它生成对应结构的网站结构图. 第一步:在“主页”上面新建一个和“主页”平级的页面,命名为“网站 ...
- Hibernate Student_Course_Score设计
示例: 设计代码,实现在数据库中建student表.course表.和score表,展现三者关系 student表:id.name course表:id.name score表:id.score.st ...
- 在控制台中实现“单词竞猜”游戏 C# 猜词游戏
场景 设计规则 a) 这是一个单人玩的游戏. b) 可以分三个级别,分别是高级.中级.低级.不同级别对应的单词系列也不一样.要求一旦玩家选定了要玩的级别,应当先提示它关于此级别最高分是多少,是谁创下的 ...