HDU 6161 Big binary tree(树形DP)

本文介绍了一道关于完全二叉树的算法题,主要任务是对树进行操作包括更新节点值和查询最大路径和,并提供了详细的解题思路及AC代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题链接

Problem Description

You are given a complete binary tree with n nodes. The root node is numbered 1, and node x’s father node is ⌊x/2⌋. At the beginning, node x has a value of exactly x. We define the value of a path as the sum of all nodes it passes(including two ends, or one if the path only has one node). Now there are two kinds of operations:
1. change u x Set node u’s value as x(1≤u≤n;1≤x≤10^10)
2. query u Query the max value of all paths which passes node u.

Input

There are multiple cases.
For each case:
The first line contains two integers n,m(1≤n≤10^8,1≤m≤10^5), which represent the size of the tree and the number of operations, respectively.
Then m lines follows. Each line is an operation with syntax described above.

Output

For each query operation, output an integer in one line, indicating the max value of all paths which passes the specific node.

Sample Input

6 13
query 1
query 2
query 3
query 4
query 5
query 6
change 6 1
query 1
query 2
query 3
query 4
query 5
query 6

Sample Output

17
17
17
16
17
17
12
12
12
11
12
12

题目大意

给定一棵完全二叉树,每次询问给出一个节点的编号,问所有经过这个点的路径的最大值。路径的值被定义为路径上所有节点的数值之和。

解题思路

由于结点的数量很大,所以只能考虑用map存储信息。map < int,ll > val 中val[i]表示i号结点当前的值。map < int , ll > down 中down[i]表示从i号结点向下走可以找到的最大和,详细递推过程见代码注释。

AC代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<algorithm>
#include<cmath>
#include<vector>
#include<string>
#include<queue>
#include<list>
#include<stack>
#include<set>
#include<map>
#define ll long long
#define ull unsigned long long
#define fil(a,b) memset((a),(b),sizeof(a))
#define cl(a) fil(a,0)
#define pb push_back
#define mp make_pair
#define exp 2.7182818
#define PI 3.141592653589793238462643383279502884
#define inf 0x3f3f3f3f
#define fi first
#define se second
#define eps 1e-6
#define MOD 1000000007ll
using namespace std;


int n,m;
char in[10];
map<int,ll> val,down;
ll cal(int x)  //计算从x结点向下走可以取得的最大和
{
    if(x>n) return 0;
    if(down.count(x)) return down[x];  //如果在down中出现过,直接返回
    else  //说明以x为根的树中所有结点都没有被修改过,尽量向右走
    {
        ll res=0;
        int leftlevel=0,rightlevel=0;
        int compare=x;
        while((compare<<1)<=n)
        {
            compare<<=1;
            leftlevel++;
        }
        compare=x;
        while((compare<<1|1)<=n)
        {
            compare=(compare<<1|1);
            rightlevel++;
        }
        if(leftlevel!=rightlevel) compare=n;
        while(compare>=x)
        {
            res+=compare;
            compare>>=1;
        }   
        return res;
    }
}
ll query(int x)
{
    //初始路径设为以x为根,分别向左右两个方向走
    ll res=cal(x<<1)+cal(x<<1|1)+(val.count(x)?val[x]:x);
    ll sti=0; //记录向上走的路径上的结点之和
    int org=x;
    //答案也有可能是以x的某个祖先为根,分别向两边走的路径,所以一层一层向上递推
    while(x>>1)
    {
        sti+=val.count(x>>1)?val[x>>1]:(x>>1);
        if(x&1)//从右儿子上来
        {
            x>>=1;
            res=max(res,cal(org)+sti+cal(x<<1));
        }
        else//从左儿子上来
        {
            x>>=1;
            res=max(res,cal(org)+sti+cal(x<<1|1));
        }
    }
    return res;
}
void change(int pos,ll value)
{
    val[pos]=value;
    down[pos]=max(cal(pos<<1),cal(pos<<1|1))+val[pos];
    while(pos>>1)//改变某个点的值只会影响它的祖先
    {
        pos>>=1;
        down[pos]=max(cal(pos<<1),cal(pos<<1|1))+(val.count(pos)?val[pos]:pos);
    }
}
int main(void) 
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    int a;
    ll b;
    while(scanf("%d%d",&n,&m)!=EOF)
    {   
        val.clear();
        down.clear();
        for(int i=1;i<=m;++i)
        {
            scanf("%s",in);
            if(in[0]=='q')
            {
                scanf("%d",&a);
                printf("%I64d\n",query(a));
            }
            else
            {
                scanf("%d%I64d",&a,&b);
                change(a,b);
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值