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. ligerUI---ligerForm中下拉框使用

    写在前面: 最近项目的前框框架用的是ligerUI,一开始我是拒绝的,因为貌似ligerUI很少有人用,我真的很想问我们team的斌哥哥为什么要用ligerUI来做前端框架?????(啊哈哈哈,用什么 ...

  2. redis基础二

    前面已经学习了redis的基本的命令行操作和数据类型,下面开始redis一些有趣的功能. 订阅和发布机制 定义:发布者相当于电台,订阅者相当于客户端,客户端发到频道的消息,将会被推送到所有订阅此频道的 ...

  3. 如何用VSCode愉快的写Python

    在学习Python的过程中,一直没有找到比较趁手的第三方编辑器,用的最多的还是Python自带的编辑器.由于本人用惯了宇宙第一IDE(Visual Studio),所以当Visual Studio C ...

  4. S7-200以太网通信

    一.西门子网络系统 二.s7-200通过以太网模块接入以太网 三.S7-200可以接入的以太网系统 四.S7-200以太网通讯实验 五.实验硬件系统组成 六.S7-200作为服务器的配置 1.进入以太 ...

  5. ArcGIS API for JavaScript 中的数据类型【vs】GPServer的数据类型

    熟悉GPServer的同学肯定知道,GPServer在10.1的ArcMap后需要执行成功一次才能发布. 发布GPServer,可以是ArcMap的工具箱的工具,也可以是自己写的模型. 不管是ArcM ...

  6. 关于PLC

    学电气的一方面是单片机,一方面是PLC,,,,常常看到说选择比努力更重要,,单片机都很熟悉了,我就来介绍一下PLC..... 然后呢我先吹吹牛,,,目的是让大家相信我介绍的PLC绝对是亲身体验.... ...

  7. & 与 kill -3

    mysqld_safe --defaults-file=/app/3307/my.cnf 2>&1 1>/dev/null & 将mysqld服务进程放入后台后, 因为忘记 ...

  8. Struts2-整理笔记(五)拦截器、拦截器配置

    拦截器(Interceptor) 拦截器是Struts2最强大的特性之一,它是一种可以让用户在Action执行之前和Result执行之后进行一些功能处理的机制. 拦截器的优点 简化了Action的实现 ...

  9. 中文代码示例之Angular入门教程尝试

    原址: https://zhuanlan.zhihu.com/p/30853705 原文: 中文代码示例教程之Angular尝试 为了检验中文命名在Angular中的支持程度, 把Angular官方入 ...

  10. Xamarin.Forms (Android制作启动画面)

    http://blog.csdn.net/zapzqc/article/details/38496117     Xamarin.Forms 在启动的时候相当慢,必须添加一个启动界面,步骤如下: 1. ...