HDU 6161 Big binary tree(树形DP)

原题链接

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
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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);
            }
        }
    }
}

你可能感兴趣的:(HDU 6161 Big binary tree(树形DP))