2017 Multi-University Training Contest - Team 9 1001&&HDU 6161 Big binary tree【树形dp+hash】
Big binary tree
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 597 Accepted Submission(s): 207
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
Source
2017 Multi-University Training Contest - Team 9
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6161
分析:考虑dp,f(x)表示从点x开始向下走得到的最大的点权和,查询直接从x开始向上走更新答案即可。 考虑快速算 f(x) 对于子树内没有被修改过的点的 f(x) 可以快速分类讨论算出,而不满足本条件的点只有 O(mlogm) 个,在hash上dp即可。
下面给出AC代码:
1 #include<bits/stdc++.h>
2 using namespace std;
3 #define pb push_back
4 #define mkp make_pair
5 #define fi first
6 #define se second
7 #define ll long long
8 #define M 1000000007
9 #define all(a) a.begin(), a.end()
10
11 int n, m;
12 char s[20];
13 map<int, ll> mp;
14 map<int, int> amp;
15
16 inline ll askmax(int u){
17 if(u > n) return 0;
18 if(mp.count(u)) return mp[u];
19 else{
20 int l = u, r = u;
21 while(l * 2 <= n){
22 l <<= 1;
23 r = (r << 1) | 1;
24 }
25 r = min(r, n);
26 ll res = 0;
27 while(r >= u) res += r, r >>= 1;
28 return res;
29 }
30 }
31
32 inline int ask(int x){
33 return amp.count(x) ? amp[x] : x;
34 }
35
36 int main(){
37 while(~scanf("%d%d", &n, &m)){
38 mp.clear();
39 amp.clear();
40 while(m--){
41 int x, v;
42 scanf("%s", s);
43 if(s[0] == 'c'){
44 scanf("%d%d", &x, &v);
45 amp[x] = v;
46 for(; x; x >>= 1)
47 mp[x] = max(askmax(x << 1), askmax((x << 1) | 1)) + ask(x);
48 }else{
49 scanf("%d", &x);
50 int px = x;
51 ll res = 0, now = 0;
52 for(; x >> 1;){
53 bool k = ~x & 1; x >>= 1;
54 now += ask(x);
55 ll tmp = askmax(x << 1 | k);
56 if(now + tmp > res) res = now + tmp;
57 }
58 res += askmax(px);
59 res = max(res, askmax(px << 1) + askmax(px << 1 | 1) + ask(px));
60 printf("%lldn", res);
61 }
62 }
63 }
64
65 #ifndef ONLINE_JUDGE
66 system("pause");
67 #endif
68 return 0;
69 }
- [Silverlight动画]转向行为 - 对象回避
- [mobile开发碎碎念]手机页面上显示PDF文件
- Windows Server 2008 R2 配置AD(Active Directory)域控制器
- ios开发基础知识 - 2
- ios开发基础知识 - 1
- 为UINavigationBar添加自定义背景
- UIScrollView无法响应touches的解决方法
- WCF技术剖析之十七:消息(Message)详解(下篇)
- UITextView自动滚动的解决方案
- MVC解决Json DataGrid返回的日期格式是/Date(20130450000365)
- Easyui 让DataGrid适应浏览器宽度
- Easyui 让Window弹出居中与最大化后居中
- WCF技术剖析之十六:数据契约的等效性和版本控制
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(13)-系统日志和异常的处理③
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- [OHIF-Viewers]医疗数字阅片-医学影像-redux-token实操(1)
- [OHIF-Viewers]医疗数字阅片-医学影像-屏蔽StudyList病例列表
- Ant Design for Vue的Table组件一列显示多个参数
- 【React】React-router的使用记录
- Blazor带我重玩前端(四)
- Android绘制系统简介
- E: Sub-process /usr/bin/dpkg returned an error code (1) 解决方案
- Linux 如何使用包管理器安装 Node.js
- CSS画图
- R语言聚类算法的应用实例
- Python时间序列选择波动率预测指数收益算法分析案例
- Linux 常用系统工作命令-date
- R使用LASSO回归预测股票收益
- Linux 常用系统工作命令-reboot、poweroff、wget
- Node.js + Socket.io 实现一对一即时聊天