思路:异或值最大,我们就让每一位不同的尽可能多,那么给你区间l,r我们将l,r变成成2进制高位补齐,为了不超过L-R区间从高位向低位遍历时,遇到相同的我们就继续走,如果遇到某一位不同那说明从这一位开始后面的每一位0,1我们可以任选,使得从当前位置到后面的每一位异或后变成1,此时值最大
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include<iostream>
#include<vector>
#include<queue>
//#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define SIS std::ios::sync_with_stdio(false)
#define space putchar(' ')
#define enter putchar('\n')
#define lson root<<1
#define rson root<<1|1
typedef pair<int,int> PII;
const int mod=1e9+7;
const int N=2e6+10;
const int M=1e5+10;
const int inf=0x7f7f7f7f;
const int maxx=2e5+7;
ll gcd(ll a,ll b)
{
return b==0?a:gcd(b,a%b);
}
ll lcm(ll a,ll b)
{
return a*(b/gcd(a,b));
}
template <class T>
void read(T &x)
{
char c;
bool op = 0;
while(c = getchar(), c < '0' || c > '9')
if(c == '-')
op = 1;
x = c - '0';
while(c = getchar(), c >= '0' && c <= '9')
x = x * 10 + c - '0';
if(op)
x = -x;
}
template <class T>
void write(T x)
{
if(x < 0)
x = -x, putchar('-');
if(x >= 10)
write(x / 10);
putchar('0' + x % 10);
}
ll qsm(int a,int b,int p)
{
ll res=1%p;
while(b)
{
if(b&1) res=res*a%p;
a=1ll*a*a%p;
b>>=1;
}
return res;
}
int main()
{
int t;
read(t);
while(t--)
{
ll l,r;
int cnt=-1;
read(l),read(r);
for(int i=63;i>=0;i--)
{
if((l>>i)!=(r>>i)){
cnt=i;
break;
}
}
ll ans=(1ll<<(cnt+1))-1;
write(ans);
enter;
}
return 0;
}
/*
1
3 2
1 1 2 3 4
1 0 1 2 3
2 1 2 3 4
*/

京公网安备 11010502036488号