博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2309 BST
阅读量:5764 次
发布时间:2019-06-18

本文共 1490 字,大约阅读时间需要 4 分钟。

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 <= 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

2810

Sample Output

1 159 11

Source

,Minkerui

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 
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<
<<" "<
<<" "<

转载地址:http://hywux.baihongyu.com/

你可能感兴趣的文章
Java 架构师眼中的 HTTP 协议
查看>>
Linux 目录结构和常用命令
查看>>
Linux内存管理之mmap详解 (可用于android底层内存调试)
查看>>
利润表(年末)未分配利润公式备份
查看>>
Android开发中ViewStub的应用方法
查看>>
gen already exists but is not a source folder. Convert to a source folder or rename it 的解决办法...
查看>>
HDOJ-2069Coin Change(母函数加强)
查看>>
遍历Map的四种方法
查看>>
IOS atomic与nonatomic,assign,copy与retain的定义和区别
查看>>
JAVA学习:maven开发环境快速搭建
查看>>
Altium Designer 小记
查看>>
【Linux高级驱动】I2C驱动框架分析
查看>>
赵雅智:js知识点汇总
查看>>
二维有序数组查找数字
查看>>
20个Linux服务器性能调优技巧
查看>>
多重影分身:一套代码如何生成多个小程序?
查看>>
Oracle将NetBeans交给了Apache基金会
查看>>
填坑记:Uncaught RangeError: Maximum call stack size exceeded
查看>>
SpringCloud之消息总线(Spring Cloud Bus)(八)
查看>>
DLA实现跨地域、跨实例的多AnalyticDB读写访问
查看>>