题目链接

Problem Description

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.
 
Input
There are multiple cases.
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.
 
Output
For each query operation, output an integer in one line, indicating the max value of all paths which passes the specific node.
 
Sample Input
6 13
query 1
query 2
query 3
query 4
query 5
query 6
change 6 1
query 1
query 2
query 3
query 4
query 5
query 6
 
Sample Output
17
17
17
16
17
17
12
12
12
11
12
12
 
题意:有一棵 由 n 个节点构成的完全二叉树,节点编号1~n,对于i节点其父亲节点为i/2,并且其初始权值为 x ,现在有m次操作:1、修改节点 i 权值为 x 。   2、求经过i节点的所有路径中路径上节点最大权值和。
 
思路:由于n很大1e8,所以不能直接建树进行计算,不能建树怎么计算呢?发现询问的m为1e5 ,所以我们可以用map保存修改的节点。对于每次修改一个节点u时,我们可以计算并用map存下u的子树中从u到其叶子节点的最大权值和,并且向上搜索u的祖先节点,也用map存下相应的最大路径权值和。 对于询问经过 u 的最大的路径权值和,我们只需要从u向上搜索祖先节点即可。
        如求经过D的最大路径权值和。有以下的路径:1、起始为从D的叶子节点经D到D的另一个叶子节点。 2、从D的叶子节点经D B 到B的一个叶子节点。  3、从D的一个叶子节点经B D A 到A的一个叶子节点。所以求经过D的最大路径权值和时就是由D开始向上搜索D的祖先节点,进行计算。
 
代码如下:
#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(思维--压缩空间)的更多相关文章

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

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

  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. HDU 6161.Big binary tree 二叉树

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

  4. 【hdu 6161】Big binary tree(二叉树、dp)

    多校9 1001 hdu 6161 Big binary tree 题意 有一个完全二叉树.编号i的点值是i,操作1是修改一个点的值为x,操作2是查询经过点u的所有路径的路径和最大值.10^5个点,1 ...

  5. Binary Tree HDU - 5573 (思维)

    题目链接: B - Binary Tree  HDU - 5573 题目大意: 给定一颗二叉树,根结点权值为1,左孩子权值是父节点的两倍,右孩子是两倍+1: 给定 n 和 k,让你找一条从根结点走到第 ...

  6. HDU 1710 二叉树的遍历 Binary Tree Traversals

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  7. HDU 5573 Binary Tree 构造

    Binary Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5573 Description The Old Frog King lives ...

  8. HDU 1710 Binary Tree Traversals (二叉树遍历)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  9. HDU 1710 Binary Tree Traversals(树的建立,前序中序后序)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

随机推荐

  1. 3. Go语言基本类型

    Go语言基本类型如下: bool string 数值类型 (int8, int16, int32, int64, int, uint8, uint16, uint32, uint64, uint, f ...

  2. Sonar+maven+jenkins集成,Java代码走查

    Sonar服务在Sonar安装与使用篇已经介绍过,此文章不再说了 Jenkins的安装与配置方法参考http://www.cnblogs.com/chenchen-tester/p/6408815.h ...

  3. js string to date

    Date.prototype.pattern=function(fmt) { //alert(this.getFullYear()); fmt=fmt.toUpperCase(); var o = { ...

  4. 【Spark2.0源码学习】-7.Driver与DriverRunner

         承接上一节内容,Client向Master发起RequestSubmitDriver请求,Master将DriverInfo添加待调度列表中(waitingDrivers),下面针对于Dri ...

  5. BZOJ4386[POI2015]Wycieczki / Luogu3597[POI2015]WYC - 矩乘

    Solution 想到边权为$1$的情况直接矩乘就可以得出长度$<=t$ 的路径条数, 然后二分check一下即可 但是拓展到边权为$2$,$3$ 时, 需要新建节点 $i+n$ 和 $i+2n ...

  6. Mac下Tomcat安装与Intellij IDEA配置Tomcat

    Mac下Tomcat安装与Intellij IDEA配置Tomcat 一 安装 1 下载地址:https://tomcat.apache.org/download-90.cgi 2 将压缩包解压后移至 ...

  7. selenium中动作链的使用

    一.问题 我们有时候在使用selenium的时候,会遇到悬停后点击元素的操作,因此需要一个动作链来完成这个功能. 二.解决 从selenium的包中导入actionchains函数,利用xpath找到 ...

  8. centos7 制作yum源

    一.依赖 1.rsync yum install -y rsync xinetd 2.nginx,这个就不多说了 二.建立目录 mkdir -p /data/yum_source/nginx/html ...

  9. 你会跟谁结婚zz

    正文 咱们日课去年有篇文章叫<你会跟谁结婚?>,提到经济学家的数据显示,现在美国的婚姻中“强强联合”的现象越来越严重,高收入者和高收入者结婚,高学历者和高学历者结婚. 经济学家很无奈,我们 ...

  10. LOJ-10102(求A到B之间的割点)

    题目链接:传送门 思路:求A到B之间必要的中间节点 条件:(1)只有一条路径经过中间节点:(low[B]>=num[u]&&num[v]<=num[B],没有从B到u的路径 ...