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. 【批处理】shift用法举例

    @echo off set sum=0 call :sub sum 1 2 3 4 echo sum=%sum% pause :sub set /a %1=%1+%2 shift /2 if not ...

  2. PE格式第三讲扩展,VA,RVA,FA(RAW),模块地址的概念

    PE格式第三讲扩展,VA,RVA,FA的概念 作者:IBinary出处:http://www.cnblogs.com/iBinary/版权所有,欢迎保留原文链接进行转载:) 一丶VA概念 VA (vi ...

  3. Python学习(二):函数入门

    1.函数代码格式: def 函数名(): 函数内容 执行函数:函数名() 2.代码举例: #!/usr/bin/env python #coding=utf-8 #定义函数 def Func1(): ...

  4. uptime 命令详解

    作用: 打印系统总共运行了多长时间和系统的平均负载. uptime 命令可以显示的信息依次为:  现在时间, 系统已经运行时间, 目前登录用户个数, 系统1,5,15 分钟内的平均负载 实例:  up ...

  5. 限制ssh远程登陆

    超过十次,就添加到hosts.deny里面去 #!/bin/bash date=`date +%Y%m%d` file="/var/log/secure" max=10 if [[ ...

  6. useradd 命令 及 相关文件解读

    翻译自 man useradd 名称: 创建新用户或更新默认的新用户信息 快照:useradd -D 描述: 在不加-D参数调用时,useradd命令使用指定的参数和系统的默认值来创建新账户. 取决于 ...

  7. Tomcat 组件介绍

    用了好长时间tomcat,但是其实自己只是反复听了这个名字,对Tomcat并不了解 1.Tomcat组件 Catalina Coyote Jasper Cluster 2.组件介绍 Tomcat Co ...

  8. KandQ:单例模式的七种写法及其相关问题解析

    设计模式中的单例模式可以有7种写法,这7种写法有各自的优点和缺点: 代码示例(java)及其分析如下: 一.懒汉式 public class Singleton { private static Si ...

  9. JavaScript的DOM编程--09--节点的替换

    节点的替换: 1). replaceChild(): 把一个给定父元素里的一个子节点替换为另外一个子节点 var reference = element.replaceChild(newChild,o ...

  10. C#中DataGridView动态添加行及添加列的方法

    http://www.jb51.net/article/72259.htm Datagridview添加列: ? 1 2 3 4 5 DataGridViewTextBoxColumn acCode ...