题目: LINK
给n个询问,每个询问给出l, r 求出[l,r]内数字写成2进制形式,含有1的个数最多的数字是哪个,如果有多个输出值最小的那个。
F(l, r) 定义为[l, r]区间内题目要求的结果
if l == r , F(l, r) = l;
2^x为不超过r的最大值 如果 l< 2^x <= r ,那么最大值为2^(x+1)-1 (如果这个值在范围内的话),否则为2^x - 1;
如果2^x <= l, 最大值为 F(l - 2^x, r - 2^x) + 2^x
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std;
#define INF 1000000000
typedef __int64 LL;
int n;
LL l, r;
LL sol(LL l, LL r) {
LL ret = 0;
if(l == r ) return l;
LL tmp = 1;
while(tmp <= r) tmp <<= 1;
tmp >>= 1;
LL nn = tmp << 1; nn --;
if(tmp <= l) {
return sol(l - tmp, r - tmp) + tmp;
}
else if(nn >= l && r >= nn) return nn;
else return tmp-1;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE
scanf("%d", &n);
while(n--) {
cin>>l>>r;
cout<<sol(l, r)<<endl;
}
return 0;
}