BST
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8565   Accepted: 5202

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. 

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 <= 231 - 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

lowbit的作用。计算x相应的二进制数中第一个1的位置k,返回权值2k。

这个函数的作用就是求出t这个数的二进制存储下,最高的非0bit所表示的大小。
即满足2^k<=t的最大的2^k,当中k为非负整数。

1、min和max为奇数,否则min和max非叶子,还能够向下拓展

2、依据满二叉树的性质,x的左右子树的个数都为2的k次方减1个节点

3、依据二叉树搜索的性质,左子树编号的区间为[min,x-1],右子树的编号区间为[x+1,max]

由此得出min=x-(2^k-1),max=x+(2^k-1)

#include <iostream>
using namespace std;
int lowbit(int x)
{
return x&(-x);
}
int main()
{
int N;
cin>>N; int c;
for(c=1;c<=N;c++)
{
int n;
cin>>n; int min,max;
min=n-lowbit(n)+1;
max=n+lowbit(n)-1; cout<<min<<" "<<max<<" "<<endl;
}
return 0;
}

POJ 2309 BST的更多相关文章

  1. POJ 2309 BST(二叉搜索树)

    BST Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8657   Accepted: 5277 Description C ...

  2. POJ 2309 BST 树状数组基本操作

    Description Consider an infinite full binary search tree (see the figure below), the numbers in the ...

  3. 暑假集训单切赛第一场 POJ 2309 BST(找规律的题)

    题意:给出一棵二分搜索树,再给一个节点编号n,求以这个节点为根节点的子树叶子节点的最大值与最小值. 首先求n所在的层数,他的层数就是他的因子中2的个数(规律). n的左右各有num=2^i-1个数.最 ...

  4. poj 2309 BST 使用树阵lowbit

    假设领悟了树阵lowbit,这个问题很简单,底部是奇数,使用lowbit(x)寻找x父亲,然后x父亲-1是的最大数量 至于lowbit问题是如何计算,寻找x父亲,事实上x+2^x二进制结束0的数量. ...

  5. POJ 2309 BST(树状数组Lowbit)

    题意是给你一个满二叉树,给一个数字,求以这个数为根的树中最大值和最小值. 理解树状数组中的lowbit的用法. 说这个之前我先说个叫lowbit的东西,lowbit(k)就是把k的二进制的高位1全部清 ...

  6. POJ 2309 BST(二叉搜索树)

    思路:除以2^k,找到商为奇数的位置,k为层数,有2^(k+1)-1个节点 这里直接用位运算,x & -x 就求出 2^k 了. #include<iostream> using ...

  7. POJ 2309:BST lowbit

    BST Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9140   Accepted: 5580 Description C ...

  8. BST POJ - 2309 思维题

    Consider an infinite full binary search tree (see the figure below), the numbers in the nodes are 1, ...

  9. poj 2309

    http://poj.org/problem?id=2309//找规律 可以看到每个根节点都可以将其在同一层的最左边的根节点整除,并且最大值为该节点加上最左边的节点值-1,最小值为////为该节点减去 ...

随机推荐

  1. JAVA-STRUTS-2x的项目配置

    首先是web.xml的配置,这个是项目加载的开始. <display-name></display-name> <!--struts2配置开始--> <fil ...

  2. 解决面试问题中的top k问题 Leetcode

    https://leetcode.com/problems/kth-largest-element-in-an-array/ 使用堆,堆插入一个数据是logk,删除一个数据是logk,复杂度为logk ...

  3. Map容器——TreeMap及常用API,Comparator和Comparable接口

    TreeMap及常用API ①   TreeMap类通过使用红黑树实现Map接口; ②   TreeMap提供按排序顺序存储键/值对的有效手段,同时允许快速检索; ③   不像散列(HashMap), ...

  4. BZOJ4556 [Tjoi2016&Heoi2016]字符串 【后缀数组 + 主席树 + 二分 + ST表】

    题目 佳媛姐姐过生日的时候,她的小伙伴从某东上买了一个生日礼物.生日礼物放在一个神奇的箱子中.箱子外边写了 一个长为n的字符串s,和m个问题.佳媛姐姐必须正确回答这m个问题,才能打开箱子拿到礼物,升职 ...

  5. 刷题总结——Human Gene Functions(hdu1080)

    题目: Problem Description It is well known that a human gene can be considered as a sequence, consisti ...

  6. Miracast HDCP 等知识

    Miracast 通讯架构中关于视频数据处理流程的部分.整个视频数据处理及传输的流程,大致上分为几个阶段,一开始将撷取到系统的画面及声音进行压缩,而压缩后的影音数据再转为基本封包串流(Packetiz ...

  7. C#递归删除进程及其子进程

    /// <summary> /// 结束进程和相关的子进程 /// </summary> /// <param name="pid">需要结束的 ...

  8. 解决vue项目route使用history模式,tomcat部署刷新url 404问题

    在webapps/项目名 创建WEB-INF ,创建web.xml文件 文件内容如下: <?xml version="1.0" encoding="UTF-8&qu ...

  9. Windows connect to mysql failed: can't get hostname for your address

    My mysql is on Linux platform. When I used my laptop connect to mysql, I got error message like &quo ...

  10. one day php. alomost all;

    <? namespace Test; use \PhpProject\PhpApp as Other; $u=new Other("ns test"); echo $u-&g ...