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. jQuery 正则数字验证、’Money验证、电话号码验证、身份证验证、邮箱验证、网址验证、敏感字符验证

    jQuery只能输入数字 第一种写法: <input onkeyup="value=value.replace(/[^\d]/g,'')" /> 第二种写法: < ...

  2. HTTP 请求头中的 Remote_Addr,X-Forwarded-For,X-Real-IP

    REMOTE_ADDR 表示发出请求的远程主机的 IP 地址,remote_addr代表客户端的IP,但它的值不是由客户端提供的,而是服务端根据客户端的ip指定的,当你的浏览器访问某个网站时,假设中间 ...

  3. ORACLE导出导入意外终止导致 ORACLE initialization or shutdown in progress 问题解决

    由于意外情况导致 ORACLE initialization or shutdown in progress 个人理解为主要是归档日志出现问题, 首先cmd 1.sqlplus /nolog  进入s ...

  4. 国内最全的Spring Boot系列之二

    历史文章 <国内最全的Spring Boot系列之一> 视频&交流平台 SpringBoot视频:http://t.cn/R3QepWG Spring Cloud视频:http:/ ...

  5. 常用LINUX命令汇总

    一.基本命令bash Bash(GNU Bourne-Again Shell)是许多Linux平台的内定Shellpwd 查看当前所在目录ls 查看目录内所有文件cd 进入目录cd .. 返回上一层p ...

  6. ServletContextListener中的方法contextInitialized执行了两次

    有一个web06项目是直接拷贝web05的,复制过后web06项目默认的web配置中的Context Root还是web05,导致tomcat在启动时还是会创建两个web应用,修改成web06后,cl ...

  7. Lightgbm 随笔

    lightGBM LightGBM 是一个梯度 boosting 框架,使用基于学习算法的决策树.它可以说是分布式的,高效的,有以下优势: 更快的训练效率 低内存使用 更高的准确率 支持并行化学习 可 ...

  8. C++_注释、枚举、typedef

    #include<iostream> //using namespace std; int main() { /* ************************************ ...

  9. 使用python备份文件

    想写个定时备份文件的功能,这个功能需要实现:1.搜索指定的目录里是否存在当天的文件2.如果存在压缩并加密文件3.通过ftp上传到备份服务器4.在备份服务器上定时将文件拷贝到移动硬盘并定时清理文件 本来 ...

  10. hadoop更改保存路径后,批量重启不能启动datanode(或者format以后不能启动datanode)

    这是因为更改文件后,所有的子节点的core-site.xml并没有一致,所以使用start-dfs.sh的时候导致机器起不起来. 修改slave(datanode)节点的core-site.xml就可 ...