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序+线段树了.而对于第二个操作,考虑再维护一 ...
随机推荐
- Moto P30(XT1943-1) 免解锁BL 免rec 保留数据 Magisk Xposed ROOT 救砖 ZUI 4.0.374
>>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...
- 集合Set、List、Map的遍历方法
package com.shellway.javase; import java.util.ArrayList; import java.util.Collection; import java.ut ...
- 【译】x86程序员手册14-5.1段转换
5.1 Segment Translation 段转换 Figure 5-2 shows in more detail how the processor converts a logical add ...
- 详谈java集合框架
1.为什么使用集合框架 当我们并不知道程序运行时会需要多少对象,或者需要更复杂方式存储对象——可以使用Java集合框架 2.Java集合框架包含的内容 接口:(父类)Collection接口下包含Li ...
- 15个最受欢迎的Python开源框架(转)
原文地址:http://blog.jobbole.com/72306/ Django: Python Web应用开发框架 Django 应该是最出名的Python框架,GAE甚至Erlang都有框架受 ...
- (转)Hibernate关联映射——一对多(多对一)
http://blog.csdn.net/yerenyuan_pku/article/details/70152173 Hibernate关联映射——一对多(多对一) 我们以客户(Customer)与 ...
- codeforces_731D_(前缀和)(树状数组)
D. 80-th Level Archeology time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- python编写webservice接口
1.pip install suds-jurko 2.pip install client #coding=utf-8 from suds.client import Client class Web ...
- Self-Attetion
四.self-attention 1.是什么? attention机制通常用在encode与decode之间,但是self-attention则是输入序列与输出序列相同,寻找序列内部元素的关系即 K= ...
- 【VIP视频网站项目三】项目框架搭建、项目路由配置、数据库表结构设计
一.项目路由的设计 目前项目代码已经全部开源:项目地址:https://github.com/xiugangzhang/vip.github.io 视频网站前台页面路由设计 路由 请求方法 模板 作用 ...