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⌋⌊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,代表一个二叉树的节点数,二叉树的每个节点的初始值为节点编号,即node[1]=1,node[2]=2....;然后有M个操作分别为:query k.查询经过节点k的一的一条链的最长长度;

题解:对于未修改之前,对于任意节点,其右儿子,必定大于其左儿子。因此,我们可以求出find_cnt(u),返回u的子节点连成的权值最大的一条链,对于查询来说,只需求出num[u](节点u上的值)+find_cnt(u<<1)+find_cnt(u<<1|1) 最大值,不断更新最大值(沿着节点u往上更新一直到根节点),对于更新,只需判断新值与旧值得大小,。然后不断往上更新。

参考代码为:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
map<int,ll>dp,num;
int n,m;
char s1[10];
ll find(int u)
{
return num.count(u)?num[u]:u;
}
ll find_cnt(int u)
{
if(!u||u>n) return 0;
if(dp.count(u)) return dp[u];
int v,flag1=0,flag2=0;
for(v=u;v<=n;++flag1,v<<=1);
for(v=u;v<=n;++flag2,v=v<<1|1);
if(flag1!=flag2) v=n;
else v>>=1;
ll ans=0;
for(;v>=u;ans+=v,v>>=1);
return ans;
}
void update(int u,ll x)
{
num[u]=x;
while(u)
{
dp[u]=max(find_cnt(u<<1),find_cnt(u<<1|1))+find(u);
u>>=1;
}
}
ll query(int u)
{
ll ans=find(u)+find_cnt(u<<1)+find_cnt(u<<1|1);
ll tot=find_cnt(u);
while(u)
{
ans=max(ans,tot+find_cnt(u^1)+find(u>>1));
u>>=1; tot+=find(u);
}
return ans;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
dp.clear(); num.clear();
while(m--)
{
int u;ll x;
scanf("%s%d",s1,&u);
if(s1[0]=='q') printf("%lld\n",query(u));
else
{
scanf("%lld",&x);
update(u,x);
}
}
}
return 0;
}

  

 

(全国多校重现赛一)A-Big Binary Tree的更多相关文章

  1. (全国多校重现赛一)F-Senior Pan

    Senior Pan fails in his discrete math exam again. So he asks Master ZKC to give him graph theory pro ...

  2. (全国多校重现赛一)D Dying light

    LsF is visiting a local amusement park with his friends, and a mirror room successfully attracts his ...

  3. (全国多校重现赛一) J-Two strings

    Giving two strings and you should judge if they are matched.  The first string contains lowercase le ...

  4. (全国多校重现赛一) H Numbers

    zk has n numbers a1,a2,...,ana1,a2,...,an. For each (i,j) satisfying 1≤i<j≤n, zk generates a new ...

  5. (全国多校重现赛一)E-FFF at Valentine

    At Valentine's eve, Shylock and Lucar were enjoying their time as any other couples. Suddenly, LSH, ...

  6. (全国多校重现赛一)B-Ch's gifts

    Mr. Cui is working off-campus and he misses his girl friend very much. After a whole night tossing a ...

  7. 2017ACM/ICPC广西邀请赛-重现赛 1010.Query on A Tree

    Problem Description Monkey A lives on a tree, he always plays on this tree. One day, monkey A learne ...

  8. 2016ACM/ICPC亚洲区沈阳站-重现赛赛题

    今天做的沈阳站重现赛,自己还是太水,只做出两道签到题,另外两道看懂题意了,但是也没能做出来. 1. Thickest Burger Time Limit: 2000/1000 MS (Java/Oth ...

  9. 2016 CCPC 东北地区重现赛

    1. 2016 CCPC 东北地区重现赛 2.总结:弱渣,只做出01.03.05水题 08   HDU5929 Basic Data Structure    模拟,双端队列 1.题意:模拟一个栈的操 ...

随机推荐

  1. 012.Kubernetes二进制部署worker节点Flannel

    一 部署flannel 1.1 安装flannel kubernetes 要求集群内各节点(包括 master 节点)能通过 Pod 网段互联互通.flannel 使用 vxlan 技术为各节点创建一 ...

  2. ASP.NET Core 1.0: 指定Static File中的文件作为default page

    指定一个网站的default page是很容易的事情.譬如IIS Management中,可以通过default page来指定,而默认的index.html, index.htm之类,则早已经被设置 ...

  3. [HTML] 学HTML写的第一第二个网页

    ①第一个网页 <h2>英雄联盟(电子竞技类游戏)</h2> <p><b>(英雄联盟)</b>(简称lol)是由美国<i>Roit ...

  4. Test Complete 的自动化测试 --- 三角形

    Test Complete 的自动化测试 --- 三角形 PS:工具:Test Complete , OS:win 10 这里做三角形的测试与上一篇博客做计算器的测试大致一样,都是对.exe的执行文件 ...

  5. SpringBoot 配置文件与依赖库分离打包配置

    一.应用场景 一般情况下我们对springboot应用打包时使用springboot的maven插件spring-boot-maven-plugin的maven进行打包,打包完成得到一个fatjar, ...

  6. 快速遍历OpenCV Mat图像数据的多种方法和性能分析 | opencv mat for loop

    本文首发于个人博客https://kezunlin.me/post/61d55ab4/,欢迎阅读! opencv mat for loop Series Part 1: compile opencv ...

  7. salesforce lightning零基础学习(十五) 公用组件之 获取表字段的Picklist(多语言)

    此篇参考:salesforce 零基础学习(六十二)获取sObject中类型为Picklist的field values(含record type) 我们在lightning中在前台会经常碰到获取pi ...

  8. Obtaining the backtrace - libunwind

    Sometimes when working on a large project, I find it useful to figure out all the places from which ...

  9. SQL查询优化实践

    为什么要优化 系统的吞吐量瓶颈往往出现在数据库的访问速度上,即随着应用程序的运行,数据库的中的数据会越来越多,处理时间会相应变慢,且数据是存放在磁盘上的,读写速度无法和内存相比 如何优化 设计数据库时 ...

  10. java之初见

    1.Java语言的了解: Java语言最早是由SUN公司创造出来的,1991年,SUN公司的green项目,Oak,随后SUN公司和后来的甲骨文公司又先后发布了java1.0,1.1,1.2,1.3, ...