Submission #1082739

#TimeUsernameProblemLanguageResultExecution timeMemory
1082739TymondTriple Jump (JOI19_jumps)C++17
100 / 100
2476 ms111956 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define fi first
#define se second
#define vi vector<int>
#define vll vector<long long>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
inline int rand(int l,int r){return uniform_int_distribution<int>(l, r)(rng);}
inline ll rand(ll l,ll r){return uniform_int_distribution<ll>(l, r)(rng64);}
#ifdef DEBUG
auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

struct Node{
    ll r[3];
    Node(vll nr = {0LL, 0LL, 0LL}){
        r[0] = nr[0];
        r[1] = nr[1];
        r[2] = nr[2];
    }
};

const int MAXN = 5e5 + 7;
const ll MIN = -1000000000;
const int BASE = (1 << 19);
ll tab[MAXN];
Node tree[2 * BASE + 7];
ll lazy[2 * BASE + 7];
vector<pii> queries[MAXN];
ll ans[MAXN];
vi nxt[MAXN];
int n, q;
vll worst;

Node get(Node x, Node y){
    Node res = Node(worst);
    res.r[0] = max(x.r[0], y.r[0]);
    res.r[1] = max(x.r[1], y.r[1]);
    res.r[2] = max({x.r[2], y.r[2], (ll)x.r[0] + y.r[1]});
    return res;
}

void Push(int v){
    tree[2 * v].r[0] = max(tree[2 * v].r[0], lazy[v]);
    tree[2 * v + 1].r[0] = max(tree[2 * v + 1].r[0], lazy[v]);
    lazy[2 * v] = max(lazy[2 * v], lazy[v]);
    lazy[2 * v + 1] = max(lazy[2 * v + 1], lazy[v]);
    tree[2 * v].r[2] = max(tree[2 * v].r[2], lazy[v] + tree[2 * v].r[1]);
    tree[2 * v + 1].r[2] = max(tree[2 * v + 1].r[2], lazy[v] + tree[2 * v + 1].r[1]);
}

void upd(int v, int l, int p, int a, int b, ll val){
    if(p < a || b < l){
        return;
    }

    if(a <= l && p <= b){
        lazy[v] = max(lazy[v], val);
        tree[v].r[0] = max(tree[v].r[0], val);
        tree[v].r[2] = max(tree[v].r[2], val + tree[v].r[1]);
        return;
    }
    Push(v);
    int mid = (l + p) / 2;
    upd(2 * v, l, mid, a, b, val);
    upd(2 * v + 1, mid + 1, p, a, b, val);
    tree[v] = get(tree[2 * v], tree[2 * v + 1]);
}

Node query(int v, int l, int p, int a, int b){
    if(p < a || b < l){
        return worst;
    }

    if(a <= l && p <= b){
        return tree[v];
    }
    Push(v);
    int mid = (l + p) / 2;
    Node ret = query(2 * v, l, mid, a, b);
    ret = get(ret, query(2 * v + 1, mid + 1, p, a, b));
    tree[v] = get(tree[2 * v], tree[2 * v + 1]);
    return ret;
}

void init(){
    worst = {MIN, MIN, MIN};
    for(int i = 1; i < 2 * BASE + 7; i++){
        tree[i] = Node(worst);
    }

    for(int i = 1; i <= n; i++){
        tree[i + BASE].r[1] = tab[i];
    }

    for(int i = BASE - 1; i >= 1; i--){
        tree[i] = get(tree[2 *i], tree[2 * i + 1]);
    }
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> tab[i];
    }

    init();

    vi xd;
    for(int i = 1; i <= n; i++){
        while(sz(xd) && tab[xd.back()] <= tab[i]){
            nxt[xd.back()].pb(i);
            xd.pop_back();
        }

        if(sz(xd)){
            nxt[xd.back()].pb(i);
        }
        xd.pb(i);
    }

    cin >> q;
    for(int i = 1; i <= q; i++){
        int l, p;
        cin >> l >> p;
        queries[l].pb({p, i});
    }

    for(int i = n; i >= 1; i--){
        for(auto ele : nxt[i]){
            upd(1, 0, BASE - 1, 2 * ele - i, n, tab[i] + tab[ele]);
        }

        for(auto ele : queries[i]){
            ans[ele.se] = query(1, 0, BASE - 1, i, ele.fi).r[2];
        }
    }

    for(int i = 1; i <= q; i++){
        cout << ans[i] << '\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...