Submission #1064681

# Submission time Handle Problem Language Result Execution time Memory
1064681 2024-08-18T16:45:15 Z Sharky Triple Jump (JOI19_jumps) C++17
100 / 100
711 ms 90452 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 5e5 + 8;
int n, q, a[N], ans[N], mx[N];
vector<int> pr[N];
vector<pair<int, int>> qry[N];
stack<pair<int, int>> stk;

struct Node {
    int pf, sf, sum;
} seg[4 * N];

Node merge(Node lhs, Node rhs) {
    Node res;
    res.pf = max(lhs.pf, rhs.pf);
    res.sf = max(lhs.sf, rhs.sf);
    res.sum = max({lhs.sum, rhs.sum, lhs.pf + rhs.sf});
    return res;
}

void build(int l, int r, int id) {
    if (l == r) {
        seg[id].pf = 0;
        seg[id].sf = seg[id].sum = a[l];
        return;
    }
    int mid = (l + r) >> 1;
    build(l, mid, id * 2);
    build(mid + 1, r, id * 2 + 1);
    seg[id] = merge(seg[id * 2], seg[id * 2 + 1]);
}

void update(int pos, int val, int l, int r, int id) {
    if (l == r) {
        seg[id].pf = val;
        seg[id].sum = seg[id].pf + seg[id].sf;
        return;
    }
    int mid = (l + r) >> 1;
    if (pos <= mid) update(pos, val, l, mid, id * 2);
    else update(pos, val, mid + 1, r, id * 2 + 1);
    seg[id] = merge(seg[id * 2], seg[id * 2 + 1]);
}

Node query(int ql, int qr, int l, int r, int id) {
    if (qr < l || r < ql) return {0, 0};
    if (ql <= l && r <= qr) return seg[id];
    int mid = (l + r) >> 1;
    return merge(query(ql, qr, l, mid, id * 2), query(ql, qr, mid + 1, r, id * 2 + 1));
}

int32_t main() {
    ios::sync_with_stdio(0); cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        while (!stk.empty() && stk.top().first <= a[i]) {
            pr[stk.top().second].push_back(i);
            stk.pop();
        }
        if (!stk.empty()) pr[stk.top().second].push_back(i);
        stk.push({a[i], i});
    }
    cin >> q;
    for (int i = 1; i <= q; i++) {
        int l, r;
        cin >> l >> r;
        qry[l].push_back({r, i});
    }
    build(1, n, 1);
    for (int i = n; i >= 1; i--) {
        for (int& j : pr[i]) {
            if (2 * j - i > n) continue;
            int nw = a[i] + a[j];
            if (nw > mx[2 * j - i]) {
                update(2 * j - i, nw, 1, n, 1);
                mx[2 * j - i] = nw;
                // cout << "upd: " << 2 * j - i << " " << nw << "\n";
            }
        }
        for (auto& [j, idx] : qry[i]) {
            // cout << "idx = " << idx << '\n';
            ans[idx] = query(i, j, 1, n, 1).sum;
        }
    }
    for (int i = 1; i <= q; i++) cout << ans[i] << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 29020 KB Output is correct
2 Correct 4 ms 29020 KB Output is correct
3 Correct 4 ms 29144 KB Output is correct
4 Correct 4 ms 29020 KB Output is correct
5 Correct 4 ms 29020 KB Output is correct
6 Correct 4 ms 29020 KB Output is correct
7 Correct 5 ms 29020 KB Output is correct
8 Correct 5 ms 29020 KB Output is correct
9 Correct 4 ms 29020 KB Output is correct
10 Correct 4 ms 29020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 29020 KB Output is correct
2 Correct 4 ms 29020 KB Output is correct
3 Correct 4 ms 29144 KB Output is correct
4 Correct 4 ms 29020 KB Output is correct
5 Correct 4 ms 29020 KB Output is correct
6 Correct 4 ms 29020 KB Output is correct
7 Correct 5 ms 29020 KB Output is correct
8 Correct 5 ms 29020 KB Output is correct
9 Correct 4 ms 29020 KB Output is correct
10 Correct 4 ms 29020 KB Output is correct
11 Correct 244 ms 48212 KB Output is correct
12 Correct 216 ms 48184 KB Output is correct
13 Correct 221 ms 48216 KB Output is correct
14 Correct 222 ms 48044 KB Output is correct
15 Correct 227 ms 48212 KB Output is correct
16 Correct 218 ms 47444 KB Output is correct
17 Correct 237 ms 47444 KB Output is correct
18 Correct 221 ms 47276 KB Output is correct
19 Correct 224 ms 47880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 113 ms 42068 KB Output is correct
2 Correct 69 ms 42032 KB Output is correct
3 Correct 70 ms 43604 KB Output is correct
4 Correct 113 ms 42072 KB Output is correct
5 Correct 112 ms 42200 KB Output is correct
6 Correct 104 ms 41556 KB Output is correct
7 Correct 104 ms 41300 KB Output is correct
8 Correct 107 ms 41252 KB Output is correct
9 Correct 110 ms 41812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 29020 KB Output is correct
2 Correct 4 ms 29020 KB Output is correct
3 Correct 4 ms 29144 KB Output is correct
4 Correct 4 ms 29020 KB Output is correct
5 Correct 4 ms 29020 KB Output is correct
6 Correct 4 ms 29020 KB Output is correct
7 Correct 5 ms 29020 KB Output is correct
8 Correct 5 ms 29020 KB Output is correct
9 Correct 4 ms 29020 KB Output is correct
10 Correct 4 ms 29020 KB Output is correct
11 Correct 244 ms 48212 KB Output is correct
12 Correct 216 ms 48184 KB Output is correct
13 Correct 221 ms 48216 KB Output is correct
14 Correct 222 ms 48044 KB Output is correct
15 Correct 227 ms 48212 KB Output is correct
16 Correct 218 ms 47444 KB Output is correct
17 Correct 237 ms 47444 KB Output is correct
18 Correct 221 ms 47276 KB Output is correct
19 Correct 224 ms 47880 KB Output is correct
20 Correct 113 ms 42068 KB Output is correct
21 Correct 69 ms 42032 KB Output is correct
22 Correct 70 ms 43604 KB Output is correct
23 Correct 113 ms 42072 KB Output is correct
24 Correct 112 ms 42200 KB Output is correct
25 Correct 104 ms 41556 KB Output is correct
26 Correct 104 ms 41300 KB Output is correct
27 Correct 107 ms 41252 KB Output is correct
28 Correct 110 ms 41812 KB Output is correct
29 Correct 704 ms 84820 KB Output is correct
30 Correct 571 ms 84248 KB Output is correct
31 Correct 579 ms 88428 KB Output is correct
32 Correct 698 ms 84820 KB Output is correct
33 Correct 696 ms 84820 KB Output is correct
34 Correct 711 ms 82516 KB Output is correct
35 Correct 682 ms 82364 KB Output is correct
36 Correct 694 ms 82260 KB Output is correct
37 Correct 685 ms 83788 KB Output is correct
38 Correct 558 ms 90452 KB Output is correct
39 Correct 561 ms 90452 KB Output is correct
40 Correct 543 ms 87124 KB Output is correct
41 Correct 559 ms 86788 KB Output is correct
42 Correct 541 ms 86692 KB Output is correct
43 Correct 553 ms 88404 KB Output is correct
44 Correct 602 ms 89944 KB Output is correct
45 Correct 600 ms 89940 KB Output is correct
46 Correct 589 ms 86868 KB Output is correct
47 Correct 593 ms 86384 KB Output is correct
48 Correct 578 ms 86356 KB Output is correct
49 Correct 597 ms 88508 KB Output is correct
50 Correct 641 ms 88148 KB Output is correct
51 Correct 648 ms 88148 KB Output is correct
52 Correct 622 ms 85588 KB Output is correct
53 Correct 620 ms 85200 KB Output is correct
54 Correct 651 ms 85332 KB Output is correct
55 Correct 625 ms 86868 KB Output is correct