2309 BST
来源:互联网 发布:哪个车票购票软件 编辑:程序博客网 时间:2024/06/14 09:05
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
2810
Sample Output
1 159 11
很有意思的一道题,我在纸上画了一下题目中BST的树状数组找出了规律,不得不说它真的挺神奇。对于一个子树根x,它的左子树是它覆盖的区间,最小值即为区间的最左端;右子树是覆盖它的区间,最大值即为区间的第二最右端,最右端是x的根。关键还是lowbit()这个运算
#include <iostream>#include <cstdio>#define lowbit(i) (i & (-i))using namespace std;int main(){ int n, x; scanf("%d", &n); while(n--) { scanf("%d", &x); int l = x - lowbit(x) + 1; long long r = x + lowbit(x) - 1; printf("%d %lld\n", l, r); } return 0;}
阅读全文
0 0
- 2309 BST
- poj 2309 BST (^ ^)
- POJ 2309 BST
- poj 2309 BST
- POJ 2309 BST
- POJ 2309 BST
- BST - POJ 2309 水题
- POJ 2309 BST
- poj 2309 BST(lowbit)
- POJ 2309 BST
- poj 2309 BST
- POJ 2309:BST lowbit
- poj 2309 BST
- POJ 2309 BST
- Poj 2309 BST
- POJ 2309 BST
- POJ 2309 BST
- POJ 2309 BST
- markdown pad2注册码(2017年9月27日)
- memcpy和memmove()
- 核函数与径向基函数 (Radial Basis Function 简称 RBF)详解
- 机器学习之离散特征自动化扩展与组合
- 分布式Session的几种实现方式
- 2309 BST
- 晨魅--学Struts1做easyui树形
- (三)架构的代码结构
- Hello world!
- 【知识发现】python开源哈夫曼编码库huffman
- Android自定义View——实现水波纹效果类似剩余流量球
- 罗斯基的机器学习(二)线性回归与Logistic回归
- Linux终端命令
- 重磅 | 最全PPT实录!英伟达发布可编程AI推理加速器TensorRT