Codeforces Round #276 (Div. 1) A. Bits 贪心

A. Bits
 

Let's denote as  the number of bits set ('1' bits) in the binary representation of the non-negative integer x.

You are given multiple queries consisting of pairs of integers l and r. For each query, find the x, such that l ≤ x ≤ r, and is maximum possible. If there are multiple such numbers find the smallest of them.

Input

The first line contains integer n — the number of queries (1 ≤ n ≤ 10000).

Each of the following n lines contain two integers li, ri — the arguments for the corresponding query (0 ≤ li ≤ ri ≤ 1018).

Output

For each query print the answer in a separate line.

Sample test(s)
input
3
1 2
2 4
1 10
output
1
3
7
Note

The binary representations of numbers from 1 to 10 are listed below:

110 = 12

210 = 102

310 = 112

410 = 1002

510 = 1012

610 = 1102

710 = 1112

810 = 10002

910 = 10012

1010 = 10102

 题意:给n个询问,每次询问你l,r之间的数,在二进制下1位数最多的是哪个数

题解:我们从l向上构造,在0位补齐,贪心从小位到大位的补齐,一定是最优

///1085422276

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std ;
typedef long long ll;
typedef unsigned long long ull;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')f=-1;ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-'0';ch=getchar();
    }return x*f;
}
//****************************************
const int  N=6000+50;
#define mod 10000007
#define inf 1000000001
#define maxn 10000

int d[5000];
ll test(ll x,ll y) {
     for(int i=0;i<63;i++) {
        if(!(x&(1ll<<i))&&x+(1ll<<i)<=y)
          x+=(1ll<<i);
     }
    return x;
}
int main() {
    int n=read();ll l,r;
    for(int i=1;i<=n;i++) {
       cin>>l>>r;
        cout<<test(l,r)<<endl;
    }
    return 0;
}
代码

 

posted @ 2015-11-14 21:20  meekyan  阅读(318)  评论(0编辑  收藏  举报