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. 设计模式之单例模式 Singleton

    核心作用 保证一个类只有一个实例,并且提供一个访问该实例的全局访问点. 常见应用场景 优点 由于单例模式只生成一个实例,减少了系统性开销,当一个对象的产生需要比较多的资源时,如读取配置,产生其他依赖对 ...

  2. TransH中的Hinge Loss Function

    Hinge Loss Function Hinge Loss 函数一种目标函数,有时也叫max-margin objective. 在Trans系列中,有一个 \[ \max(0,f(h,r,t) + ...

  3. Leetcode 459.重复的子字符串

    重复的子字符串 给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且长度不超过10000. 示例 1: 输入: "abab" 输出: ...

  4. Android library projects cannot be launched解决方法

    着了一个例子项目,总是报标题说的错误. 解决方法如下: 红圈的地方,勾掉. 貌似如果你这个项目是作为一个被引用的project的话, 要勾上这个.单独作为一个app的话,不能勾选这个. --不懂,瞎写 ...

  5. UVa——540Team Queue(STL练习map、queue数组的综合使用)

    Team Queue Time Limit:                                                        3000MS                 ...

  6. linux查找文件命令

    (2)find /etc -name httpd.conf #在/etc目录下文件httpd.conf

  7. vim 翻页命令记录

    vim命令: ctrl-f:往前翻一页(forward) ctrl-b:往后翻一页(backward) ​ ctrl-d:往下翻半页(down) ctrl-u:往上翻半页(up) ​​​

  8. Unity 过度光照贴图

    背景:开关窗帘过程,让环境在亮和暗之间过度 事先烘培出亮.暗两张Lighting map.然后代码实现,窗帘开关由动作实现,而代码中通过动作执行进度来过度两张Lighting map void OnA ...

  9. hdu 6119 小小粉丝度度熊

    小小粉丝度度熊 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  10. 【CF666B】World Tour(贪心,最短路)

    题意:给你一张有向图,叫你给出四个点的序列a,b,c,d,使得这四个点依次间的最短路之和最大.(4 ≤ n ≤ 3000, 3 ≤ m ≤ 5000) 思路:O(n4)可用来对拍 我们需要O(n2)级 ...