1.链接地址
https://vjudge.net/problem/POJ-2309
2.问题描述
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.
输入样例
2 8 10
输出样例
1 15 9 11
3.解题思路
使用lowbit函数,也就是求该数在二进制下最低位1的位置所代表的值,结果与x&(-x)相同,可以理解lowbit就是求结点的管辖位置
4.算法实现源代码
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> using namespace std; int lowbit(int k) { return k&(-k); } int main() { int n; scanf("%d",&n); while(n--) { int x; scanf("%d",&x); int k=lowbit(x); printf("%d %d\n",x-k+1,x+k-1); } }