제출 #1331839

#제출 시각아이디문제언어결과실행 시간메모리
1331839kawhiet3단 점프 (JOI19_jumps)C++20
0 / 100
17 ms3392 KiB
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<int> stk;
    vector<array<int, 2>> pos;
    for (int i = 0; i < n; i++) {
        while (!stk.empty() && a[stk.back()] < a[i]) {
            stk.pop_back();
        }
        if (!stk.empty()) {
            pos.push_back({stk.back(), i});
        }
        stk.push_back(i);
    }
    vector<int> s(n + 1);
    for (int i = n - 1; i >= 0; i--) {
        s[i] = max(s[i + 1], a[i]);
    }
    int q;
    cin >> q;
    while (q--) {
        int l, r;
        cin >> l >> r;
        l--; r--;
        int ans = 0;
        for (auto [i, j] : pos) {
            int k = 2 * j - i;
            if (k > j && k < n && l <= i && k <= r) {
                ans = max(ans, a[i] + a[j] + s[k]);
            }
        }
        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...