【hdu 6161】Big binary tree(二叉树、dp)
多校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)的更多相关文章
- HDU 6161.Big binary tree 二叉树
Big binary tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- 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 ...
- 2017多校第9场 HDU 6161 Big binary tree 思维,类似字典树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6161 题意: 题目是给一棵完全二叉树,从上到下从左到右给每个节点标号,每个点有权值,初始权值为其标号, ...
- Leetcode 110 Balanced Binary Tree 二叉树
判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- [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 ...
- [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 ...
- [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [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 ...
- [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 ...
随机推荐
- python实现满二叉树递归循环
一.二叉树介绍点这片文章 二叉树及题目介绍 例题: 有一颗满二叉树,每个节点是一个开关,初始全是关闭的,小球从顶点落下, 小球每次经过开关就会把它的状态置反,这个开关为关时,小球左跑,为开时右跑.现在 ...
- MongoDB Redis
MongoDB Redis设置用户名密码了吗?看看shodan这款邪恶的搜索引擎吧!~ 早上看新闻的时候看到了个醒目的新闻 开源中国:MongoDB 赎金事件持续发酵,究竟是谁之过?博客园:Mon ...
- O(N) 求数组中最大子串和
int MaxSubSum3(int *arr, int len) { int i; long long MaxSum = 0; long long CurSum = 0; for(int i = 0 ...
- 使用fetch代替ajax请求 post传递方式
let postData = {a:'b'}; fetch('http://data.xxx.com/Admin/Login/login', { method: 'POST', mode: 'cors ...
- laravel中migration 数据迁移
简介 数据库迁移就像是数据库的版本控制,可以让你的团队轻松修改并共享应用程序的数据库结构.迁移通常与 Laravel 的数据库结构生成器配合使用,让你轻松地构建数据库结构.如果你曾经试过让同事手动在数 ...
- [转帖]K8H3D 病毒 腾讯御剑的解析
https://weibo.com/ttarticle/p/show?id=2309404344350225132710 永恒之蓝下载器木马又双叒叕升级了新的攻击方式 背景 腾讯安全御见威胁情报中 ...
- day 7-11 初识MySQL数据库及安装密码设置破解
一. 什么是数据库 之前所学,数据要永久保存,比如用户注册的用户信息,都是保存于文件中,而文件只能存在于某一台机器上. 如果我们不考虑从文件中读取数据的效率问题,并且假设我们的程序所有的组件都运行在一 ...
- VMware与CentOS的安装与Linux简单指令
一 . VMware与CentOS系统安装 下载CentOS系统的ISO镜像 # 官方网站,国外网站,下载速度会很慢 www.centos.org # 由于国外的下载速度慢,我们可以使用国内的镜像源 ...
- 老男孩python学习自修第七天【包与模块】
1.如何导入 from package import module module.function() 常用魔术方法 __init__.py 如果某个文件夹下面有该文件,则该文件夹是一个包,否则只是一 ...
- Yii2常用ActiveRecord用法
1.多表连表查询与对象关联查询 public function getWmsCheck(){ return $this->hasOne(\core\models\WmsCheck::classN ...