Bstar2016的一道水题T3
2016-05-21 21:22
323 查看
Snacks
Problem Description
百度科技园内有n个零食机,零食机之间通过n−1条路相互连通。每个零食机都有一个值v,表示为小度熊提供零食的价值。
由于零食被频繁的消耗和补充,零食机的价值v会时常发生变化。小度熊只能从编号为0的零食机出发,并且每个零食机至多经过一次。另外,小度熊会对某个零食机的零食有所偏爱,要求路线上必须有那个零食机。
为小度熊规划一个路线,使得路线上的价值总和最大。
Solution
一道水题还想了蛮久==
首先dfs处理出dfs序以及每个点到根结点的距离。
然后建一棵记录距离的线段树。
然后就好了
记得会爆int
[源代码]
竟然在C++里跑第一==
一定是交的人太少了==
Problem Description
百度科技园内有n个零食机,零食机之间通过n−1条路相互连通。每个零食机都有一个值v,表示为小度熊提供零食的价值。
由于零食被频繁的消耗和补充,零食机的价值v会时常发生变化。小度熊只能从编号为0的零食机出发,并且每个零食机至多经过一次。另外,小度熊会对某个零食机的零食有所偏爱,要求路线上必须有那个零食机。
为小度熊规划一个路线,使得路线上的价值总和最大。
Solution
一道水题还想了蛮久==
首先dfs处理出dfs序以及每个点到根结点的距离。
然后建一棵记录距离的线段树。
然后就好了
记得会爆int
[源代码]
#include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> #include<vector> #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; typedef long long ll; const int M=1e5+5; int A[M],tid[M],nxt[M],dfs_clock,tst,_; ll d[M]; vector<int>G[M]; struct Segment_Tree{ struct node{ int l,r; ll mx,add; }tree[M<<2]; inline void up(int p){ tree[p].mx=max(tree[p<<1].mx,tree[p<<1|1].mx); } inline void down(int p){ if(!tree[p].add)return; tree[p<<1].add+=tree[p].add;tree[p<<1|1].add+=tree[p].add; tree[p<<1].mx+=tree[p].add,tree[p<<1|1].mx+=tree[p].add; tree[p].add=0; } void build(int l,int r,int p){ tree[p].l=l,tree[p].r=r,tree[p].mx=tree[p].add=0; if(l==r){ tree[p].mx=d[l]; return; } int mid=l+r>>1; build(l,mid,p<<1); build(mid+1,r,p<<1|1); up(p); } void update(int l,int r,int x,int p){ if(tree[p].l==l&&tree[p].r==r){ tree[p].add+=x,tree[p].mx+=x; return; } down(p); int mid=tree[p].l+tree[p].r>>1; if(l>mid)update(l,r,x,p<<1|1); else if(r<=mid)update(l,r,x,p<<1); else update(l,mid,x,p<<1),update(mid+1,r,x,p<<1|1); up(p); } ll query(int l,int r,int p){ if(tree[p].l==l&&tree[p].r==r)return tree[p].mx; down(p); int mid=tree[p].l+tree[p].r>>1; if(l>mid)return query(l,r,p<<1|1); if(r<=mid)return query(l,r,p<<1); return max(query(l,mid,p<<1),query(mid+1,r,p<<1|1)); } }T; void dfs(int v,int f,ll dep){ tid[v]=++dfs_clock,d[tid[v]]=dep; for(int i=0;i<G[v].size();++i) if(G[v][i]^f)dfs(G[v][i],v,dep+A[G[v][i]]); nxt[v]=dfs_clock; } void gao(){ int n,q;cin>>n>>q; dfs_clock=0; memset(d,0,sizeof(d)); for(int i=0;i^n;++i)G[i].clear(); for(int i=1,a,b;i^n;++i)scanf("%d %d",&a,&b),G[a].push_back(b),G[b].push_back(a); for(int i=0;i^n;++i)scanf("%d",&A[i]); printf("Case #%d:\n",++tst); dfs(0,0,A[0]); T.build(1,n,1); for(int opt,x,k;q--;){ scanf("%d %d",&opt,&x); switch(opt){ case(0):scanf("%d",&k);T.update(tid[x],nxt[x],k-A[x],1);A[x]=k;break; case(1):printf("%I64d\n",T.query(tid[x],nxt[x],1));break; } } } int main(){for(cin>>_;_--;)gao();}
竟然在C++里跑第一==
一定是交的人太少了==
相关文章推荐
- 2015 Multi-University Training Contest 6 Solutions
- hdu 5124 BestCoder #20 1002 lines 解题报告
- hdu 5105 Math Problem bestcoder#18 1002 求最大值
- BESTCODER 68
- 【BestCoder Round #16】
- BestCoder55 hdu5432 二分
- hduacm 5464 Clarke and problem 题解
- hduacm 5433 Xiao Ming climbing题解
- ACM hdu5432 Pyramid Split
- BestCoder Round #61 (div.2)
- HDU 4907 Task schedule(BestCoder Round #3 )
- BestCoder 20160416 Sequence大数运算
- bc_#6
- hdu-5571 tree
- 2016"百度之星" - 资格赛(Astar Round1)
- BestCoder Round #63 (div.2)
- BestCoder Round #53 (div.2) C.Rikka with Graph II(dfs)
- HDU5601:N*M bulbs
- HDU5600:N bulbs
- HDU5595:GTW likes math