题目
题意翻译
-
n n n组询问,每次给出一个区间 l , r l, r l,r,你需要输出在这个区间内二进制表示中1的个数最多的数
-
如有多个答案,输出最小的那个
-
( n ≤ 1 0 4 , 0 ≤ l , r ≤ 1 0 18 ) (n \leq10^4, 0\leq l, r \leq10^{18}) (n≤104,0≤l,r≤1018)
效果
nn组询问,每次给出一个区间l, rl,r,你需要输出在这个区间内二进制表示中1的个数最多的数
如有多个答案,输出最小的那个
(n \leq10^4, 0\leq l, r \leq10^{18})(n≤10
4
,0≤l,r≤10
18
)
题目描述
Let’s denote as the number of bits set (‘1’ bits) in the binary representation of the non-negative integer x x .
You are given multiple queries consisting of pairs of integers l l and r r . For each query, find the x x , such that l<=x<=r l<=x<=r , and is maximum possible. If there are multiple such numbers find the smallest of them.
输入输出格式
输入格式:
Let’s denote as the number of bits set (‘1’ bits) in the binary representation of the non-negative integer x x .
You are given multiple queries consisting of pairs of integers l l and r r . For each query, find the x x , such that l<=x<=r l<=x<=r , and is maximum possible. If there are multiple such numbers find the smallest of them.
输出格式:
For each query print the answer in a separate line.
输入输出样例
输入样例#1: 复制
3
1 2
2 4
1 10
输出样例#1: 复制
1
3
7
说明
Let’s denote as the number of bits set (‘1’ bits) in the binary representation of the non-negative integer x x .
You are given multiple queries consisting of pairs of integers l l and r r . For each query, find the x x , such that l<=x<=r l<=x<=r , and is maximum possible. If there are multiple such numbers find the smallest of them.
思路
很明显是贪心。
我们很明显发现在位权小的位置放1能够使得最终结果最少。
做法:
初始的时候是1
于是我们就从位0到inf放1,如果>r就break
代码
#include <bits/stdc++.h>
using namespace std ;
#define int long long
int n,l,r ;
signed main(){
scanf("%lld",&n) ;
while(n--){
scanf("%lld%lld",&l,&r) ;
for (int i=0;;i++){
if ((l|(1ll<<i))>r) break ;
l|=(1ll<<i) ;
}
printf("%lld\n",l) ;
}
return 0 ;
}