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- l ≤ x ≤ R, and is maximum Possibl E. If there is 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 II integers li, ri -t He arguments for the corresponding query (0≤ li ≤ ri ≤101 8).
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 ten are listed below:
1= 12
2=2
3=2
4=2
5= 1012
6=2
7= 1112
8=2
9= 10012
Ten= 10102
Test Instructions : To n inquiries, each time you ask the number between l,r, in the binary under the 1-digit number of which is the most
Solution: We build from the L, in 0-bit, greedy from small to large-bit, must be the best
///1085422276#include<bits/stdc++.h>using namespacestd;#pragmaComment (linker, "/stack:102400000,102400000")using namespacestd; typedefLong Longll;typedef unsignedLong Longull;#defineMem (a) memset (A,0,sizeof (a))#definePB Push_backinline ll read () {ll x=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){ if(ch=='-') f=-1; ch=GetChar (); } while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar (); }returnx*F;}//****************************************Const intn=6000+ -;#defineMoD 10000007#defineINF 1000000001#defineMAXN 10000intd[ the];ll Test (ll X,ll y) { for(intI=0;i< the; i++) { if(! (x& (1ll<<i)) &&x+ (1ll<<i) <=y) x+ = (1ll<<i); } returnx;}intMain () {intn=read (); ll L,r; for(intI=1; i<=n;i++) {cin>>l>>R; cout<<test (l,r) <<Endl; } return 0;}Code
Codeforces Round #276 (Div. 1) A. Bits Greedy