【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 ...
随机推荐
- Randomized Online PCA Algorithms with Regret Bounds that are Logarithmic in the Dimension
目录 Setup of Batch PCA and Online PCA Hedge Algorithm 改进算法 用于矩阵 \(rounding()\) 前俩次,都用到了\(rounding()\) ...
- iOS 传感器集锦
https://www.jianshu.com/p/5fc26af852b6 传感器集锦:指纹识别.运动传感器.加速计.环境光感.距离传感器.磁力计.陀螺仪 效果预览.gif 一.指纹识别 应用: ...
- NET操作RabbitMQ组件EasyNetQ
NET操作RabbitMQ组件EasyNetQ使用中文简版文档. 本文出自EasyNetQ官方文档,内容为自己理解加翻译.文档地址:https://github.com/EasyNetQ/EasyNe ...
- 自己实现数据结构系列五---BinarySearchTree
一.二分搜索树: 1.代码: public class BST<E extends Comparable<E>> { private class Node{ public E ...
- rest framwork 小试身手
models.py from django.db import models class Course(models.Model): """ 课程表 "&quo ...
- bootstrap模态框动态赋值, ajax异步请求数据后给id为queryInfo的模态框赋值并弹出模态框(JS)
/查询单个 function query(id) { $.ajax({ url : "/small/productServlet", async : true, type : &q ...
- 关于我使用spring mvc框架做文件上传时遇到的问题
非常感谢作者 原文:https://blog.csdn.net/lingirl/article/details/1714806 昨天尝试着用spring mvc框架做文件上传,犯了挺多不该犯的毛病问题 ...
- websocket协议握手详解
最近使用tornado做长链接想着怎么着也要试试websocket协议吧.所以说干就干. 首先要知道websocket是基于http协议的,为什么这么说?因为从协议来说,websocket是借用了一部 ...
- checkbox选中事件的正确写法
判断选中CHECKBOX事件 网上各种扯淡 搞死我了..加上总觉得smarty引擎和JSJQ有很多冲突.. $("#id").is(":checked");
- iperf网络测试
iperf网络测试文档 地址: https://www.jianshu.com/p/942a9d9bc704