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序+线段树了.而对于第二个操作,考虑再维护一 ...
随机推荐
- Core 项目下使用SQl语句
public static DataTable ExeQuery(string connectionString, CommandType cmdType, string cmdText) { usi ...
- [Windows Server 2008] 安装IIS7.5及FTP
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:安装IISII ...
- Android中ViewPager动态创建的ImageView铺满屏幕
ImageView imageView=new ImageView(context); imageView.setScaleType(ScaleType.FIT_XY);//铺满屏幕
- HDU_2212_水
DFS Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- java 类名.this
类名为this的限定词. 相对于内部类:有多个this: 1.内部类本身的this: 2.内部类的环境类的this: 类名.this,就是为了对这些this指针的指向做出限定. 区别于类名.class ...
- js的一些老司机写法
//取整 parseInt(a,10); //Before Math.floor(a); //Before a>>0; //Before ~~a; //After a|0; //After ...
- C语言比较好的风格梳理
errno int err; tb = malloc(sizeof(struct xtracer_table)); if (!tb) { err = errno; fprintf(stderr, &q ...
- POJ 1088 滑雪(简单的记忆化dp)
题目 又一道可以称之为dp的题目,虽然看了别人的代码,但是我的代码写的还是很挫,,,,,, //看了题解做的简单的记忆化dp #include<stdio.h> #include<a ...
- fork()调用使子进程先于父进程被调度
由于内核使用写时复制机制,fork之后父子进程是共享页表描述符的,如果让父进程先执行,那么有很大几率父进程会修改共享页表指向的数据,那么内核此时必须给父进程分配并复制新的页表供父进程修改使用,那么如果 ...
- vue轮播插件vue-awesome-swiper
https://surmon-china.github.io/vue-awesome-swiper/ 第一步安装 npm install vue-awesome-swiper --save 第二部在m ...