版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Jane_JXR/article/details/76690907
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5692
分析:分析:求编号为0的零食机,必须经过编号为x零食机的路线中,价值总和的最大值。
其中有两种操作:0 x y,表示编号为x的零食机的价值变为y;
1 x,表示询问从编号为0的零食机出发,必须经过编号为x零食机的路线中,价值总和的最大值。
连通图,其中某个点的修改会影响必经过她的点,我们可以先dfs确定点的顺序以及记录修改当前点所影响的范围。每次修改的话,就相当于所影响区间的更新,这样就变成区间更新求最值问题,线段树搞一下就可以了。本题巧妙的将DFS序和线段树结合在一起。
哎!做题的时候要看清数据,把最终结果初始化为INF,而INF是int范围内的,然后就疯狂WA。。。
推荐题目:http://acm.hdu.edu.cn/showproblem.php?pid=3974
这道题跟上面那题是相同的套路,我们可以用线段树更新区间最值(把跟新顺序编号,数组记录,线段树更新有关区间,查询某个节点的值,注意这个是处理过的,要搞清楚之间的关系)。
CODE:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<math.h>
#define INF 1e18//注意数据的初始化,会爆int且有负数
#define lson l,m,root<<1
#define rson m+1,r,root<<1|1
typedef long long LL;
using namespace std;
const int maxn=1000005;
const int mod=998244353;
int head[maxn],tot,num;
int l[maxn],r[maxn];
int nd[maxn];
LL dis[maxn],a[maxn];
LL lazy[maxn<<2],sum[maxn<<2];
struct edge
{
int to,next;
} eg[maxn<<2];
void addedge(int u,int v)
{
eg[tot].to=v;
eg[tot].next=head[u];
head[u]=tot++;
}
void init()
{
tot=0,num=0;
memset(head,-1,sizeof(head));
}
void dfs(int u,int fa)
{
num++;
l[u]=num;//当前结点更新影响的左端点
nd[num]=u;//当前编号所对应零食机的编号
for(int i=head[u]; i!=-1; i=eg[i].next)
{
int v=eg[i].to;
if(v!=fa)
{
dis[v]=dis[u]+a[v];
dfs(v,u);
}
}
r[u]=num;//当前结点更新影响的右端点
}
void pushdown(int root)
{
if(lazy[root])
{
sum[root<<1]+=lazy[root];
sum[root<<1|1]+=lazy[root];
lazy[root<<1]+=lazy[root];
lazy[root<<1|1]+=lazy[root];
lazy[root]=0;
}
}
void pushup(int root)
{
sum[root]=max(sum[root<<1],sum[root<<1|1]);
}
void build(int l,int r,int root)
{
lazy[root]=0;
if(l==r)
{
sum[root]=dis[nd[l]];
return;
}
int m=(l+r)>>1;
build(lson);
build(rson);
pushup(root);
}
void update(int L,int R,int add,int l,int r,int root)
{
if(L<=l&&r<=R)
{
lazy[root]+=add;
sum[root]+=add;
return;
}
pushdown(root);
int m=(l+r)>>1;
if(L<=m)
update(L,R,add,lson);
if(R>m)
update(L,R,add,rson);
pushup(root);//!!!!!!!!!!!!!!!!!
}
LL query(int L,int R,int l,int r,int root)
{
if(L<=l&&r<=R)
{
return sum[root];
}
pushdown(root);//!!!!!!!!!!!!!!
int m=(l+r)>>1;
LL ans=-INF;
if(L<=m)
ans=max(ans,query(L,R,lson));
if(R>m)
ans=max(ans,query(L,R,rson));
return ans;
}
int main()
{
int t,T=0,n,m;
scanf("%d",&t);
while(t--)
{
init();
printf("Case #%d:\n",++T);
int x,y;
scanf("%d%d",&n,&m);
for(int i=1; i<n; i++)
{
scanf("%d%d",&x,&y);
addedge(x,y);
addedge(y,x);
}
for(int i=0; i<n; i++)
scanf("%lld",&a[i]);
dis[0]=a[0];
dfs(0,-1);
build(1,n,1);
while(m--)
{
int q,xx,yy;
scanf("%d",&q);
if(q==1)
{
scanf("%d",&xx);
LL ans=query(l[xx],r[xx],1,n,1);
printf("%lld\n",ans);
}
else
{
scanf("%d%d",&xx,&yy);
update(l[xx],r[xx],yy-a[xx],1,n,1);
a[xx]=yy;//更新当前节点的值,对下次更新有影响
}
}
}
return 0;
}
SnacksTime Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3109 Accepted Submission(s): 723
Problem Description
百度科技园内有
n个零食机,零食机之间通过
n−1条路相互连通。每个零食机都有一个值
v,表示为小度熊提供零食的价值。
由于零食被频繁的消耗和补充,零食机的价值 v会时常发生变化。小度熊只能从编号为0的零食机出发,并且每个零食机至多经过一次。另外,小度熊会对某个零食机的零食有所偏爱,要求路线上必须有那个零食机。 为小度熊规划一个路线,使得路线上的价值总和最大。
Input
输入数据第一行是一个整数
T(T≤10),表示有
T组测试数据。
对于每组数据,包含两个整数 n,m(1≤n,m≤100000),表示有 n个零食机, m次操作。 接下来 n−1行,每行两个整数 x和 y(0≤x,y<n),表示编号为 x的零食机与编号为 y的零食机相连。 接下来一行由 n个数组成,表示从编号为0到编号为 n−1的零食机的初始价值 v(|v|<100000)。 接下来 m行,有两种操作: 0 x y,表示编号为 x的零食机的价值变为 y; 1 x,表示询问从编号为0的零食机出发,必须经过编号为 x零食机的路线中,价值总和的最大值。 本题可能栈溢出,辛苦同学们提交语言选择c++,并在代码的第一行加上: `#pragma comment(linker, "/STACK:1024000000,1024000000") `
Output
对于每组数据,首先输出一行”Case #?:”,在问号处应填入当前数据的组数,组数从1开始计算。
对于每次询问,输出从编号为0的零食机出发,必须经过编号为 x零食机的路线中,价值总和的最大值。
Sample Input
1 6 5 0 1 1 2 0 3 3 4 5 3 7 -5 100 20 -5 -7 1 1 1 3 0 2 -1 1 1 1 5
Sample Output
Case #1: 102 27 2 20
Source
|