多校9 1001 hdu 6161 Big binary tree

题意

有一个完全二叉树。编号i的点值是i,操作1是修改一个点的值为x,操作2是查询经过点u的所有路径的路径和最大值。105个点,108次操作。

题解

用map储存修改过的点的值val,和dp[i],表示i子树的最大路径和。

查询就是考虑两种情况,经过u点的两个孩子和经过它的一个孩子再经过它父亲,需要边走到根节点边更新答案。

代码

#include <cstdio>
#include <algorithm>
#include <map>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
const ll mod=1000000007;
const int N=201000;
map<int,ll>dp,val;
int n,m;
char o[10];
ll get(int u){
return val.count(u)?val[u]:u;
}
ll cal(int u){
if(!u||u>n)return 0;
if(dp.count(u))return dp[u];
int v,ls=0,rs=0;
for(v=u;v<=n;++ls,v<<=1);
for(v=u;v<=n;++rs,v=v<<1|1);
if(ls!=rs) v=n;
else v>>=1;
ll ans=0;
for(;v>=u;ans+=v,v>>=1);
return ans;
}
void update(int u,ll x){
val[u]=x;
while(u){
dp[u]=max(cal(u<<1),cal(u<<1|1))+get(u);
u>>=1;
}
}
ll query(int u){
ll ans=get(u)+cal(u<<1)+cal(u<<1|1);
ll tot=cal(u);
while(u){
ans=max(ans,tot+cal(u^1)+get(u>>1));
u>>=1;tot+=get(u);
}
return ans;
}
int main() {
while(~scanf("%d%d",&n,&m)){
dp.clear();val.clear();//又忘记了。。
while(m--){
int u;ll x;
scanf("%s%d",o,&u);
if(o[0]=='q'){
printf("%lld\n",query(u));
}else{
scanf("%lld",&x);
update(u,x);
}
}
}
return 0;
}

【hdu 6161】Big binary tree(二叉树、dp)的更多相关文章

  1. HDU 6161.Big binary tree 二叉树

    Big binary tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  2. 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)T ...

  3. 2017多校第9场 HDU 6161 Big binary tree 思维,类似字典树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6161 题意: 题目是给一棵完全二叉树,从上到下从左到右给每个节点标号,每个点有权值,初始权值为其标号, ...

  4. Leetcode 110 Balanced Binary Tree 二叉树

    判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...

  5. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

  6. [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  7. [LeetCode] 543. Diameter of Binary Tree 二叉树的直径

    Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...

  8. [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  9. [LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  10. [LeetCode] Minimum Depth of Binary Tree 二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

随机推荐

  1. 广州商学院16级软工一班&二班-第二次作业成绩

    作业地址 https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2097 https://edu.cnblogs.com/campus/gzc ...

  2. Vladik and Complicated Book CodeForces - 811B (思维实现)

    Vladik had started reading a complicated book about algorithms containing n pages. To improve unders ...

  3. ajax相关问题

    1.contentType和dataType contentType 主要设置你发送给服务器的数据格式 dataType设置你收到服务器数据的格式(如text,json等),最常用的为json. 2. ...

  4. PHP中多个文件包含的问题 (二)

    首先php中有常用的两种方法将文件包含:include和require,而include_once和require_once无非就是升级版而已,这里就不阐述他们的区别,我只提一下我遇到的问题: 先看一 ...

  5. 解决mysql1336

    1.mysql字符集与插入数据字符集不匹配 USE 数据库名称SHOW VARIABLES LIKE 'character%'SET character_set_server=utf8;SET cha ...

  6. HTTP之referrer

    我们知道,在页面引入图片.JS 等资源,或者从一个页面跳到另一个页面,都会产生新的 HTTP 请求,浏览器一般都会给这些请求头加上表示来源的 Referrer 字段.Referrer 在分析用户来源时 ...

  7. java lang(ClassLoader)

    一.什么是ClassLoader? 大家都知道,当我们写好一个Java程序之后,不是管是CS还是BS应用,都是由若干个.class文件组织而成的一个完整的Java应用程序,当程序在运行时,即会调用该程 ...

  8. Linux bc 命令简单学习

    1. bash里面能够实现比较简单的四则运算 echo $((*)) 注意是 双括号+ $ 地址符号. 2. 但是比较复杂的 可能就难以为继了 比如不支持精度 3. 所以这里面需要使用 bc 命令来执 ...

  9. laravel实现批量添加数据

    在使用laravel eloquent进行数据库操作的时候惊讶的发现这货居然不支持批量添加,看到网上很多人在循环里进行数据库插入操作来实现批量添加,我想说这样做是很损失性能滴!好在框架的DB门面里的i ...

  10. servlet ServletContext

    一.Servlet简介 1.什么是Servlet Servlet 运行在服务端的Java小程序,是sun公司提供一套规范(接口),用来处理客户端请求.响应给浏览器的动态资源.但servlet的实质就是 ...