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. Unity中用Mesh画一个圆环(二)

    中目标-生成完整面 在之前的内容中我们已经成功生成了一个面,接下来我们要生成剩下的面就很容易了. 我们把之前生成的面当作顶面,接着我们来生成底面. 还记得前面说过\(\color{#1E90FF}{D ...

  2. Vue学习笔记:Vue组件的核心概念(下)

    1.双向绑定和单向数据流: 本质上还是单向数据流 视图<——>数据 v-model:仅仅是一个简写,用更少代码去实现功能. 自定义事件 .sync 修饰符 2.虚拟DOM及KEY属性作用 ...

  3. PHP Laravel 队列技巧:Fail、Retry 或者 Delay

    当创建队列jobs.监听器或订阅服务器以推送到队列中时,您可能会开始认为,一旦分派,队列工作器决定如何处理您的逻辑就完全由您自己决定了. 嗯……并不是说你不能从作业内部与队列工作器交互,但是通常情况下 ...

  4. 一个ip, 两个域名, 两个ssl, 对应多个不同的项目 之 坑

    之前配置了好几天, 就想通过tomcat直接配置. 找各种资料, 都说先配置Connector, 在配置Host. 我试了很多次, 都不成功. 原因我也没有找到在哪里. 我的配置参考如下网址: 修改这 ...

  5. HTML中的表格标签

      表格是网页制作中使用最多的工具之一,在制作网页时,使用表格可以更清晰地排列数据.但是在实际制作过程中,表格更多用在网页布局的定位上.很多网页都是以表格布局的.这是因为表格在文本和图像的位置控制方面 ...

  6. 堡垒机的核心武器:WebSSH录像实现

    WebSSH终端录像的实现终于来了 前边写了两篇文章『Asciinema:你的所有操作都将被录制』和『Asciinema文章勘误及Web端使用介绍』深入介绍了终端录制工具Asciinema,我们已经可 ...

  7. MySQL数据库的主从同步

    什么要进行数据库的主从同步? 防止单点故障造成的数据丢失 主从复制的原理 MySQL从数据库开启I/O线程,向主服务器发送请求数据同步(获取二进制日志) MySQL数据库开启I/O线程回应从数据库 从 ...

  8. HTML基础学习心得分享

    开始学些Html的时候主要进行一些简单的静态网页的处理: 1.HTML 标题 HTML 标题(Heading)是通过 h1-h6 加中括号<>等标签进行定义的. 2.HTML 段落 HTM ...

  9. .NET进阶篇06-async异步、thread多线程3

    知识需要不断积累.总结和沉淀,思考和写作是成长的催化剂 梯子 一.任务Task1.启动任务2.阻塞延续3.任务层次结构4.枚举参数5.任务取消6.任务结果7.异常二.并行Parallel1.Paral ...

  10. python进程概要

    进程 狭义:正在运行的程序实例. 广义:进程是一个具有一定独立功能的程序关于某个数据集合的一次运行活动,他是操作系统动态执行的基本单元. python的进程都是并行的. 并行:两个进程同时执行一起走. ...