Beingamanforever

Range OR

Dec 25th, 2024
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. /**
  2.  *    author:  compounding
  3.  *    created: 2024-12-25 16:55:49
  4.  **/
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
  8. #define NeedForSpeed                  \
  9.     ios_base::sync_with_stdio(false); \
  10.     cin.tie(NULL);                    \
  11.     cout.tie(NULL);
  12. #define int long long
  13. #define all(x) (x).begin(), (x).end()
  14. typedef vector<int> vi;
  15. typedef vector<bool> vb;
  16. typedef vector<vi> vvi;
  17. typedef vector<pair<int, int>> vpi;
  18. #define f first
  19. #define s second
  20. #define yes cout << "YES" << endl
  21. #define no cout << "NO" << endl
  22. #define endl "\n"
  23. const int mod = 1000000007;
  24. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  25. void solve()
  26. {
  27.     // Range OR from l to r
  28.     int n, m;
  29.     cin >> n >> m;
  30.     int l = max(0LL, n - m), r = n + m, ans = 0;
  31.     // consider each bit separately
  32.     for (int i = 0; i < 32; i++)
  33.     {
  34.         int bit = 1LL << i;
  35.         // means it has been flipped once (r - l ) >= bit, if the difference is more than the power of 2
  36.         if ((l & bit) || (r & bit) || (r - l) >= bit)
  37.         {
  38.             ans |= bit;
  39.         }
  40.     }
  41.     cout << ans << endl;
  42.     return;
  43. }
  44.  
  45. signed main()
  46. {
  47.     NeedForSpeed;
  48.     int t = 1;
  49.     cin >> t;
  50.     while (t--)
  51.     {
  52.         solve();
  53.     }
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment