鉴于本题解书写时洛谷题面暂无中文翻译,为避免可能的歧义或困惑,先对本题解中的译法进行约定:(顺便吐槽音译怪)
- 英文题面中“mochi”或日文题面中“餅”译为“饼”。
- 英文题面中“kagamimochi”或日文题面中“鏡餅”译为“镜饼”。
鉴于本题是 C 和 E 的加强版,而我懒得去写那两题的题解,这里不加证明地给出那两题的做法:
- C:对于每个饼,双指针求出大小不超过其一半的最后一个饼的位置。
- E:二分答案 \(k\),check 是否前 \(k\) 个饼可以依次和后 \(k\) 个饼组合成镜饼。
朴素写法 E 的 check 是 \(O(n)\) 的,我们只要将其优化到 \(O(1)\),就可以直接套用过来通过本题。
这时候 C 就给了我们启发。我们预处理出 C 的位置数组 \(pos\),并定义下标差数组 \(dis_i=i-pos_i\)。容易发现,二分的答案为 \(k\) 时,E 的 check 等价于判断 \(\max\limits_{i=r-k+1}^{r}\{dis_i\}\le (r-k+1)-l\) 是否成立。使用 ST 表维护 \(dis\) 的区间最值即可。
时间复杂度 \(O((n+m)\log n)\)。
// Problem: G - Simultaneous Kagamimochi 2
// Contest: AtCoder - HHKBプログラミングコンテスト2025(AtCoder Beginner Contest 388)
// URL: https://atcoder.jp/contests/abc388/tasks/abc388_g
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)//By: OIer rui_er
#include <bits/stdc++.h>
#define rep(x, y, z) for(int x = (y); x <= (z); ++x)
#define per(x, y, z) for(int x = (y); x >= (z); --x)
#define debug(format...) fprintf(stderr, format)
#define fileIO(s) do {freopen(s".in", "r", stdin); freopen(s".out", "w", stdout);} while(false)
#define endl '\n'
using namespace std;
typedef long long ll;mt19937 rnd(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
int randint(int L, int R) {uniform_int_distribution<int> dist(L, R);return dist(rnd);
}template<typename T> void chkmin(T& x, T y) {if(y < x) x = y;}
template<typename T> void chkmax(T& x, T y) {if(x < y) x = y;}template<int mod>
inline unsigned int down(unsigned int x) {return x >= mod ? x - mod : x;
}template<int mod>
struct Modint {unsigned int x;Modint() = default;Modint(unsigned int x) : x(x) {}friend istream& operator>>(istream& in, Modint& a) {return in >> a.x;}friend ostream& operator<<(ostream& out, Modint a) {return out << a.x;}friend Modint operator+(Modint a, Modint b) {return down<mod>(a.x + b.x);}friend Modint operator-(Modint a, Modint b) {return down<mod>(a.x - b.x + mod);}friend Modint operator*(Modint a, Modint b) {return 1ULL * a.x * b.x % mod;}friend Modint operator/(Modint a, Modint b) {return a * ~b;}friend Modint operator^(Modint a, int b) {Modint ans = 1; for(; b; b >>= 1, a *= a) if(b & 1) ans *= a; return ans;}friend Modint operator~(Modint a) {return a ^ (mod - 2);}friend Modint operator-(Modint a) {return down<mod>(mod - a.x);}friend Modint& operator+=(Modint& a, Modint b) {return a = a + b;}friend Modint& operator-=(Modint& a, Modint b) {return a = a - b;}friend Modint& operator*=(Modint& a, Modint b) {return a = a * b;}friend Modint& operator/=(Modint& a, Modint b) {return a = a / b;}friend Modint& operator^=(Modint& a, int b) {return a = a ^ b;}friend Modint& operator++(Modint& a) {return a += 1;}friend Modint operator++(Modint& a, int) {Modint x = a; a += 1; return x;}friend Modint& operator--(Modint& a) {return a -= 1;}friend Modint operator--(Modint& a, int) {Modint x = a; a -= 1; return x;}friend bool operator==(Modint a, Modint b) {return a.x == b.x;}friend bool operator!=(Modint a, Modint b) {return !(a == b);}
};const int N = 5e5 + 5;int n, a[N], m, dis[N];struct SparseTable {int mx[18][N];void init(int* a, int n) {rep(i, 1, n) mx[0][i] = a[i];rep(j, 1, 17) rep(i, 1, n - (1 << j) + 1) mx[j][i] = max(mx[j - 1][i], mx[j - 1][i + (1 << (j - 1))]);}int ask(int l, int r) {if(l > r) return -2e9;int k = __lg(r - l + 1);return max(mx[k][l], mx[k][r - (1 << k) + 1]);}
}st;int main() {ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);cin >> n;rep(i, 1, n) cin >> a[i];cin >> m;int ptr = 0;rep(i, 1, n) {while(ptr < n && a[ptr + 1] * 2 <= a[i]) ++ptr;dis[i] = i - ptr;// cout << dis[i] << " \n"[i == n];}st.init(dis, n);rep(i, 1, m) {int ql, qr;cin >> ql >> qr;int l = 0, r = (qr - ql + 1) / 2 + 1;while(l < r) {int mid = (l + r) >> 1;if(st.ask(qr - mid + 1, qr) <= (qr - mid + 1) - ql) l = mid + 1;else r = mid;}cout << l - 1 << endl;}return 0;
}