#include <iostream>
#include <vector>
#include <algorithm>
#define int long long
const int inf = 1e17;
using namespace std;
vector<vector<int>> st;
vector<int> lg2;
int rmx(int l, int r) {
if (l > r) return -inf;
int lg = lg2[(r - l + 1)];
return max(st[lg][l], st[lg][r - (1ll << lg) + 1]);
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n; cin >> n;
vector<int> a(n);
for (auto& x : a) cin >> x;
lg2.resize(n + 1);
for (int i = 2; i <= n; ++i)
lg2[i] = lg2[i >> 1] + 1;
st.resize(lg2[n] + 1, vector<int>(n, -inf));
for (int i = 0; i < n; ++i) st[0][i] = a[i];
for (int j = 1; j <= lg2[n]; ++j)
for (int i = 0; i < n; ++i)
if((i + (1ll << (j - 1))) < n)
st[j][i] = max(st[j - 1][i], st[j - 1][i + (1ll << (j - 1))]);
int q; cin >> q;
while (q--) {
int l, r, ans = 0; cin >> l >> r; --l, --r;
vector<pair<int, int>> arr;
for (int i = l; i <= r; ++i)
arr.push_back({ a[i], i });
sort(arr.begin(), arr.end(), greater<pair<int, int>>());
for (int j = 1; j < min((int)arr.size(), 100000ll); ++j)
for (int i = 0; i < j; ++i) {
int x = min(arr[i].second, arr[j].second);
int y = max(arr[i].second, arr[j].second);
int mx = rmx(y + (y - x), r);
ans = max(ans, arr[i].first + arr[j].first + mx);
}
cout << ans << endl;
}
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |