제출 #806893

#제출 시각아이디문제언어결과실행 시간메모리
806893hugo_pm3단 점프 (JOI19_jumps)C++17
100 / 100
594 ms106744 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 Node {
    int maxRouge, maxBleu, maxSomme;
    void up() { chmax(maxSomme, maxRouge+maxBleu); }
};

Node op(Node a, Node b) {
    return {
        max(a.maxRouge, b.maxRouge),
        max(a.maxBleu, b.maxBleu),
        max(max(a.maxSomme, b.maxSomme), a.maxRouge + b.maxBleu)
    };
}
Node e = {0, 0, 0};
const int ROUGE = 0, BLEU = 1;
struct Segtree {
    vector<Node> tree;
    int N;
    Segtree(int _N) : N(_N) {
        tree.assign(2*N, e);
    }
    int query_include(int L, int R) {
        L += N, R += N+1;
        Node resl = e, resr = e;
        while (L < R) {
            if (L & 1) resl = op(resl, tree[L++]);
            if (R & 1) resr = op(tree[--R], resr);
            L /= 2, R /= 2;
        }
        return op(resl, resr).maxSomme;
    }
    void prop(int i, int coul, int x) {
        i += N;
        assert(0 <= i && i < 2*N);
        chmax(coul == ROUGE ? tree[i].maxRouge : tree[i].maxBleu, x);
        tree[i].up();
        while (i >= 1) {
            i /= 2;
            tree[i] = op(tree[2*i], tree[2*i+1]);
        }
    }
};

struct Req {
    int l, r, i;
};
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<vector<Req>> req(N);
    vector<int> ansReq(Q);
    rep(i, 0, Q) {
        int l, r;
        cin >> l >> r;
        --l, --r;
        req[l].push_back({l, r, i});
    }
    vector<int> stk;
    vector<vi> cand(N);
    rep(B, 0, N) {
        while (!stk.empty()) {
            int A = stk.back();
            cand[A].push_back(B);
            if (val[A] >= val[B]) break;
            stk.pop_back();
        }
        stk.push_back(B);
    }
    Segtree st(N+1);
    vector<pii> ptRouge, ptBleu;
    for (int i = N-1; i >= 0; --i) {
        //ptBleu.emplace_back(i, val[i]);
        st.prop(i, BLEU, val[i]);
        {
            int A = i;
            for (int B : cand[i]) {
                int suffC = min(B + (B-A), N);
                //ptRouge.emplace_back(suffC, val[A]+val[B]);
                st.prop(suffC, ROUGE, val[A]+val[B]);
            }
        }
        for (Req rq : req[i]) {
            ansReq[rq.i] = st.query_include(rq.l, rq.r);
        }
    }
    rep(i, 0, Q) {
        cout << ansReq[i] << '\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...