poj2309

1.链接地址

https://vjudge.net/problem/POJ-2309

2.问题描述

 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. 

输入样例

2
8
10

输出样例

1 15
9 11

3.解题思路

 使用lowbit函数,也就是求该数在二进制下最低位1的位置所代表的值,结果与x&(-x)相同,可以理解lowbit就是求结点的管辖位置

4.算法实现源代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;

int lowbit(int k)
{
    return k&(-k);
}

int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
        int x;
        scanf("%d",&x);
        int k=lowbit(x);
        printf("%d %d\n",x-k+1,x+k-1);
    } 
}

 

转载于:https://www.cnblogs.com/KasenBob/p/11176744.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值