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. 使用客户机和主机做DNS服务正向解析及小问题解决

    1.下载yum包 命令:yum install bind-chroot 2.更改配置文件 在这里,要了解到主配置文件为:   /etc/named.conf 但是,为了避免经常修改主配置文件named ...

  2. java中线程同步的几种方法

    1.使用synchronized关键字 由于java的每个对象都有一个内置锁,当用此关键字修饰方法时, 内置锁会保护整个方法.在调用该方法前,需要获得内置锁,否则就处于阻塞状态. 注: synchro ...

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

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

  4. sso单点登录系统

    sso单点登录概念 1.一处登录,处处登录.会单独做一个单点登录系统,只负责颁发token和验证token,和页面登录功能. 2.通过在浏览器cookie中放入token,和在redis中对应toke ...

  5. 深入理解计算机系统 第八章 异常控制流 Part2 第二遍

    第二遍读这本书,每周花两到三小时时间,能读多少读多少(这次看了第 508~530 页,共 23 页) 第一遍对应笔记链接 https://www.cnblogs.com/stone94/p/10206 ...

  6. Project Euler 60: Prime pair sets

    素数3, 7, 109, 673很有意思,从中任取两个素数以任意顺序拼接起来形成的仍然是素数.例如,取出7和109,7109和1097都是素数.这四个素数的和是792,是具有这样性质的四个素数的最小的 ...

  7. Redis实战--使用Jedis实现百万数据秒级插入

    echo编辑整理,欢迎转载,转载请声明文章来源.欢迎添加echo微信(微信号:t2421499075)交流学习. 百战不败,依不自称常胜,百败不颓,依能奋力前行.--这才是真正的堪称强大!!! 当我们 ...

  8. Linux 下的这些高效指令,是你快速学习的神器

    Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户.多任务.支持多线程和多CPU的操作系统.它能运行主要的UNIX工具软件.应用程序和网络协议.它支持32位 ...

  9. requests模拟登陆的三种方式

    ###获取登录后的页面三种方式: 一.实例化seesion,使用seesion发送post请求,在使用他获取登陆后的页面 import requests session = requests.sess ...

  10. Spring Boot: Spring Doc生成OpenAPI3.0文档

    1. 概述 公司正好最近在整理项目的文档,且文档对于构建REST API来说是至关重要的.在这篇文章中,我将介绍Spring Doc , 一个基于OpenAPI 3规范简化了Spring Boot 1 ...