HDU 6161 Big binary tree(树形DP)
来源:互联网 发布:美国传播学专业知乎 编辑:程序博客网 时间:2024/05/16 04:01
原题链接
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 1000000007llusing 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)
- hdu 6161 Big binary tree
- HDU 6161 Big binary tree
- hdu 6161 Big binary tree
- hdu 6161--Big binary tree(思维--压缩空间)
- hdu 6161--Big binary tree(思维--压缩空间)
- 2017多校九 01题 HDU6161 Big binary tree 树形dp+hash
- hdu 3534 Tree(树形dp)
- hdu 5379 Mahjong tree(树形dp)
- hdu 6035 Colorful Tree(树形DP)
- HDU 6035 Colorful Tree (树形dp)
- HDU 6035 Colorful Tree(树形dp)
- HDU 6035 Colorful Tree(树形DP)
- Ural 1018 Binary Apple Tree (树形DP)
- Ural 1018 Binary Apple Tree(树形dp)
- ural 1018 Binary Apple Tree(树形dp | 经典)
- URAL 1018 Binary Apple Tree(树形dp)
- ural 1018 Binary Apple Tree(树形DP)
- No identifier specified for entity两种解决方式
- LA -- 3695 Distant Galaxy 【思维】
- DB和DBMS
- 频道管理(第二步)
- 简单的js实现放大镜效果
- HDU 6161 Big binary tree(树形DP)
- tensorflow学习笔记(六):cnn过程可视化
- iOS关于模块化开发解决方案(纯干货)
- 常考排序算法
- android ril 深度学习
- UVa 11148 Hyper Prefix Sets (Trie)
- 毕业以后上百万大学生去哪了
- 计算机学习网站汇总
- 如何批量修改Word文档中的表格样式和题注格式?