HDU 5692 Snacks(DFS序+线段树)
Snacks
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3213 Accepted Submission(s): 743
由于零食被频繁的消耗和补充,零食机的价值v会时常发生变化。小度熊只能从编号为0的零食机出发,并且每个零食机至多经过一次。另外,小度熊会对某个零食机的零食有所偏爱,要求路线上必须有那个零食机。
为小度熊规划一个路线,使得路线上的价值总和最大。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string.h>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const double PI=acos(-1.0);
const double eps=0.0000000001;
const int INF=1e9;
const int N=+;
int head[N];
int tot;
ll a[N];
ll b[N];
int n,m;
struct NOde{
int l,r;
ll val;
ll lazy;
}tree[N*];
struct node{
int to,next;
}edge[N<<];
int L[N],R[N];
int time;
ll dis[N];
void init(){
memset(head,-,sizeof(head));
tot=;
time=;
}
void add(int u,int v){
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
}
void DFS(int x,int fa){
L[x]=++time;
b[time]=x;
for(int i=head[x];i!=-;i=edge[i].next){
int v=edge[i].to;
if(v==fa)continue;
a[v]=a[x]+a[v];
DFS(v,x);
}
R[x]=time;
}
void pushdown(int pos){
if(tree[pos].lazy){
tree[pos<<].val+=tree[pos].lazy;
tree[pos<<|].val+=tree[pos].lazy;
tree[pos<<].lazy+=tree[pos].lazy;
tree[pos<<|].lazy+=tree[pos].lazy;
tree[pos].lazy=; }
}
void build(int left,int right,int pos){
// cout<<55<<endl;
tree[pos].l=left;
tree[pos].r=right;
tree[pos].lazy=;
int mid=tree[pos].l+tree[pos].r>>;
if(tree[pos].l==tree[pos].r){
tree[pos].val=a[b[left]];
return ;
}
build(left,mid,pos<<);
build(mid+,right,pos<<|);
tree[pos].val=max(tree[pos<<].val,tree[pos<<|].val); }
void update(int left,int right,int pos,ll x){
if(tree[pos].l==left&&tree[pos].r==right){
tree[pos].lazy+=x;
tree[pos].val+=x;
return ;
}
pushdown(pos);
int mid=(tree[pos].l+tree[pos].r)>>;
if(mid>=right)update(left,right,pos<<,x);
else if(mid<left)update(left,right,pos<<|,x);
else{
update(left,mid,pos<<,x);
update(mid+,right,pos<<|,x);
}
tree[pos].val=max(tree[pos<<].val,tree[pos<<|].val);
}
ll query(int left,int right,int pos){
if(tree[pos].l==left&&tree[pos].r==right){
return tree[pos].val;
}
pushdown(pos);
int mid=(tree[pos].l+tree[pos].r)>>;
if(mid>=right){
return query(left,right,pos<<);
}
else if(left>mid){
return query(left,right,pos<<|);
}
else{
return max(query(left,mid,pos<<),query(mid+,right,pos<<|));
}
}
int main(){
int t;
scanf("%d",&t);
int Case=;
while(t--){
init();
scanf("%d%d",&n,&m);
int u,v;
for(int i=;i<n;i++){
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
for(int i=;i<n;i++){
scanf("%I64d",&a[i]);
dis[i]=a[i];
}
DFS(,-);
build(,time,);
int flag;
int x;
ll y;
printf("Case #%d:\n",Case++);
while(m--){
scanf("%d",&flag);
if(flag==){
scanf("%d%I64d",&x,&y);
update(L[x],R[x],,(ll)y-dis[x]);
dis[x]=y;
}
else{
scanf("%d",&x);
ll ans=query(L[x],R[x],);
printf("%I64d\n",ans);
} } }
}
HDU 5692 Snacks(DFS序+线段树)的更多相关文章
- HDU.5692 Snacks ( DFS序 线段树维护最大值 )
HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...
- hdu 5692 Snacks(dfs时间戳+线段树)
Snacks Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- HDU5692 Snacks DFS序 线段树
去博客园看该题解 题目 HDU5692 Snacks Problem Description 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的 ...
- hdu-5692 Snacks(dfs序+线段树)
题目链接: Snacks Problem Description 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的 ...
- Assign the task HDU - 3974(dfs序+线段树)
There is a company that has N employees(numbered from 1 to N),every employee in the company has a im ...
- HDU 4366 Successor( DFS序+ 线段树 )
Successor Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- 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序+线段树了.而对于第二个操作,考虑再维护一 ...
随机推荐
- 解决Android弹出软键盘导致的问题
一.当Activity启动后EditText直接获取了焦点,此时软键盘会自动弹出,这种体验并不是很好,因此要做的Activity启动不自动弹出软键盘,只需要在Manifest中对应的Activity添 ...
- Count the consecutive zero bits (trailing) on the right with multiply and lookup
我在网上看到了一点神奇的代码,用来计算一个数字末尾连续零的个数. 刚好我在优化一个I2C读写函数(只写入I2C特定bit),觉得这个很有用.经过尝试,确实没问题. 下面我隆重介绍一下: Count t ...
- Centos6.7 ELK日志系统部署
Centos6.7 ELK日志系统部署 原文地址:http://www.cnblogs.com/caoguo/p/4991602.html 一. 环境 elk服务器:192.168.55.134 lo ...
- 微信小程序音频长度获取的问题
小程序推荐使用wx.createInnerAudioContext()创建的innerAudioContext,我们也通过这个接口创建音频.音频的长度可以通过属性获取: 但是,给innerAudioC ...
- -moz、-ms、-webkit
1.-moz代表firefox浏览器私有属性 2.-ms代表IE浏览器私有属性 3.-webkit代表safari.chrome私有属性 需要设置这个的样式: transform,border-rad ...
- CAD取Excel表格(com接口)
1 2 3 4 5 6 7 8 9 10 11 12 MxDrawResbuf ret = (MxDrawResbuf)axMxDrawX1.Call("ExApp_GetExcel&quo ...
- 一步一步教你用IntelliJ IDEA 搭建SSM框架(2)——配置mybatis-geneator
我们要搭建整个SSM框架,所以要继续上篇文章没有完成的工作,下面配置mybatis-geneator,自动生成mybatis代码. 在上篇文章中的pom.xml的配置文件中已经加了mybatis-ge ...
- eclipse常用设置之自动格式化
Eclipse 保存文件时自动格式化代码 很多同学不知道Eclipse有个很有用的功能,就是自动格式源代码的功能,一般大家都是直接Ctrl+Shift+F手动格式化,多浪费时间. 其实Eclips ...
- 如何区分null和undefined
null和undefined是两种数据类型, 如果硬要区分的话. null是一种类型, 赋值变量为null型. 未定义的变量, 即为undefined. var a = null a // null ...
- 使用命令行打开vscode
今天看到一个博客,直接使用code . 就可以打开vscode