求条玄关
查看原帖
求条玄关
732034
Exscallop64_楼主2025/1/21 19:45
#include<bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 2e5 + 5, X = 19, INF = 1e9;

int n, q, a[N];

struct ST{
  int n, st[N][X], lg[N];

  void Init(int x){
    n = x;
    for(int i = 1; i <= n; i++){
      int r = lower_bound(a + 1, a + 1 + n, a[i] << 1) - a;
      st[i][0] = r - i;
    }
    for(int i = 1; i <= n; i++){
      for(int x = 1; i + (1 << x) - 1 <= n; x++){
        st[i][x] = max(st[i][x - 1], st[i + (1 << x - 1)][x - 1]);
      }
    }
    lg[1] = 0;
    for(int i = 2; i <= n; i++){
      lg[i] = lg[i >> 1] + 1;
    }
  }

  int Query(int l, int r){
    int k = lg[r - l + 1];
    return max(st[l][k], st[r - (1 << k) + 1][k]);
  }
}S;

bool Check(int L, int R, int k){
  return S.Query(L, L + k - 1) <= R - k - L + 1;
}

int BinarySearch(int L, int R){
  int l = 0, r = (R - L + 1) >> 1;
  while(l < r){
    int mid = (l + r + 1) >> 1;
    Check(L, R, mid) ? l = mid : r = mid - 1;
  }
  return l;
}

int main(){
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for(int i = 1; i <= n; i++){
    cin >> a[i];
  }
  S.Init(n);
  cin >> q;
  for(int l, r; q; q--){
    cin >> l >> r;
    cout << BinarySearch(l, r) << "\n";
  }
  return 0;
}
2025/1/21 19:45
加载中...