POJ 2309 BST
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 8565 | Accepted: 5202 |
Description
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
Output
Sample Input
2
8
10
Sample Output
1 15
9 11
Source
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 <iostream>
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<<min<<" "<<max<<" "<<endl;
}
return 0;
}
POJ 2309 BST的更多相关文章
- POJ 2309 BST(二叉搜索树)
BST Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8657 Accepted: 5277 Description C ...
- POJ 2309 BST 树状数组基本操作
Description Consider an infinite full binary search tree (see the figure below), the numbers in the ...
- 暑假集训单切赛第一场 POJ 2309 BST(找规律的题)
题意:给出一棵二分搜索树,再给一个节点编号n,求以这个节点为根节点的子树叶子节点的最大值与最小值. 首先求n所在的层数,他的层数就是他的因子中2的个数(规律). n的左右各有num=2^i-1个数.最 ...
- poj 2309 BST 使用树阵lowbit
假设领悟了树阵lowbit,这个问题很简单,底部是奇数,使用lowbit(x)寻找x父亲,然后x父亲-1是的最大数量 至于lowbit问题是如何计算,寻找x父亲,事实上x+2^x二进制结束0的数量. ...
- POJ 2309 BST(树状数组Lowbit)
题意是给你一个满二叉树,给一个数字,求以这个数为根的树中最大值和最小值. 理解树状数组中的lowbit的用法. 说这个之前我先说个叫lowbit的东西,lowbit(k)就是把k的二进制的高位1全部清 ...
- POJ 2309 BST(二叉搜索树)
思路:除以2^k,找到商为奇数的位置,k为层数,有2^(k+1)-1个节点 这里直接用位运算,x & -x 就求出 2^k 了. #include<iostream> using ...
- POJ 2309:BST lowbit
BST Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9140 Accepted: 5580 Description C ...
- BST POJ - 2309 思维题
Consider an infinite full binary search tree (see the figure below), the numbers in the nodes are 1, ...
- poj 2309
http://poj.org/problem?id=2309//找规律 可以看到每个根节点都可以将其在同一层的最左边的根节点整除,并且最大值为该节点加上最左边的节点值-1,最小值为////为该节点减去 ...
随机推荐
- 浅析 Node.js 的 vm 模块以及运行不信任代码
在一些系统中,我们希望给用户提供插入自定义逻辑的能力,除了 RPC 和 REST 之外,运行客户提供的代码也是比较常用的方法,好处是可以极大地减少在网络上的耗时.JavaScript 是一种非常流行而 ...
- 2017"百度之星"程序设计大赛 - 初赛(B)
Chess Accepts: 1805 Submissions: 5738 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768 ...
- POJ 2181 Jumping Cows
Jumping Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6398 Accepted: 3828 Desc ...
- 【转】C#中的implicit 和 explicit
The implicit and explicit keywords in C# are used when declaring conversion operators. Let's say tha ...
- [LOJ#121]动态图连通性
[LOJ#121]动态图连通性 试题描述 这是一道模板题. 你要维护一张无向简单图.你被要求加入删除一条边及查询两个点是否连通. 0:加入一条边.保证它不存在. 1:删除一条边.保证它存在. 2:查询 ...
- BZOJ3325 [Scoi2013]密码 【manacher】
题目 Fish是一条生活在海里的鱼.有一天他很无聊,就到处去寻宝.他找到了位于海底深处的宫殿,但是一扇带有密码锁的大门却阻止了他的前进.通过翻阅古籍,Fish 得知了这个密码的相关信息: 该密码的长度 ...
- [BZOJ4318] WJMZBMR打osu! / Easy (期望DP)
题目链接 Solution Wa,我是真的被期望折服了,感觉这道题拿来练手正好. DP的难度可做又巧妙... 我们定义: \(f[i]\) 代表到第 \(i\) 次点击的时候的最大答案. \(g[i] ...
- spoj 7001 Visible Lattice Points莫比乌斯反演
Visible Lattice Points Time Limit:7000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Su ...
- 理解 Glance
OpenStack 由 Glance 提供 Image 服务. 理解 Image 要理解 Image Service 先得搞清楚什么是 Image 以及为什么要用 Image? 在传统 IT 环境下, ...
- vue2 父子组件间通信
父组件往子组件传值 props 传text father.vue <template> <div class="father"> {{'我是父组件'}} & ...