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
分析:考虑dp,f(x)表示从点x开始向下走得到的最大的点权和,查询直接从x开始向上走更新答案即可。
考虑快速算 f(x) 对于子树内没有被修改过的点的 f(x) 可以快速分类讨论算出,而不满足本条件的点只有 O(mlogm) 个,在hash上dp即可。
下面给出AC代码:
 #include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mkp make_pair
#define fi first
#define se second
#define ll long long
#define M 1000000007
#define all(a) a.begin(), a.end() int n, m;
char s[];
map<int, ll> mp;
map<int, int> amp; inline ll askmax(int u){
if(u > n) return ;
if(mp.count(u)) return mp[u];
else{
int l = u, r = u;
while(l * <= n){
l <<= ;
r = (r << ) | ;
}
r = min(r, n);
ll res = ;
while(r >= u) res += r, r >>= ;
return res;
}
} inline int ask(int x){
return amp.count(x) ? amp[x] : x;
} int main(){
while(~scanf("%d%d", &n, &m)){
mp.clear();
amp.clear();
while(m--){
int x, v;
scanf("%s", s);
if(s[] == 'c'){
scanf("%d%d", &x, &v);
amp[x] = v;
for(; x; x >>= )
mp[x] = max(askmax(x << ), askmax((x << ) | )) + ask(x);
}else{
scanf("%d", &x);
int px = x;
ll res = , now = ;
for(; x >> ;){
bool k = ~x & ; x >>= ;
now += ask(x);
ll tmp = askmax(x << | k);
if(now + tmp > res) res = now + tmp;
}
res += askmax(px);
res = max(res, askmax(px << ) + askmax(px << | ) + ask(px));
printf("%lld\n", res);
}
}
} #ifndef ONLINE_JUDGE
system("pause");
#endif
return ;
}

2017 Multi-University Training Contest - Team 9 1001&&HDU 6161 Big binary tree【树形dp+hash】的更多相关文章

  1. 2017 Multi-University Training Contest - Team 1 1001&&HDU 6033 Add More Zero【签到题,数学,水】

    Add More Zero Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  2. 2017 Multi-University Training Contest - Team 9 1005&&HDU 6165 FFF at Valentine【强联通缩点+拓扑排序】

    FFF at Valentine Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  3. 2017 Multi-University Training Contest - Team 9 1004&&HDU 6164 Dying Light【数学+模拟】

    Dying Light Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  4. 2017 Multi-University Training Contest - Team 9 1003&&HDU 6163 CSGO【计算几何】

    CSGO Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  5. 2017 Multi-University Training Contest - Team 9 1002&&HDU 6162 Ch’s gift【树链部分+线段树】

    Ch’s gift Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  6. 2017 Multi-University Training Contest - Team 1 1003&&HDU 6035 Colorful Tree【树形dp】

    Colorful Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  7. 2017 Multi-University Training Contest - Team 1 1006&&HDU 6038 Function【DFS+数论】

    Function Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  8. 2017 Multi-University Training Contest - Team 1 1002&&HDU 6034 Balala Power!【字符串,贪心+排序】

    Balala Power! Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  9. 2017 Multi-University Training Contest - Team 1 1011&&HDU 6043 KazaQ's Socks【规律题,数学,水】

    KazaQ's Socks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

随机推荐

  1. 分享一个Maven的架构,以及在Maven配置中的注意事项

    Maven作为一个十分流行的项目管理工具,在我们的项目变得逐渐复杂的时候,可以有效的降低项目管理难度. 聚合工程就是当项目到达一定规模时,十分有效的管理子项目和模块的方法. 首先介绍一下,在大型项目中 ...

  2. iptables 命令详解

    转载:http://blog.chinaunix.net/uid-26495963-id-3279216.html 一:前言 防火墙,其实说白了讲,就是用于实现Linux下访问控制的功能的,它分为硬件 ...

  3. SpringMVC处理multipart请求.

    一.简述 multipart格式的数据会将一个表单拆分为多个部分(part),每个部分对应一个输入域.在一般的表单输入域中,它所对应的部分中会放置文本型数据,但是如果上传文件的话,它所对应的部分可以是 ...

  4. 尤克里里 ukulele 单板 非kaka tom uma

    本店冲人气优惠,不搞倒闭之类的事 23寸尤克里里 单板 单板 单板 彩贝镶边演出大气 单板 单板 单板 彩贝镶边演出大气 单板 单板 单板 彩贝镶边演出大气 配件选购40元全套(加棉琴包.金属变调夹. ...

  5. 鸟哥的linux私房菜学习-(二)VMware虚拟机及linux系统安装过程

    一.安装虚拟机 1.虚拟机常用版本及注册码地址:https://pan.baidu.com/s/1dFnkBrN#list/path=%2FSoftware%20Big%2FVMware%20Work ...

  6. 洛谷 P1485 火枪打怪

    题目描述 LXL进入到了一片丛林,结果他发现有n只怪物排成一排站在他面前.LXL有一杆火枪能对付这些怪物.他知道从左至右数第i只怪物的血量是mi.现在LXL可以将一些子弹射向某个怪物.LXL可以控制他 ...

  7. DJango_生命周期

    在django中,当我们访问一个url时,会通过路由匹配进入到响应的html页面中. Django的生命周期,指的就是当用户在浏览器上输入url,到用户看到整个页面之前,django后台都做了哪些事情 ...

  8. Node.js 蚕食计划(四)—— Express + SQL Server 搭建电影网站

    前段时间在慕课网上看了 scott 大神的<node+mongodb建站攻略>课程,按照自己的思路做了一遍,发博客记录一下 一.项目介绍 这个项目是一个简单的电影网站,由首页.详情页.评论 ...

  9. 微信小程序之页面路由

    路由方式 简介 对于路由的触发方式以及页面生命周期函数如下: 路由方式 触发时机 路由前页面 路由后页面 初始化 小程序打开的第一个页面   onLoad, onSHow 打开新页面 调用 API w ...

  10. Java笔记:Java 流(Stream)、文件(File)和IO

    更新时间:2018-1-7 12:27:21 更多请查看在线文集:http://android.52fhy.com/java/index.html java.io 包几乎包含了所有操作输入.输出需要的 ...