You can directly construct the answer to this question.
For the binary representation of l, change it from right to left to 1. If it cannot be changed again (then l is greater than r), the answer is l.
This method ensures that the answer is greater than or equal to l and less than or equal to r, and that the value of 1 in binary representation is the most.
# Include <iostream> # include <cstdio> # include <cstdlib> # include <cstring> # include <string> # include <cmath> # include <map> # include <set> # include <vector> # include <algorithm> # include <stack> # include <queue> # include <cctype> # include <sstream> using namespace std; # define pii pair <int, int> # define LL long intconst int eps = 1e-8; const int INF = 1000000000; const int maxn = 10000 + 5; int n; LL l, r, p; int main () {// freopen ("in9.txt", "r", stdin); // freopen ("out.txt", "w", stdout ); scanf ("% d", & n); while (n --) {scanf ("% I64d % I64d", & l, & r); p = 1; while (1) {if (l | p)> r) {printf ("% I64d \ n", l); break;} else {l | = p; p * = 2 ;}}// fclose (stdin); // fclose (stdout); return 0 ;}
Codeforces Round #276 (Div. 2) C. Bits (constructor)