#include <bits/stdc++.h>
using namespace std;
constexpr int inf = 1e9 + 7;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
auto func = [&](int i, int j) {
return a[i] > a[j] ? i : j;
};
const int max_log = __lg(n) + bool(n & (n - 1));
vector<vector<int>> st(max_log);
st[0].resize(n);
iota(st[0].begin(), st[0].end(), 0);
for (int i = 1; i < max_log; ++i) {
st[i].resize(n - (1 << i) + 1);
for (int j = 0; j < (int) st[i].size(); ++j) {
st[i][j] = func(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);
}
}
vector<int> logn(n + 1);
for (int i = 2; i <= n; ++i) {
logn[i] = logn[i >> 1] + 1;
}
auto query = [&](int l, int r) {
int lg = logn[r - l];
return func(st[lg][l], st[lg][r - (1 << lg)]);
};
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
--l;
vector<int> p{l, r - 1};
for (int t = 0; t < r - l && p.size() < 32; ++t) {
int kek = -1;
for (int i = 0; i < int(p.size()) - 1; ++i) {
if (p[i + 1] != p[i] + 1) {
int d = query(p[i] + 1, p[i + 1]);
if (kek == -1 || a[d] > a[kek]) {
kek = d;
}
}
}
if (kek == -1) {
break;
}
p.insert(lower_bound(p.begin(), p.end(), kek), kek);
}
int ans = 0;
auto relax = [&](int i, int j) {
int R = j + (j - i);
if (R < r) {
ans = max(ans, a[i] + a[j] + a[query(R, r)]);
}
int len = j - i - 1;
if (len > 0) {
ans = max(ans, a[i] + a[j] + a[query(i + 1, i + 1 + ((len + 1) / 2))]);
}
int L = max(l, i - (j - i));
if (L < i) {
ans = max(ans, a[i] + a[j] + a[query(L, i)]);
}
};
vector<int> stk;
for (int i: p) {
while (!stk.empty() && a[stk.back()] <= a[i]) {
relax(stk.back(), i);
stk.pop_back();
}
if (!stk.empty()) {
relax(stk.back(), i);
}
stk.push_back(i);
}
cout << ans << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
2934 ms |
5928 KB |
Output is correct |
12 |
Correct |
974 ms |
5532 KB |
Output is correct |
13 |
Correct |
958 ms |
5580 KB |
Output is correct |
14 |
Correct |
2880 ms |
5732 KB |
Output is correct |
15 |
Correct |
2828 ms |
5692 KB |
Output is correct |
16 |
Incorrect |
2835 ms |
9576 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
14900 KB |
Output is correct |
2 |
Correct |
26 ms |
14924 KB |
Output is correct |
3 |
Correct |
24 ms |
14944 KB |
Output is correct |
4 |
Correct |
26 ms |
14936 KB |
Output is correct |
5 |
Correct |
27 ms |
14904 KB |
Output is correct |
6 |
Incorrect |
23 ms |
14940 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
2934 ms |
5928 KB |
Output is correct |
12 |
Correct |
974 ms |
5532 KB |
Output is correct |
13 |
Correct |
958 ms |
5580 KB |
Output is correct |
14 |
Correct |
2880 ms |
5732 KB |
Output is correct |
15 |
Correct |
2828 ms |
5692 KB |
Output is correct |
16 |
Incorrect |
2835 ms |
9576 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |