Segment Tree
时间限制: 1 Sec 内存限制: 512 MB
提交: 107 解决: 23
[提交] [状态] [命题人:admin]
题目描述
Mcginn opens the code which he wrote 25 years ago.
Clever Mcginn wants to know how many positive interger n satisfied that the maximum c can reach whencall function build(1 , 1 , n) in the main function is m.
There are T test cases. Each case contains one integer m.
输入
Input is given from Standard Input in the following format:
T
m1
m2
.
.
.
mT
Constraints
1 ≤ T ≤ 100
1 ≤ m ≤ 1018
输出
For each m print one line denotes the answer.
样例输入
复制样例数据
3
3
4
5
样例输出
1
0
1
题目大意:
先输入一个整数t,其下t行每行输入一个整数m,代表的是线段树的最后一个节点的数值,问有多少范围满足这种线段树。
解题思路:
首先我们可以知道,对于节点x,在线段树中其左节点为x*2,右节点为x*2+1,并且左子数包含的点等于右子树包含的点数或者等于右子树包含的点数加一,所以对于每一个节点,用l代表此节点包含的最小能包含几个数,r代表此节点包含的最多能包含几个数,c代表当前节点的层数。
以57为例:
代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
int main()
{#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endif//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);int t;cin>>t;while(t--) {ll m;cin>>m;if(m%2==0) cout<<0<<endl;else {bool ju=false;ll l,r,x,c;l=1LL;r=1LL;x=m;c=0;while(x>1) {ll l1,l2,r1,r2;/*cout<<l<<" "<<r<<" "<<x<<" "<<c<<endl;*/if(x&1) {l2=l;r2=r;if(c==0) {l1=1;r1=1;}else {l1=l;r1=min(r2+1,1LL<<c);if(r1<l1) {ju=true;break;}}l=l1+l2;r=r1+r2;if(r<l) {ju=true;break;}x=(x-1)>>1;c++;}else {l1=l;r1=r;if(c==0) {l2=0;r2=0;}else {if(l==1) l2=1;else l2=l-1;r2=min(r1,1LL<<(c-1));if(r2<l2) {ju=true;break;}}l=l1+l2;r=r1+r2;if(r<l) {ju=true;break;}x=x>>1; c++;}}if(ju) cout<<0<<endl;else cout<<r-l+1<<endl;}}return 0;
}