Submission #1134761

#TimeUsernameProblemLanguageResultExecution timeMemory
1134761domblyTriple Jump (JOI19_jumps)C++20
0 / 100
4094 ms6472 KiB
#include <bits/stdc++.h>
#define int long long
#define F first
#define S second
#define pb push_back
using namespace std;
const int N = 1e5 + 10;
const int inf = 1e15;
const int mod = 1e9 + 7;

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n,q;
    cin >> n;
    vector<int>a(n + 1),l(n + 1),r(n + 1);
    for(int i = 1; i <= n; i++) cin >> a[i];
    stack<int>st;
    for(int i = 1; i <= n; i++) {
        while(!st.empty() && a[st.top()] <= a[i]) st.pop();
        l[i] = (st.empty() ? -1 : st.top());
        st.push(i);
    }
    while(!st.empty()) st.pop();
    for(int i = n; i >= 1; i--) {
        while(!st.empty() && a[st.top()] <= a[i]) st.pop();
        r[i] = (st.empty() ? n + 1 : st.top());
        st.push(i);
    }
    cin >> q;
    while(q--) {
        int ll,rr;
        cin >> ll >> rr;
        vector<int>res(n + 1,0);
        for(int i = ll; i <= rr; i++) {
            if(l[i] >= ll) {
                for(int j = 2 * i - l[i]; j <= n; j++) res[j] = max(res[j],a[l[i]] + a[i]);
            }
            if(r[i] <= rr) {
                for(int j = 2 * r[i] - i; j <= n; j++) res[j] = max(res[j],a[r[i]] + a[i]);
            }
        }
        int ans = 0;
        for(int i = ll; i <= rr; i++) ans = max(ans,res[i] + a[i]);
        cout << ans << "\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...