Advertisement
Md_Ahad

Untitled

Jul 1st, 2025
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long int ll;
  4. const ll N = 1e5+7;
  5. ll arr[N], cnt[N][25];
  6.  
  7. ll andValue(ll l, ll r) {
  8.     ll ans = 0;
  9.     for (ll j = 20; j >= 0; j--) {
  10.         if (cnt[r][j]-cnt[l-1][j] == r-l+1) ans += (1LL << j);
  11.     }
  12.     return ans;
  13. }
  14.  
  15. signed main() {
  16.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  17.     ll t = 1; cin >> t;
  18.  
  19.     while (t--) {
  20.         ll n, q; cin >> n >> q;
  21.  
  22.         for (ll i = 1; i <= n; i++) {
  23.             cin >> arr[i];
  24.             for (ll j = 20; j >= 0; j--) {
  25.                 if (arr[i] & (1LL << j)) cnt[i][j] = 1;
  26.             }
  27.             for (ll j = 20; j >= 0; j--) cnt[i][j] += cnt[i-1][j];            
  28.         }
  29.  
  30.         while (q--) {
  31.             ll l, r; cin >> l >> r;
  32.  
  33.             ll curr = l, ans = 0;
  34.             while (curr < r) {
  35.                 ll low = curr, high = r-1, indx = curr;
  36.  
  37.                 while (low <= high) {
  38.                     ll mid = low + (high-low) / 2;
  39.                     if (andValue(curr, mid) == andValue(l, curr)) {
  40.                         indx = mid;
  41.                         low = mid+1;
  42.                     }
  43.                     else high = mid-1;
  44.                 }
  45.    
  46.                 ans = max(ans, andValue(l, indx)+andValue(indx+1, r));
  47.                 curr = indx+1;
  48.             }
  49.            
  50.             cout << ans << "\n";
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement