#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define ld long double
#define pii pair<int, int>
#define f first
#define s second
#define boost() cin.tie(0), cin.sync_with_stdio(0)
const int MN = 500005;
int n, q, a[MN], l, r, st[MN * 4];
void build(int l, int r, int idx) {
if (l == r) {st[idx] = a[l]; return;}
int mid = (l + r) >> 1;
build(l, mid, idx * 2), build(mid + 1, r, idx * 2 + 1);
st[idx] = max(st[idx * 2], st[idx * 2 + 1]);
}
int qry(int l, int r, int x, int y, int idx) {
if (r < x || l > y || r < l) return -0x3f3f3f3f;
if (r <= y && l >= x) return st[idx];
int mid = (l + r) >> 1;
return max(qry(l, mid, x, y, idx * 2), qry(mid + 1, r, x, y, idx * 2 + 1));
}
int twos(int j, int i, int l, int r) {
if (j > i) swap(i, j);
int ret = 0;
int lo = max(l, j - (i - j));
ret = max(ret, qry(1, n, lo, j - 1, 1) + a[j] + a[i]);
int hi = i + (i - j);
ret = max(ret, qry(1, n, hi, r, 1) + a[j] + a[i]);
int mid = (j + i) / 2;
ret = max(ret, qry(1, n, j + 1, mid, 1) + a[j] + a[i]);
return ret;
}
int check(int i, int l, int r) {
int ret = 0;
for (int j = l; j <= r; j++) {
if (j != i) ret = max(ret, twos(i, j, l, r));
}
return ret;
}
int32_t main() {
boost();
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
build(1, n, 1);
cin >> q;
while (q--) {
cin >> l >> r;
vector<pii> v;
for (int i = l; i <= r; i++) v.push_back({a[i], i});
sort(v.begin(), v.end(), greater<>());
int ans = 0;
for (int i = 0; i < min((int) v.size(), 20ll); i++) {
ans = max(ans, check(v[i].s, l, r));
}
printf("%lld\n", ans);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
14 ms |
340 KB |
Output is correct |
3 |
Correct |
16 ms |
348 KB |
Output is correct |
4 |
Correct |
14 ms |
344 KB |
Output is correct |
5 |
Correct |
14 ms |
340 KB |
Output is correct |
6 |
Correct |
14 ms |
344 KB |
Output is correct |
7 |
Correct |
14 ms |
340 KB |
Output is correct |
8 |
Correct |
13 ms |
340 KB |
Output is correct |
9 |
Correct |
16 ms |
340 KB |
Output is correct |
10 |
Correct |
13 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
14 ms |
340 KB |
Output is correct |
3 |
Correct |
16 ms |
348 KB |
Output is correct |
4 |
Correct |
14 ms |
344 KB |
Output is correct |
5 |
Correct |
14 ms |
340 KB |
Output is correct |
6 |
Correct |
14 ms |
344 KB |
Output is correct |
7 |
Correct |
14 ms |
340 KB |
Output is correct |
8 |
Correct |
13 ms |
340 KB |
Output is correct |
9 |
Correct |
16 ms |
340 KB |
Output is correct |
10 |
Correct |
13 ms |
344 KB |
Output is correct |
11 |
Execution timed out |
4074 ms |
1000 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1907 ms |
11972 KB |
Output is correct |
2 |
Correct |
1691 ms |
11968 KB |
Output is correct |
3 |
Correct |
1625 ms |
11896 KB |
Output is correct |
4 |
Correct |
1698 ms |
11972 KB |
Output is correct |
5 |
Correct |
1763 ms |
11976 KB |
Output is correct |
6 |
Correct |
1840 ms |
11332 KB |
Output is correct |
7 |
Correct |
1952 ms |
11204 KB |
Output is correct |
8 |
Correct |
1921 ms |
11204 KB |
Output is correct |
9 |
Correct |
2045 ms |
11468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
14 ms |
340 KB |
Output is correct |
3 |
Correct |
16 ms |
348 KB |
Output is correct |
4 |
Correct |
14 ms |
344 KB |
Output is correct |
5 |
Correct |
14 ms |
340 KB |
Output is correct |
6 |
Correct |
14 ms |
344 KB |
Output is correct |
7 |
Correct |
14 ms |
340 KB |
Output is correct |
8 |
Correct |
13 ms |
340 KB |
Output is correct |
9 |
Correct |
16 ms |
340 KB |
Output is correct |
10 |
Correct |
13 ms |
344 KB |
Output is correct |
11 |
Execution timed out |
4074 ms |
1000 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |