Big binary tree

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 670    Accepted Submission(s): 235

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
 
Source
 
Recommend
liuyiding   |   We have carefully selected several similar problems for you:  6216 6215 6214 6213 6212 
 
题意:题目是给一棵完全二叉树,从上到下从左到右给每个节点标号,每个点有权值,初始权值为其标号,然后有两种操作: 
1、把u点权值改为x 
2、查询所有经过u点的路径中,路径上的点权和最大。
思路:每次修改只会改变log(n)个节点,最多改变log(n)*m个节点,约为2.7*10^6个节点,但是n最大为10^8,所以只能用map存储。没有修改过的结点不需要存储,因为没有修改过的话一定是右儿子大,有以只需要往右走就能寻找到以当前点为根的最大路径点权和,但是如果左边的结点数比右边的多的话就需要往左了。
代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<bitset>
#include<map>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define bug(x) cout<<"bug"<<x<<endl;
#define PI acos(-1.0)
#define eps 1e-8
const int N=2e5+,M=1e5+;
const int inf=0x3f3f3f3f;
const ll INF=1e18+,mod=1e9+;
ll n;
char s[];
map<ll,ll> vis,deep;
ll getnum(ll tmp)
{
ll ans=;
while(tmp<=n)
{
if(deep.find(tmp)!=deep.end()) return ans+deep[tmp];
if(vis.find(tmp)==vis.end()) ans+=tmp;
else ans+=vis[tmp];
ll tmp1=tmp<<,tmp2=tmp<<|;
int h1=,h2=;
while(tmp1<=n) tmp1=tmp1<<,h1++;
while(tmp2<=n) tmp2=tmp2<<,h2++;
if(h1==h2) tmp=tmp<<|;
else tmp=tmp<<;
}
return ans;
}
void getchild(ll u,ll &ch1,ll &ch2)
{
ch1=getnum(u<<),ch2=getnum(u<<|);
}
int main()
{
int m;
while(scanf("%lld%d",&n,&m)!=EOF)
{
vis.clear(),deep.clear();
for(int i=; i<=m; i++)
{
ll u,x;
scanf("%s %lld",s,&u);
if(s[]=='c')
{
scanf("%lld",&x);
vis[u]=x;
while(u>=)
{
ll ch1,ch2;
getchild(u,ch1,ch2);
if(vis.find(u)==vis.end()) deep[u]=max(ch1,ch2)+u;
else deep[u]=max(ch1,ch2)+vis[u];
u/=;
}
}
else
{
ll ch1,ch2;
getchild(u,ch1,ch2);
u=ch1>ch2?(u<<):(u<<|);
ll cou=max(ch1,ch2);
ll ans=;
while(u>)
{
if(vis.find(u/)==vis.end()) cou+=u/;
else cou+=vis[u/];
ans=max(ans,getnum(u^)+cou);
u/=;
}
printf("%lld\n",ans);
}
}
}
return ;
}

HDU 6161.Big binary tree 二叉树的更多相关文章

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

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

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

  3. Leetcode 110 Balanced Binary Tree 二叉树

    判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...

  4. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

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

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

  7. [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

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

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

随机推荐

  1. H5判断手机是否存在应用和打开应用

    伪命题,其实js无法判断您的手机是否存在此应用,遇到这样的需求我们应该跟ios和Android开发的同事商量,需要他们给你一个url尝试打开,如果能打开就表示手机中有该应用,如果不能打开就表示手机没有 ...

  2. 【学习】数据处理基础知识(缺失值处理)【pandas】

    缺失数据(missing data)大部分数据分析应用中非常常见.pd设计目标之一就是让缺失数据的处理任务尽量轻松. pd 使用浮点值NaN(Not a Number) 表示浮点和非浮点数组中的缺失数 ...

  3. 分布式 基本理论 CAP

    谈及分布式,必然谈到 CAP, CAP 已经是被说烂了的一个 话题, 绕不开, 逃不掉.  而且, 理解起来 会有些吃力. 分布式的CAP理论告诉我们 “任何一个分布式系统都无法同时满足 一致性(Co ...

  4. 关联tomcat源代码

    1.进入tomcat官网下载对应版本源代码文件 2. 3..ctrl+鼠标左键 点击Cookie对象 4. 5.

  5. 注解@Transactional(rollbackFor = Exception.class) 的用法

    由于业务需求要求,在一个Service的一个方法A中有一个for循环,每次循环里面的业务逻辑有可能发生异常,这个时候就需要将这个循环内的所有数据库操作给回滚掉,但是又不能影响到之前循环里数据的更改,并 ...

  6. mysql安装,以及从csv插入数据

    1.mysql安装 用管理员身份打开cmd命令行工具,cd到解压文件的bin目录:    ・MySql的Windows服务安装: mysqld install 回车 ・生成无密码的root用户: my ...

  7. eclipse/idea远程调试Linux程序

    第一步.在Tomcat的bin目录下的startup.sh文件的倒数第二行增加“JPDA_ADDRESS=8787”,最后一行在start的前边增加“jpda”,之后重启Tomcat 第二步.配置Ec ...

  8. 最大化系统并发连接数.Windows.reg

    最大化系统并发连接数.Windows.reg Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentContro ...

  9. JavaScript Array some() 方法

    some 判断数组中是否至少有一个元素满足条件 只要有一个满足就返回true 只有都不满足时才返回false 语法: array.some(function(value,index,array),th ...

  10. 查询当前局域网下所有IP和物理网卡地址

    WIN+R –> 打开cmd 键入 arp -a