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;
#define int long long
#define INF 1000000000000000000
//#define INF 1000000000
#define MOD 998244353
struct node {
int s, e, m, v;
node *l, *r;
node(int _s, int _e) {
s = _s, e = _e, m = (s+e)/2, v = 0;
if(s != e) {
l = new node(s, m);
r = new node(m+1, e);
}
}
void update(int x, int val) {
if(s == e) {v = max(v, val); return;}
else if(x > m) r->update(x, val);
else l->update(x, val);
v = max(l->v, r->v);
}
int rmax(int x, int y) {
if(x <= s && e <= y) return v;
else if(x > m) return r->rmax(x, y);
else if(y <= m) return l->rmax(x, y);
else return max(l->rmax(x, y), r->rmax(x, y));
}
} *root;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, q, x, y;
cin >> n;
int a[n], mx[n+1][n+1], ans[n][n];
for(int i = 0; i < n; i++) cin >> a[i];
for(int j = 0; j < n; j++) {
mx[j][j] = a[j];
for(int i = j-1; i >= 0; i--) {
mx[i][j] = max(mx[i+1][j], a[i]);
}
for(int i = j+1; i <= n; i++) mx[i][j] = 0;
}
root = new node(0, n-1);
for(int i = 2; i < n; i++) {
for(int j = i-2; j >= 0; j--) {
root->update(j, a[j] + mx[j+1][(i+j)/2] + a[i]);
ans[j][i] = root->rmax(j, i);
}
}
cin >> q;
while(q--) {
cin >> x >> y;
x--; y--;
cout << ans[x][y] << '\n';
}
}
# | 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... |