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,最小值为////为该节点减去 ...
随机推荐
- Python守护进程、进程互斥锁、进程间通信ICP(Queue队列)、生产者消费者模型
知识点一:守护进程 守护进程:p1.daemon=True 守护进程其实就是一个“子进程“,守护=>伴随 守护进程会伴随主进程的代码运行完毕后而死掉 进程:当父进程需要将一个任务并发出去执行,需 ...
- 用Navicat Premium快速查看mysql数据库版本信息
在出现的界面输入命令 select version();
- 大数相减 C语言
#include <stdio.h> #include <string.h> using namespace std; ],b[]; void Sub() { ; if(a = ...
- 关于 __int128
__int128 是 GCC 提供的扩展(extension),可以当作 128 位整数使用. 关于 __int128 和 __int128_t Normally, _t suffix means a ...
- [luoguP2601] [ZJOI2009]对称的正方形(二维Hash + 二分 || Manacher)
传送门 很蒙蔽,不知道怎么搞. 网上看题解有说可以哈希+二分搞,也有的人说用Manacher搞,Manacher是什么鬼?以后再学. 对于这个题,可以从矩阵4个角hash一遍,然后枚举矩阵中的点,再二 ...
- mysql监控指标
1.最大连接数监控 show VARIABLES like "max_connections"; //最大连接数 show global status like 'Threads_ ...
- 理解 Glance
OpenStack 由 Glance 提供 Image 服务. 理解 Image 要理解 Image Service 先得搞清楚什么是 Image 以及为什么要用 Image? 在传统 IT 环境下, ...
- 三、 java运算符与流程控制
赋值运算 赋值运算符:=,+=,-=,×=,/=,%= class fuzhiyunsuan{ public static void main(String[] args){ int i1 = 10; ...
- PHP解码Json(json_decode)字符串返回NULL的原因及解决方法(转载)
本文主要为大家讲解了php在使用json_decode函数解码json字符串时,解码不成功返回NULL的问题原因分析和解决方法,感兴趣的同学参考下. 一般来说,php对json字符串解码使用json_ ...
- 微信小程序 wx.navigateTo()传参及多个参数方法
var workModeAndPriceList = res.data.data.workModeAndPriceList; //var result = JSON.stringify(workMod ...