传送门:https://codeforces.com/problemset/problem/485/C
Description
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 l i, r i — the arguments for the corresponding query (0 ≤ l i ≤ r i ≤ 1018).
Output
For each query print the answer in a separate line.
Input
3
1 2
2 4
1 10
Output
1
3
7
思路:
主要运用到了一种方法,或运算。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <string>
#include <sstream>
#include <stack>
#include <queue>
#include <map>
using namespace std;
const int inf=0x3f3f3f3f;
int n;
long long l,r;
int main()
{
cin>>n;
while(n--){
cin>>l>>r;
int index=1,temp=l,cnt=0;
while((l|l+1)<=r) {
l|=(l+1);
}
cout<<l<<endl;
}
return 0;
}