BST POJ - 2309 思维题

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 <= 2 31 - 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

https://blog.csdn.net/fengkuangdewoniudada/article/details/69660755规律请看这篇博客
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define pi acos(-1.0)
#define eps 1e-6
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define bug         printf("******\n")
#define mem(a,b)    memset(a,b,sizeof(a))
#define fuck(x)     cout<<"["<<x<<"]"<<endl
#define f(a)        a*a
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define FIN freopen("DATA.txt","r",stdin)
#define lowbit(x)   x&-x
#pragma comment (linker,"/STACK:102400000,102400000")

using namespace std;
typedef long long LL;

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

原文地址:https://www.cnblogs.com/qldabiaoge/p/9415740.html

时间: 2024-09-30 06:38:53

BST POJ - 2309 思维题的相关文章

poj 1852思维题

背景:挑战程序设计竞赛上的题,好思维.来就想暴力枚举都还没有仔细思考有没有数学规律,n超过20就不适合用2的n次方的算法了. 思路:最短时间十分容易讨论,这里最大时间很巧妙,两只蚂蚁相撞然后各自反向走,可以想成两只蚂蚁绕过,各走各的,这样早最大时间就简单了,就是所有走到端点的时间中最大的. 代码: #include <cstdio> #include <cstdlib> #include <iostream> #include <algorithm> #in

poj 2309 BST(数学题)

BST Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8440   Accepted: 5093 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 c

POJ 2309 BST

BST Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8565   Accepted: 5202 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 c

POJ 2309 BST 树状数组基本操作

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 t

poj 2309 BST 使用树状数组的lowbit

如果领悟了树状数组中的lowbit,这道题就是极其简单的,最底层都是奇数,用lowbit(x)寻找x的父亲,然后x的父亲-1就是最大数 至于lowbit是如何计算的嘛,寻找x的父亲,其实就是x+2^x的二进制末尾0的个数. #include<iostream> #include<stdio.h> using namespace std; typedef long long ll; ll lowbit(int x){ return x&(-x); } int main(){

Buy Tickets POJ - 2828 思维+线段树

Buy Tickets POJ - 2828 思维+线段树 题意 是说有n个人买票,但是呢这n个人都会去插队,问最后的队列是什么情况.插队的输入是两个数,第一个是前面有多少人,第二个是这个人的编号,最后输出编号就好了. 解题思路 这个题要倒着处理,因为最后一个人插队完成后,别人就不能影响他了.他前面有n个人,那么他就是n+1号位置上,这样来的话,我们只需要知道那个位置,他前面有n个人就行.默认每个位置都没有人. 详细看代码. 代码实现 #include<cstdio> #include<

poj1083 思维题

http://poj.org/problem?id=1083 Description The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in the following figure. The floor has 200 rooms each on the north side and south side along the corridor. Rec

Unique Encryption Keys (思维题 预处理)

题目 题意:给m个数字, q次询问, 询问b到e之间如果有重复数字就输出, 没有就输出OK 思路:用f[i]数组 记录从i开始向后最近的有重复数字的 位置, 如 1 3 2 2, 则f[1] = 4; 如果离a最近的重复数字的位置 都大于b, 就说明没有重复数字. f[]数组需要预处理,从后向前. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector>

sdut 2847 Monitor (思维题)

题目 题意:给定a, b, x, y;  求使c, d; 使c:d = x :y; 且c<=a, d<=b, 而且c, d尽量大. 先求最小倍数, 再用最小倍数乘 x, y; 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 using namespace std; 6 7 long long gcd(long long a, l