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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • UE4制作星际天空球[通俗易懂]

    UE4制作星际天空球[通俗易懂]效果图:需要的东东西:6张无缝连接的图片,如果没有的推荐大家下载“Spacescape”3dsMax2018软件EpicGamesLauncher(UE4游戏引擎)然后就是阅读本博客了教程开始首先教大家使用Spacescape如果有素材的读者可以直接跳过打开界面如下点击左上角文件打开或者直接快捷键Ctro+O选择素材笔者推荐最后两个,不过无妨大家都

    2022年9月27日
    0
  • 并发编程之深入理解Condition

    并发编程之深入理解Condition

    2021年8月3日
    74
  • vue3数据劫持_vue实现keepalive

    vue3数据劫持_vue实现keepalive模仿vue的数据劫持,实现mvvm

    2022年4月22日
    131
  • Java正则表达式简介及实例

    Java正则表达式简介及实例何为正则表达式?有时候会需要编写代码来验证用户输入,比如验证输入是否是一个数字,是否是一个全部小写的字符串,或者社会安全号,完成这个任务一个简单高效的方法就是用正则表达式!

    2022年7月19日
    7
  • ewebeditor漏洞大全

    ewebeditor漏洞大全1:默认管理后台: http://www.backlion.com/ewebeditor/admin_login.asp后台如果能进入:可点击样式管理:standard拷贝一份(直接修改改不了)在拷贝的一份里加入图片类型(asaaaspsp)  然后点预览在编辑器里点设计   然后直接上传asa大马.上传后在代码里可以看到马的位置!

    2022年7月14日
    73
  • php 字符串 替换 最后,如何替换php字符串中最后一个字符

    php 字符串 替换 最后,如何替换php字符串中最后一个字符如何替换php字符串中最后一个字符发布时间:2020-08-1010:36:23来源:亿速云阅读:91作者:Leah这篇文章运用简单易懂的例子给大家介绍如何替换php字符串中最后一个字符,代码非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。php替换字符串最后一个字符的方法:首先使用PHP中的“substr”函数或者“mb_substr”截取字符串至倒数第一位;然后拼接自己想要的数…

    2022年5月23日
    34

发表回复

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

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