답안 #612444

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
612444 2022-07-29T15:07:28 Z Jomnoi 3단 점프 (JOI19_jumps) C++17
100 / 100
1127 ms 91736 KB
#include <bits/stdc++.h>
using namespace std;

const int MAX_N = 5e5 + 5;
const int MAX_Q = 5e5 + 5;
const int INF = 1e9 + 7;

int A[MAX_N];
int L[MAX_Q], R[MAX_Q], ans[MAX_Q];
vector <int> queries[MAX_N], updates[MAX_N];

struct Node {
    int ans, maxL, maxR;

    Node() {}
    Node(int a, int ml, int mr) : ans(a), maxL(ml), maxR(mr) {}

    Node operator+(const Node &o) const {
        int ans_, maxL_, maxR_;
        ans_ = max({ans, o.ans, maxL + o.maxR});
        maxL_ = max(maxL, o.maxL);
        maxR_ = max(maxR, o.maxR);
        return Node(ans_, maxL_, maxR_);
    }
}tree[4 * MAX_N];

void build(int idx, int l, int r) {
    if(l == r) {
        return void(tree[idx] = Node(-INF, -INF, A[l]));
    }

    int mid = (l + r) / 2;
    build(idx * 2, l, mid);
    build(idx * 2 + 1, mid + 1, r);
    tree[idx] = tree[idx * 2] + tree[idx * 2 + 1];
}

void update(int idx, int l, int r, int q, int x) {
    if(l == r) {
        tree[idx].maxL = max(tree[idx].maxL, x);
        tree[idx].ans = max(tree[idx].ans, tree[idx].maxL + tree[idx].maxR);
        return;
    }

    int mid = (l + r) / 2;
    if(q <= mid) {
        update(idx * 2, l, mid, q, x);
    }
    else {
        update(idx * 2 + 1, mid + 1, r, q, x);
    }
    tree[idx] = tree[idx * 2] + tree[idx * 2 + 1];
}

Node query(int idx, int l, int r, int ql, int qr) {
    if(r < ql or qr < l) {
        return Node(-INF, -INF, -INF);
    }
    if(ql <= l and r <= qr) {
        return tree[idx];
    }

    int mid = (l + r) / 2;
    return query(idx * 2, l, mid, ql, qr) + query(idx * 2 + 1, mid + 1, r, ql, qr);
}

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int N;
    cin >> N;
    for(int i = 1; i <= N; i++) {
        cin >> A[i];
    }

    int Q;
    cin >> Q;
    for(int i = 1; i <= Q; i++) {
        cin >> L[i] >> R[i];

        queries[L[i]].push_back(i);
    }

    stack <int> stk;
    for(int i = 1; i <= N; i++) {
        while(!stk.empty() and A[stk.top()] <= A[i]) {
            updates[stk.top()].push_back(i);
            stk.pop();
        }
        if(!stk.empty()) {
            updates[stk.top()].push_back(i);
        }

        stk.push(i);
    }

    build(1, 1, N);
    for(int i = N; i >= 1; i--) {
        for(auto j : updates[i]) {
            if(2 * j - i <= N) {
                update(1, 1, N, 2 * j - i, A[i] + A[j]);
            }
        }
        for(auto j : queries[i]) {
            ans[j] = query(1, 1, N, L[j], R[j]).ans;
        }
    }

    for(int i = 1; i <= Q; i++) {
        cout << ans[i] << '\n';
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23788 KB Output is correct
2 Correct 15 ms 23816 KB Output is correct
3 Correct 13 ms 23796 KB Output is correct
4 Correct 13 ms 23764 KB Output is correct
5 Correct 13 ms 23764 KB Output is correct
6 Correct 13 ms 23828 KB Output is correct
7 Correct 14 ms 23764 KB Output is correct
8 Correct 15 ms 23804 KB Output is correct
9 Correct 13 ms 23820 KB Output is correct
10 Correct 13 ms 23764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23788 KB Output is correct
2 Correct 15 ms 23816 KB Output is correct
3 Correct 13 ms 23796 KB Output is correct
4 Correct 13 ms 23764 KB Output is correct
5 Correct 13 ms 23764 KB Output is correct
6 Correct 13 ms 23828 KB Output is correct
7 Correct 14 ms 23764 KB Output is correct
8 Correct 15 ms 23804 KB Output is correct
9 Correct 13 ms 23820 KB Output is correct
10 Correct 13 ms 23764 KB Output is correct
11 Correct 325 ms 43068 KB Output is correct
12 Correct 317 ms 43044 KB Output is correct
13 Correct 321 ms 43040 KB Output is correct
14 Correct 341 ms 43200 KB Output is correct
15 Correct 330 ms 43228 KB Output is correct
16 Correct 320 ms 42496 KB Output is correct
17 Correct 320 ms 42488 KB Output is correct
18 Correct 335 ms 42436 KB Output is correct
19 Correct 368 ms 42984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 130 ms 37188 KB Output is correct
2 Correct 73 ms 38772 KB Output is correct
3 Correct 75 ms 39592 KB Output is correct
4 Correct 133 ms 38996 KB Output is correct
5 Correct 133 ms 38964 KB Output is correct
6 Correct 147 ms 38336 KB Output is correct
7 Correct 132 ms 38220 KB Output is correct
8 Correct 126 ms 38228 KB Output is correct
9 Correct 135 ms 38480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23788 KB Output is correct
2 Correct 15 ms 23816 KB Output is correct
3 Correct 13 ms 23796 KB Output is correct
4 Correct 13 ms 23764 KB Output is correct
5 Correct 13 ms 23764 KB Output is correct
6 Correct 13 ms 23828 KB Output is correct
7 Correct 14 ms 23764 KB Output is correct
8 Correct 15 ms 23804 KB Output is correct
9 Correct 13 ms 23820 KB Output is correct
10 Correct 13 ms 23764 KB Output is correct
11 Correct 325 ms 43068 KB Output is correct
12 Correct 317 ms 43044 KB Output is correct
13 Correct 321 ms 43040 KB Output is correct
14 Correct 341 ms 43200 KB Output is correct
15 Correct 330 ms 43228 KB Output is correct
16 Correct 320 ms 42496 KB Output is correct
17 Correct 320 ms 42488 KB Output is correct
18 Correct 335 ms 42436 KB Output is correct
19 Correct 368 ms 42984 KB Output is correct
20 Correct 130 ms 37188 KB Output is correct
21 Correct 73 ms 38772 KB Output is correct
22 Correct 75 ms 39592 KB Output is correct
23 Correct 133 ms 38996 KB Output is correct
24 Correct 133 ms 38964 KB Output is correct
25 Correct 147 ms 38336 KB Output is correct
26 Correct 132 ms 38220 KB Output is correct
27 Correct 126 ms 38228 KB Output is correct
28 Correct 135 ms 38480 KB Output is correct
29 Correct 991 ms 85088 KB Output is correct
30 Correct 827 ms 84404 KB Output is correct
31 Correct 860 ms 86580 KB Output is correct
32 Correct 975 ms 85192 KB Output is correct
33 Correct 966 ms 85112 KB Output is correct
34 Correct 962 ms 82808 KB Output is correct
35 Correct 1002 ms 82440 KB Output is correct
36 Correct 1000 ms 82436 KB Output is correct
37 Correct 1127 ms 83912 KB Output is correct
38 Correct 811 ms 91736 KB Output is correct
39 Correct 726 ms 91656 KB Output is correct
40 Correct 712 ms 88316 KB Output is correct
41 Correct 694 ms 87924 KB Output is correct
42 Correct 675 ms 87896 KB Output is correct
43 Correct 690 ms 89564 KB Output is correct
44 Correct 746 ms 91048 KB Output is correct
45 Correct 736 ms 91096 KB Output is correct
46 Correct 738 ms 88012 KB Output is correct
47 Correct 744 ms 87488 KB Output is correct
48 Correct 721 ms 87496 KB Output is correct
49 Correct 725 ms 89608 KB Output is correct
50 Correct 810 ms 88876 KB Output is correct
51 Correct 800 ms 88872 KB Output is correct
52 Correct 837 ms 86292 KB Output is correct
53 Correct 804 ms 85960 KB Output is correct
54 Correct 804 ms 85972 KB Output is correct
55 Correct 805 ms 87764 KB Output is correct