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 ...
随机推荐
- Flask最强攻略 - 跟DragonFire学Flask - 第一篇 你好,我叫Flask
首先,要看你学没学过Django 如果学过Django 的同学,请从头看到尾,如果没有学过Django的同学,并且不想学习Django的同学,轻饶过第一部分 一. Python 现阶段三大主流Web框 ...
- idea编辑项目出现【Information:java: javacTask: 源发行版 7 需要目标发行版 1.7】
在编译项目时候出现问题: Information:java: javacTask: 源发行版 7 需要目标发行版 1.7 解决方案:按着图片操作,这几个地方设置的一样就可以了
- MYSQL 时间类型
常见四种:DATE, TIME, DATETIME, TIMESTAMP DATE: 只表示年月日,YYYY-MM-DD TIME: 只表示时分秒,HH-mm-SS DATETIME: DATE和TI ...
- HDU 4780 Candy Factory(拆点费用流)
Problem Description A new candy factory opens in pku-town. The factory import M machines to produc ...
- Django model进阶
Django-model进阶 QuerySet 可切片 使用Python 的切片语法来限制查询集记录的数目 .它等同于SQL 的LIMIT 和OFFSET 子句. >>> Ent ...
- 代码之髓读后感——名字&作用域&类型
名字和作用域 为什么要取名 看着代码中遍地都是的变量,函数,或多或少的我们都应该想过,为什么会有这些名字呢? 我们知道,计算机将数据存储到对应的物理内存中去.我们的操作就是基于数据的.我们需要使用这些 ...
- python任意进制转换
python任意进制转换 import string def module_n_converter(q, s, base=None): """ 将自然数按照给定的字符串转 ...
- Alpha 冲刺 (8/10)
队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭鸭鸭鸭鸭鸭鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作 多次测试软件运行 学习OPENMP ...
- 标准时间转YYYY-MMM-DD
// 时间处理 formatDate(date, fmt) { let o = { 'M+': date.getMonth() + 1, //月份 'd+': date.getDate(), //日 ...
- 小程序页面传值e.currentTarget
将页面确定上的数值5传到js 微信官网 wtml: <view class="distpicker-btn"> <view class="distpic ...