POJ2309 BST

POJ2309 BST

大家好,又见面了,我是全栈君。

 
Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu

Description

Consider an infinite full binary search tree (see the figure below), the numbers in the nodes are 1, 2, 3, …. In a subtree whose root node is X, we can get the minimum number in this subtree by repeating going down the left node until the last level, and we can also find the maximum number by going down the right node. Now you are given some queries as “What are the minimum and maximum numbers in the subtree whose root node is X?” Please try to find answers for there queries. 



POJ2309 BST

Input

In the input, the first line contains an integer N, which represents the number of queries. In the next N lines, each contains a number representing a subtree with root number X (1 <= X <= 2 
31 – 1).

Output

There are N lines in total, the i-th of which contains the answer for the i-th query.

Sample Input

2
8
10

Sample Output

1 15
9 11

Source

POJ Monthly,Minkerui
 
用树状数组的lowbit处理即可。(x&-x)可以取出x二进制表示下的最后一个1,即可知x的管辖半径。
 1 /*by SilverN*/
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 using namespace std;
 8 int read(){
 9     int x=0,f=1;char ch=getchar();
10     while(ch<'0' || ch>'9'){
     
     if(ch=='-')f=-1;ch=getchar();}
11     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
12     return x*f;
13 }
14 int lowbit(int x){
     
     return x&-x;}
15 int T;
16 int n;
17 int main(){
18     T=read();
19     while(T--){
20         n=read();
21         printf("%d %d\n",n-lowbit(n)+1,n+lowbit(n)-1);
22     }
23     return 0;
24 }

 

转载于:https://www.cnblogs.com/SilverNebula/p/5889498.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/108872.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Ubuntu apt-get彻底卸载软件包

    Ubuntu apt-get彻底卸载软件包如果你关注搜索到这篇文章,那么我可以合理怀疑你被apt-get的几个卸载命令有点搞晕了。apt-get的卸载相关的命令有remove/purge/autoremove/clean/autoclean等。具体来说:apt-getpurge/apt-get–purgeremove删除已安装包(不保留配置文件)。如软件包a,依赖软件包b,则执行该命令会删除a,而且不保留配置文…

    2022年5月30日
    41
  • Django(67)drf搜索过滤和排序过滤

    Django(67)drf搜索过滤和排序过滤前言当我们需要对后台的数据进行过滤的时候,drf有两种,搜索过滤和排序过滤。搜索过滤:比如我们想返回sex=1的,那么我们就可以从所有数据中进行筛选排序过滤:比如我们想对价格进行升序排列,就可以

    2022年7月30日
    4
  • mybatis实现原理,动态代理_jdk动态代理实现原理

    mybatis实现原理,动态代理_jdk动态代理实现原理前言一直以来都在使用MyBatis做持久化框架,也知道当我们定义XXXMapper接口类并利用它来做CRUD操作时,Mybatis是利用了动态代理的技术帮我们生成代理类。那么动态代理内部的实现细节到底是怎么的呀?XXXMapper.java类和XXXMapper.xml到底是如何关联起来的呀?本篇文章就来详细剖析下MyBatis的动态代理的具体实现机制。MyBatis的核心组件及应用在详细探究MyBatis中动态代理机制之前,先来补充一下基础知识,认识一下MyBatis的核心组件。SqlSessio

    2022年8月8日
    2
  • 一文让你知道为什么学了PHP的都要转学Go语言[通俗易懂]

    一文让你知道为什么学了PHP的都要转学Go语言

    2022年2月12日
    36
  • TCP四次挥手原因

    TCP四次挥手原因客户端发了一个FIN501(0)ACK701,然后服务端回了一个ACK502。。此时客户端就处于半关闭状态。至于半关闭的话,还得详细解释一下:刚开始(左边)客户端和服务端(右边)都可以互相进行通信,都能进行发送数据和接收数据,但当客户端处于半关闭时,就变成了下面这样(此图只是举个例子,没必要纠结图)这图想表达的就是,客户端(左边)只能进行接收数据,而不能发送数据,服务端(右边)能进行发送数据和接收数据这种是通过什么机制实现的呢?因为一个套接字(左右方框均可看成.

    2022年5月13日
    51
  • Boost.Lockfree官方文档翻译

    Boost.Lockfree官方文档翻译Boost_1_53_0终于迎来了久违的Boost.Lockfree模块,本着学习的心态,将其翻译如下。(原文地址:http://www.boost.org/doc/libs/1_53_0/doc/html/lockfree.html) Chapter 17. Boost.Lockfree第17章.Boost.LockfreeTableofContents目录Intro

    2022年7月19日
    12

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号