BST
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9140   Accepted: 5580

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 <= 231 - 1).

Output

There are N lines in total, the i-th of which contains the answer for the i-th query.

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的更多相关文章

  1. 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 ...

  2. POJ 3252:Round Numbers

    POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...

  3. POJ 2309 BST(树状数组Lowbit)

    题意是给你一个满二叉树,给一个数字,求以这个数为根的树中最大值和最小值. 理解树状数组中的lowbit的用法. 说这个之前我先说个叫lowbit的东西,lowbit(k)就是把k的二进制的高位1全部清 ...

  4. poj 2309 BST 使用树阵lowbit

    假设领悟了树阵lowbit,这个问题很简单,底部是奇数,使用lowbit(x)寻找x父亲,然后x父亲-1是的最大数量 至于lowbit问题是如何计算,寻找x父亲,事实上x+2^x二进制结束0的数量. ...

  5. POJ 2309 BST

    BST Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8565   Accepted: 5202 Description C ...

  6. POJ 2309 BST(二叉搜索树)

    思路:除以2^k,找到商为奇数的位置,k为层数,有2^(k+1)-1个节点 这里直接用位运算,x & -x 就求出 2^k 了. #include<iostream> using ...

  7. POJ 2309 BST(二叉搜索树)

    BST Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8657   Accepted: 5277 Description C ...

  8. 暑假集训单切赛第一场 POJ 2309 BST(找规律的题)

    题意:给出一棵二分搜索树,再给一个节点编号n,求以这个节点为根节点的子树叶子节点的最大值与最小值. 首先求n所在的层数,他的层数就是他的因子中2的个数(规律). n的左右各有num=2^i-1个数.最 ...

  9. BST POJ - 2309 思维题

    Consider an infinite full binary search tree (see the figure below), the numbers in the nodes are 1, ...

随机推荐

  1. 1 (msql实战) 基础架构

    mysql> select * from T where ID=10: 我们看到的只是输入一条语句,返回一个结果,却不知道这条语句在 MySQL 内部的执行过程. 所以今天我想和你一起把 MyS ...

  2. Dubbo与SpringBoot整合流程(从实例入手,附代码下载)

    场景 Dubbo环境搭建-管理控制台dubbo-admin实现服务监控: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10362 ...

  3. HTTP关键词收集

    [HTTP协议][客户端][服务器端][HTTPS][Web服务器][域名][DNS][IP地址][虚拟服务器][虚拟主机][中转服务器][HTTP/1.1规范][域名解析][Web托管服务][代理] ...

  4. SCP 上传

    https://www.runoob.com/linux/linux-comm-scp.html scp 当前路径(绝对路径) root@xx.xx.xx :/xx/xx/xx/

  5. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表单:表单控件大小

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:设置元素为 display:block 并居中显示

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. eclipse如何把多个项目放在文件夹下,用文件夹分开不同的项目

    在Package Explorer顶部的右侧有有机表图标按钮,点击倒三角 Top Level Elements->Working Set.此时就会发现,很多项目会自动纳入一个文件夹,这个文件夹的 ...

  8. jenkins 2.204.2 安装, 使用国内源安装, 并且跳过插件界面, 更新成国内插件源.

    需要java环境支持,自行百度. jenkins 安装源在国外, 下载会比较慢, 尤其在linux下, 使用yum或者apt install jenkins方式安装时,经常会下载失败. 由于yum或者 ...

  9. IdentityServer4专题之五:OpenID Connect及其Client Credentials流程模式

    1.基于概念 OAuth2.0与身份认证协议的角色映射 OpenID Connect 这个协议是2014颁发的,基于OAuth2.0,在这个协议中,ID Token会和Access Token一起发回 ...

  10. 记一次菜鸡的低级折腾--WordPress get Webshell(后台文件编辑插马)

    挺简单的一个测试站,开始思路错了,一直去网上找WordPress的漏洞,看有没有什么能利用的,未果,因为这个测试站有些地方并不完善,有的漏洞利用不了,菜鸡的我连弱口令都没猜对,没知识就是这么悲哀. 下 ...