兔子的区间密码

题目地址:

https://ac.nowcoder.com/acm/problem/20860

基本思路:

结论题,我们可以知道区间异或最大值的二进制位数等于区间两端点异或值的二进制位数,
并且我们将它二进制位上的每一位都换成(能证明一定能做到),就能得到区间异或最大值了。

参考代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
#define IO std::ios::sync_with_stdio(false); cin.tie(0)
#define ll long long
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, l, r) for (int i = l; i >= r; i--)
#define mset(s, _) memset(s, _, sizeof(s))
#define pb push_back
#define pii pair <int, int>
#define mp(a, b) make_pair(a, b)
#define INF (int)1e18

inline ll read() {
  ll x = 0, neg = 1; char op = getchar();
  while (!isdigit(op)) { if (op == '-') neg = -1; op = getchar(); }
  while (isdigit(op)) { x = 10 * x + op - '0'; op = getchar(); }
  return neg * x;
}
inline void print(ll x) {
  if (x < 0) { putchar('-'); x = -x; }
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}

ll l,r;
signed main() {
  IO;
  int t = read();
  while (t--) {
    l = read(), r = read();
    ll tmp = l ^r;
    ll ans = 0;
    while (tmp) {
      ans = ans * 2ll + 1ll;
      tmp >>= 1ll;
    }
    cout << ans << '\n';
  }
  return 0;
}