Submission #1161600

#TimeUsernameProblemLanguageResultExecution timeMemory
1161600hahahoang132Triple Jump (JOI19_jumps)C++20
32 / 100
4091 ms25528 KiB
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const ll N = 5e5 + 5;
const ll mod = 1e9 + 7;
const ll inf = LLONG_MAX/5;
#define bit(x,y) ((x >> y) & 1)
ll a[N];
struct segtree
{
    ll n;
    ll b[N * 4];
    void build(ll n)
    {
        this->n = n;
        build(0,1,n);
    }
    void build(ll x, ll l, ll r)
    {
        if(l == r)
        {
            b[x] = a[l];
        }else
        {
            ll mid = (l + r) / 2;
            build(x * 2 + 1,l,mid);
            build(x * 2 + 2,mid + 1,r);
            b[x] = max(b[x * 2 + 1],b[x * 2 + 2]);
        }
    }
    ll get(ll l, ll r)
    {
        return get(0,1,n,l,r);
    }
    ll get(ll x, ll l, ll r, ll s, ll e)
    {
        if(s <= l && r <= e) return b[x];
        if(r < s || e < l) return -inf;
        ll mid = (l + r) / 2;
        return max(get(x * 2 + 1,l,mid,s,e),get(x * 2 + 2,mid + 1,r,s,e));
    }
};
segtree f;
vector<ll> b[N];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ll n;
    ll i,j;
    cin >> n;
    for(i = 1;i <= n;i++) cin >> a[i];
    stack<ll> st;
    for(i = 1;i <= n;i++)
    {
        while(!st.empty() && a[st.top()] <= a[i])
        {
            b[i].push_back(st.top());
            st.pop();
        }
        if(!st.empty()) b[i].push_back(st.top());
        st.push(i);
    }
    f.build(n);
    ll q;
    cin >> q;
    while(q--)
    {
        ll l,r;
        cin >> l >> r;
        ll i,j;
        ll ans = 0;
        for(i = l;i <= r;i++)
        {
            for(auto s : b[i])
            {
                ll e = 2 * i - s;
                if(l <= s && e <= r)
                {
                    ans = max(ans,a[s] + a[i] + f.get(e,r));
                }
            }
        }
        cout << ans << "\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...