Submission #945177

#TimeUsernameProblemLanguageResultExecution timeMemory
945177hmm789Triple Jump (JOI19_jumps)C++14
19 / 100
2220 ms524288 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...