Submission #1103922

#TimeUsernameProblemLanguageResultExecution timeMemory
1103922ntminTriple Jump (JOI19_jumps)C++14
19 / 100
4058 ms138280 KiB
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
#define ON(a, b) (a >> b & 1)
#define all(x) x.begin(), x.end()
template<typename T> bool maximise(T &a, T b){if(a < b){a = b; return 1;} return 0;}
template<typename T> bool minimise(T &a, T b){if(a > b){a = b; return 1;} return 0;}

const int N = 5e5 + 1;
int a[N], st[19][N], n, q;

const int M = 5010;
int maxx[M][M];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    cin >> n;
    for(int i = 1; i <= n; ++i){
        cin >> a[i];
        st[0][i] = a[i];
    }    

    for(int k = 1; k < 19; ++k){
        for(int i = 1; i + (1 << k) - 1 <= n; ++i){
            st[k][i] = max(st[k - 1][i], st[k - 1][i + (1 << (k - 1))]);
        }
    }

    auto getmax = [&](int l, int r) -> int {
        int k = __lg(r - l + 1);
        return max(st[k][l], st[k][r - (1 << k) + 1]);
    };

    for(int i = 1; i <= n; ++i){
        for(int j = i + 2; j <= n; ++j){
            maxx[i][j] = a[i] + a[j] + getmax(i + 1, i + ((j - i + 1) / 2 - ((j - i + 1) % 2 == 0)));
        }
    }

    for(int i = n; i >= 1; --i){
        for(int j = i + 1; j <= n; ++j){
            maximise(maxx[i][j], max({maxx[i + 1][j], maxx[i][j - 1], maxx[i + 1][j - 1]}));
        }
    }

    int q; cin >> q;
    while(q--){
        int l, r; cin >> l >> r;
        cout << maxx[l][r] << '\n';
    }

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...