POJ 2309:BST lowbit
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 9140 | Accepted: 5580 |
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
发现每个点含有的节点数量就是做树状数组时lowbit()的值,lowbit()就是一个数x在二进制表示下最右边的那个1保留,其余值全部赋值为0的二进制数。
简单表示就是x&(x^(x-1))=x&(-x)。
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#pragma warning(disable:4996)
using namespace std; const int N = 32005; int c[N],a[N],n; int lowbit(int x)
{
return x & (-x);
} int main()
{
int i,query;
long long ask;
scanf("%d",&query); for(i=1;i<=query;i++)
{
cin>>ask;
cout<<ask-(lowbit(ask)-1)<<" "<<ask+(lowbit(ask)-1)<<endl;
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 2309:BST lowbit的更多相关文章
- POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)
http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...
- POJ 3252:Round Numbers
POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...
- POJ 2309 BST(树状数组Lowbit)
题意是给你一个满二叉树,给一个数字,求以这个数为根的树中最大值和最小值. 理解树状数组中的lowbit的用法. 说这个之前我先说个叫lowbit的东西,lowbit(k)就是把k的二进制的高位1全部清 ...
- poj 2309 BST 使用树阵lowbit
假设领悟了树阵lowbit,这个问题很简单,底部是奇数,使用lowbit(x)寻找x父亲,然后x父亲-1是的最大数量 至于lowbit问题是如何计算,寻找x父亲,事实上x+2^x二进制结束0的数量. ...
- POJ 2309 BST
BST Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8565 Accepted: 5202 Description C ...
- POJ 2309 BST(二叉搜索树)
思路:除以2^k,找到商为奇数的位置,k为层数,有2^(k+1)-1个节点 这里直接用位运算,x & -x 就求出 2^k 了. #include<iostream> using ...
- POJ 2309 BST(二叉搜索树)
BST Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8657 Accepted: 5277 Description C ...
- 暑假集训单切赛第一场 POJ 2309 BST(找规律的题)
题意:给出一棵二分搜索树,再给一个节点编号n,求以这个节点为根节点的子树叶子节点的最大值与最小值. 首先求n所在的层数,他的层数就是他的因子中2的个数(规律). n的左右各有num=2^i-1个数.最 ...
- BST POJ - 2309 思维题
Consider an infinite full binary search tree (see the figure below), the numbers in the nodes are 1, ...
随机推荐
- 「luogu2633」Count on a tree
「luogu2633」Count on a tree 传送门 树上主席树板子. 每个节点的根从其父节点更新得到,查询的时候差分一下就好了. 参考代码: #include <algorithm&g ...
- spark aggregateByKey 时 java.lang.OutOfMemoryError: GC overhead limit exceeded
最后发现有一个用户单日访问我们网站次数为 4千万,直接导致 aggregate 时内存不够.过滤掉该用户即可.
- 常用的UI控件
关于本文:作为一名iOS软件工程师,熟练规范的使用常用的UI控件是必备的基础技能. 指示器(UIActivityIndicatorView)----转动的等待小菊花 提醒对话框(UIAlertView ...
- centos 访问win共享
yum install samba 安装samba (其实我们只用到samba里面的winbind以便我们能够用windows机器的名称找到该机器的网络地址,在下面叙述的过程会用到.而且也要确定在 w ...
- 有关vector元素的取地址
1--原则上,最好不要对vector的元素取地址,除非所有的vector元素已经填充完毕,这样vector的元素不会发生位置移动,地址才不会变,这样才能确保取得的地址的有效性.PS:即使在可以用已经分 ...
- 第1节 kafka消息队列:3、4、kafka的安装以及命令行的管理使用
6.kafka的安装 5.1三台机器安装zookeeper 注意:安装zookeeper之前一定要确保三台机器时钟同步 */1 * * * * /usr/sbin/ntpdate us.pool.nt ...
- Day8 - C - Largest Rectangle in a Histogram HDU - 1506
A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rec ...
- 「NOIP2011」观光公交
传送门 Luogu 解题思路 有点麻烦,幸好 \(O(n^2)\) 能过... 贪心地想一想,我们如果要用加速器,肯定是要选择车上人数最多的时段加速. 但是我们就会面临这样的情况: 加速了,带来了增益 ...
- 码云上部署hexo博客框架
title: 码云上部署hexo博客框架 Hexo框架在码云上实现个人博客 本文受 https://www.jianshu.com/p/84ae2ba1c133 启发编写 本地调试 安装完Node.j ...
- Jquery实现横向tab切换
//需求:鼠标放在不同的导航栏上,下面显示的内容自动切换 //代码如下 <!DOCTYPE html> <html lang="en"> <head& ...