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); } }
博客围绕无限全二叉搜索树问题展开,给出链接https://vjudge.net/problem/POJ - 2309 。问题是求以节点X为根的子树的最小和最大数。解题思路是使用lowbit函数,即求该数二进制下最低位1位置代表的值,结果与x&(-x)相同,还给出算法实现代码的转载链接。
2798

被折叠的 条评论
为什么被折叠?



