版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

仓库源文站点原文


title: "题解 - [POJ 2309] BST" categories:


题目链接

<!-- more -->

原始题面

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 \leqslant X \leqslant 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

题意简述

给出 $x$, 输出在满二叉搜索树(如下图)中, 以 $x$ 为根的子树中的最小结点编号和最大结点编号

解题思路

在上图中随便找几个例子就能发现

证明是很容易的

对于取最低位1, 有一个运算想必是我们非常熟悉的, 就是lowbit(x), 答案和它密切相关

设给定结点编号为x, 则

代码参考

<details open> <summary><font color='orange'>Show code</font></summary> {% icodeweb cpa_cpp title:POJ_2309 POJ/2309/0.cpp %} </details>