Submission #806822

#TimeUsernameProblemLanguageResultExecution timeMemory
806822hugo_pmTriple Jump (JOI19_jumps)C++17
46 / 100
4053 ms221724 KiB
#include <bits/stdc++.h>
#define int long long
using namespace std;

#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define sz(v) ((int)((v).size()))

template<typename T>
void chmax(T &x, const T &v) { if (x < v) x = v; }
template<typename T>
void chmin(T &x, const T &v) { if (x > v) x = v; }

using pii = pair<int, int>;
using vi = vector<int>;

string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
	bool first = true;
	string res = "[";
	for (const auto &x : v) {
		if (!first)
			res += ", ";
		first = false;
		res += to_string(x);
	}
	res += "]";
	return res;
}

template <typename val, typename B>
string to_string(pair<val, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
	cout << ' ' << to_string(H);
	dbg_out(T...);
}

#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

struct Segtree {
    #define op max
    const int e = -1e18;
    vector<int> tree;
    int N;
    Segtree(vector<int> v) {
        N = v.size();
        tree.resize(2*N);
        for (int i = 2*N-1; i >= 1; --i) {
            tree[i] = (i >= N ? v[i-N] : op(tree[2*i], tree[2*i+1]));
        }
    }
    int query_include(int L, int R) {
        L += N, R += N+1;
        int res = e;
        while (L < R) {
            if (L & 1) res = op(res, tree[L++]);
            if (R & 1) res = op(res, tree[--R]);
            L /= 2, R /= 2;
        }
        return res;
    }
    #undef op
};

void s3(int N, int Q, vector<int> val, int lq, int rq) {
    vector<int> stk;
    vector<pii> cand;
    rep(B, 0, N) {
        while (!stk.empty()) {
            int A = stk.back();
            cand.emplace_back(A, B);
            if (val[A] >= val[B]) break;
            stk.pop_back();
        }
        stk.push_back(B);
    }
    Segtree st(val);
    int ans = -1e18;
    for (auto [A, B] : cand) if (lq <= A) {
        int suffC = B + (B-A);
        chmax(ans, val[A] + val[B] + st.query_include(suffC, rq));
    }
    cout << ans << '\n';
}
void s2(int N, int Q, vector<int> val, vector<pii> req) {
    vector<vi> dp(N, vi(N));
    for (int A = N-1; A >= 0; --A) {
        int B = A+1, maxMid = 0;
        rep(C, A+2, N) {
            while(B < N && B-A <= C-B) {
                chmax(maxMid, val[B++]);
            }
            dp[A][C] = val[A] + maxMid + val[C];
            if (A < C) {
                chmax(dp[A][C], max(dp[A+1][C], dp[A][C-1]));
            }
        }
    }
    for (auto [A, C] : req) {
        cout << dp[A][C] << '\n';
    }
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
    int N; cin >> N;
    vector<int> val(N);
    for (int &x : val) cin >>x;
    int Q; cin >> Q;
    vector<pii> req(Q);
    for (auto &[l, r] : req) {
        cin >> l >> r;
        --l, --r;
    }
    if (N <= 5000) {
        s2(N, Q, val, req);
    } else {
        for (auto [l,r] : req) {
            s3(N, Q, val, l, r);
        }
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...