hdu 6161--Big binary tree(思维--压缩空间)

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.
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.

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
using namespace std;
typedef long long LL;
map<int,LL>mp;
map<int,LL>mx;
LL ans;
int n,m;
int pos[]; void init()
{
int tmp=n;
int deep=(int)log2(n)+;
for(int i=deep;i>=;i--)
{
pos[i]=tmp;
tmp>>=;
}
} void cal(int x)
{
if(mp.count(x)) return ;
if(x>n) { mp[x]=; return ; }
int deep=(int)log2(x)+;
LL tmp=;
for(int i=x;i<=n;i=(i<<|)) tmp+=i;
if(pos[deep]==x){
LL sum=;
for(int i=deep;;i++)
{
sum+=pos[i];
if(pos[i]==n) break;
}
tmp=max(tmp,sum);
}
mp[x]=tmp;
} void update(int x)
{
if(!x) return ;
LL y;
if(mx.count(x)==) y=x;
else y=mx[x];
cal(x<<);
cal(x<<|);
mp[x]=max(mp[x<<],mp[x<<|])+y;
update(x>>);
} void query(LL sum,int x,int son)
{
if(!x) return ;
cal(x<<);
cal(x<<|);
if(!mx.count(x)) mx[x]=x;
ans=max(ans,sum+mp[son^]+mx[x]);
sum+=mx[x];
query(sum,x>>,x);
} int main()
{
char s[];
while(scanf("%d",&n)!=EOF)
{
init();
mp.clear();
mx.clear();
scanf("%d",&m);
while(m--)
{
scanf("%s",s);
if(s[]=='q')
{
int x; scanf("%d",&x);
cal(x<<);
cal(x<<|);
if(!mx.count(x)) mx[x]=x;
ans=mp[x<<]+mp[x<<|]+mx[x];
cal(x);
query(mp[x],x>>,x);
printf("%lld\n",ans);
}
else
{
int x; LL y; scanf("%d%lld",&x,&y);
mx[x]=y;
update(x);
}
}
}
return ;
}
hdu 6161--Big binary tree(思维--压缩空间)的更多相关文章
- 2017多校第9场 HDU 6161 Big binary tree 思维,类似字典树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6161 题意: 题目是给一棵完全二叉树,从上到下从左到右给每个节点标号,每个点有权值,初始权值为其标号, ...
- 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 ...
- HDU 6161.Big binary tree 二叉树
Big binary tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- 【hdu 6161】Big binary tree(二叉树、dp)
多校9 1001 hdu 6161 Big binary tree 题意 有一个完全二叉树.编号i的点值是i,操作1是修改一个点的值为x,操作2是查询经过点u的所有路径的路径和最大值.10^5个点,1 ...
- Binary Tree HDU - 5573 (思维)
题目链接: B - Binary Tree HDU - 5573 题目大意: 给定一颗二叉树,根结点权值为1,左孩子权值是父节点的两倍,右孩子是两倍+1: 给定 n 和 k,让你找一条从根结点走到第 ...
- HDU 1710 二叉树的遍历 Binary Tree Traversals
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU 5573 Binary Tree 构造
Binary Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5573 Description The Old Frog King lives ...
- HDU 1710 Binary Tree Traversals (二叉树遍历)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU 1710 Binary Tree Traversals(树的建立,前序中序后序)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
随机推荐
- ASP 错误捕捉,处理
Asp利用 On Error Resume Next捕捉异常,根据Err.Number判断是否有错误 注:On Error Goto 0取消捕捉异常 模板文件页面 <% Response.Buf ...
- MAC book 无法删除普通用户的解决办法
1来自苹果官网 macOS Sierra: 删除用户或群组 如果您是管理员,当您不想再让某些用户访问 Mac 时,可以删除他们.您也可以删除不想要的群组. 删除用户时,您可以存储该用户的个人文件夹(包 ...
- 微信公众号的分享接口,分享提示config:fail,invalid signature的解决办法(2017年12月)
微信中打开网页,使用微信右上角菜单中自带的分享功能的经历及总结: 最开始,微信分享页面时,直接读取页面的标题(title)和页面中的第一张符合条件的图片[此种方式在2017-03-29之前管用,这一天 ...
- 删除centos7中自带有python2.7
删除centos7中自带有python2. ()强制删除已安装python及其关联 # rpm -qa|grep python|xargs rpm -ev --allmatches --nodeps ...
- kafka可视化客户端工具(Kafka Tool)的基本使用
1.下载 下载地址:http://www.kafkatool.com/download.html 2.安装 根据不同的系统下载对应的版本,我这里kafka版本是1.1.0,下载kafka tool 2 ...
- 选择困难症的福音——团队Scrum冲刺阶段-Day 1领航
选择困难症的福音--团队Scrum冲刺阶段-Day 1领航 各个成员在 Alpha 阶段认领的任务 小组成员 分工 任务量 严域俊 完成小游戏接口部分.小游戏编写部分 21 吴恒佚 决策判断部分.小游 ...
- AJAX html 传输json字符串&&巧妙运用eval()来解析返回的JSON字符串
1.AJAX html 传输json字符串: js方法如下: function saveRetYwlsh(){ var xmbh = document.getElementById("xmb ...
- AutoCAD开发4--添加块)
Private Sub CommandButton3_Click() Dim pInsertPnt As Variant 'pInsertPnt(0) = 100.5141: pInsertPnt(1 ...
- 卓豪ManageEngine参加2018企业数字化转型与CIO职业发展高峰论坛
卓豪ManageEngine参加2018企业数字化转型与CIO职业发展高峰论坛 2018年10月20日,78CIO APP在北京龙城温德姆酒店主办了主题为“新模式.新动能.新发展”的<2018企 ...
- noip第21课资料