This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
vector<int> A;
struct node {
int l, r, m, v;
node *lc, *rc;
node(int _l, int _r): l(_l), r(_r) {
m = (l + r) >> 1;
if (l == r) {
v = A[l];
} else {
lc = new node(l, m);
rc = new node(m + 1, r);
v = max(lc -> v, rc -> v);
}
}
int query(int _l, int _r) {
if (l == _l && r == _r) {
return v;
} else if (_r <= m) {
return lc -> query(_l, _r);
} else if (_l > m) {
return rc -> query(_l, _r);
} else {
return max(lc -> query(_l, m), rc -> query(m + 1, _r));
}
}
} *root;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int N, Q, L, R, S;
cin >> N;
A.resize(N + 5);
for (int i = 1; i <= N; i++) cin >> A[i];
root = new node(1, N);
cin >> Q;
while (Q--) {
S = 0;
cin >> L >> R;
for (int l = L; l + 2 <= R; l++) {
for (int r = l + 2; r <= R; r++) {
S = max(S, A[l] + A[r] + root -> query(l + 1, (l + r) >> 1));
}
}
cout << S << '\n';
}
return 0;
}
# | 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... |