HDU5692 线段树+dfs序


Snacks

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1517    Accepted Submission(s): 368


Problem Description
百度科技园内有 n 个零食机,零食机之间通过 n1 条路相互连通。每个零食机都有一个值 v ,表示为小度熊提供零食的价值。

由于零食被频繁的消耗和补充,零食机的价值 v 会时常发生变化。小度熊只能从编号为0的零食机出发,并且每个零食机至多经过一次。另外,小度熊会对某个零食机的零食有所偏爱,要求路线上必须有那个零食机。

为小度熊规划一个路线,使得路线上的价值总和最大。
 

Input
输入数据第一行是一个整数 T(T10) ,表示有 T 组测试数据。

对于每组数据,包含两个整数 n,m(1n,m100000) ,表示有 n 个零食机, m 次操作。

接下来 n1 行,每行两个整数 x y(0x,y<n) ,表示编号为 x 的零食机与编号为 y 的零食机相连。

接下来一行由 n 个数组成,表示从编号为0到编号为 n1 的零食机的初始价值 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
 


题目大意:给你一棵树,每一个点一个权值,两种操作1.修改某一个点的权值2.给定一个点x,求一条路径经过x点的最大权值为多少。

解题思路:构建一颗线段树,对于x点来说,所有经过x点的路径,终点都设在x的子树里面,这样就x就可以影响到他们。利用dfs遍历这棵树的时候给每一个点编号,然后构建这可树。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <stdlib.h>
#define lson (id<<1)
#define rson ((id<<1)|1)
#define mid ((l+r)>>1)
using namespace std;
typedef long long ll;
const int maxn = 100010;
int l[maxn], r[maxn];
ll cost[maxn], a[maxn];
int n, m, cnt;
vector<int> G[maxn];

struct nod{
	ll sum,lazy;
}tree[maxn * 5];

void dfs(int u, int f, ll val) {
    l[u] = ++cnt;
    for(int i = 0; i < (int)G[u].size(); ++i) {
        int v = G[u][i];
        if(v == f) continue;
        dfs(v, u, cost[v] + val);
    }
    r[u] = cnt;
    a[l[u]] = val;
}
void push_up(int id)
{
	tree[id].sum=max(tree[lson].sum, tree[rson].sum);
	return;
}

void build_tree(int id,int l,int r)
{
	if (l==r)
	{
		tree[id].sum = a[l];
		tree[id].lazy=0;
		return;
	}
	build_tree(lson,l,mid);
	build_tree(rson,mid+1,r);
	push_up(id);
	tree[id].lazy=0;
	return;
}

void push_down(int id,int l,int r)
{
	tree[lson].sum+=tree[id].lazy;
	tree[lson].lazy+=tree[id].lazy;
	tree[rson].sum+=tree[id].lazy;
	tree[rson].lazy+=tree[id].lazy;
	tree[id].lazy=0;
	return;
}

void ins(int id,int l,int r,int ql,int qr,ll tt)
{
	if (ql<=l && r<=qr)
	{
		tree[id].sum+=tt;
		tree[id].lazy+=tt;
		return;
	}
	if (tree[id].lazy) push_down(id,l,r);
	if (ql<=mid)
		ins(lson,l,mid,ql,qr,tt);
	if (mid+1<=qr)
		ins(rson,mid+1,r,ql,qr,tt);
	push_up(id);
	return;
}

ll query(int id,int l,int r,int ql,int qr)
{
	if (ql<=l && r<=qr)
	{
		return tree[id].sum;
	}
	if (tree[id].lazy)
		push_down(id,l,r);
	ll sum= -99999999999999999;
	if (ql<=mid)
		sum = max(query(lson,l,mid,ql,qr), sum);
	if (mid+1<=qr)
		sum = max(sum, query(rson,mid+1,r,ql,qr));
	return sum;
}

int main()
{
	int T, u, v, p;
	scanf("%d", &T);
	for(int t = 1; t <= T; ++t) {
        memset(a, 0, sizeof(a));
        scanf("%d%d", &n, &m);
        for(int i = 1; i <= n; ++i) {
            G[i].clear();
        }
        for(int i = 1; i < n; ++i) {
            scanf("%d%d", &u, &v);
            u++;
            v++;
            G[u].push_back(v);
            G[v].push_back(u);
        }
        for(int i = 1; i <= n; ++i) {
            scanf("%I64d", &cost[i]);
        }
        cnt = 0;
        dfs(1, 0, cost[1]);
        build_tree(1, 1, n);
        printf("Case #%d:\n", t);
        while(m--) {
            scanf("%d", &p);
            if(p == 0) {
                scanf("%d%d", &u, &v);
                u++;
                ins(1, 1, n, l[u], r[u], (ll)(v - cost[u]));
                cost[u] = v;
            } else {
                scanf("%d", &u);
                u++;
                printf("%I64d\n", query(1, 1, n, l[u], r[u]));
            }
        }
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值