POJ 2309 BST
来源:互联网 发布:无间道1细节知乎 编辑:程序博客网 时间:2024/05/18 00:34
题目链接:http://poj.org/problem?id=2309
题意:好的,让我们来用一下原图,就是这样一个二叉树,给你一个节点,让你求这个节点和其子节点中,编号最大的和最小的,比如求12的就是 9和15。
思路:
让我们先把这些编号用二进制来表示。
找一下规律,发现每个数在第几层是由它后面有几个0决定的
而且一个节点在范围内属于中间的那个数,左右两边节点数量相等,而且分别都小于或大于根结点。
我们可以用树状数组的lowbit来求一下假设lowbit(x) = k;
可以发现左右子树的节点个数都是k-1,那么x-(k-1)就是最小的,x+(k-1)就是最大的。
#include <iostream>using namespace std;int n;int lowbit(int x){ return x&(-x);}int main(){ int t; cin>>n; while(n--) { cin>>t; cout<<t-lowbit(t)+1<<" "<<t+lowbit(t)-1<<endl; } return 0;}
0 0
- 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
- poj 2309 BST(水)
- 使用QT实现右击菜单实例
- 怎样选择Hadoop的基准测试
- Mysql 索引重置
- Android开发学习之路--Camera之初体验
- 零散小经验
- POJ 2309 BST
- 大数据应用的一点认识
- Smooth Follow
- 牙
- narnia4
- firefox 禁用javascript
- 博客,我来了~
- Java描述贪心算法解决背包问题
- NSTimer用法总结