Submission #681901

# Submission time Handle Problem Language Result Execution time Memory
681901 2023-01-14T18:59:57 Z nutella Triple Jump (JOI19_jumps) C++17
100 / 100
877 ms 51660 KB
#include <bits/stdc++.h>

using namespace std;

struct Info {
    int add = 0, a = 0, ans = 0;
};

Info operator+(Info a, Info b) {
    return {max(a.add, b.add), max(a.a, b.a), max({a.ans, b.ans, a.add + b.a})};
}

int sz = 1;
vector<Info> t;

void init(int n, vector<int> a) {
    sz = n;
    t.assign(sz << 1, {});
    for (int i = 0; i < n; ++i) {
        t[i + sz].a = t[i + sz].ans = a[i];
    }
    for (int i = sz - 1; i > 0; --i) {
        t[i] = t[i << 1] + t[i << 1 | 1];
    }
}

Info rangeQuery(int l, int r) {
    Info L{}, R{};
    for (l += sz, r += sz; l < r; l >>= 1, r >>= 1) {
        if (l & 1) {
            L = L + t[l++];
        }
        if (r & 1) {
            R = t[--r] + R;
        }
    }
    return L + R;
}

void modify(int i, int add) {
    i += sz;
    t[i].add = max(t[i].add, add);
    t[i].ans = t[i].add + t[i].a;
    while (i >>= 1) {
        t[i] = t[i << 1] + t[i << 1 | 1];
    }
}

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<array<int, 3>> events;

    auto relax = [&](int i, int j) {
        int r = j + (j - i);
        if (r < n) {
            events.push_back({i, j, -1});
        }
    };

    vector<int> stk;
    for (int i = 0; i < n; ++i) {
        while (!stk.empty() && a[stk.back()] <= a[i]) {
            relax(stk.back(), i);
            stk.pop_back();
        }
        if (!stk.empty()) {
            relax(stk.back(), i);
        }
        stk.push_back(i);
    }

    int q;
    cin >> q;

    for (int i = 0; i < q; ++i) {
        int l, r;
        cin >> l >> r;
        events.push_back({l - 1, r, i});
    }

    vector<int> ans(q);

    sort(events.begin(), events.end(), [&](array<int, 3> x, array<int, 3> y) {
        return x[0] != y[0] ? x[0] > y[0] : x[2] < y[2];
    });

    init(n, a);

    for (auto [i, j, tp]: events) {
        if (tp > -1) {
            ans[tp] = rangeQuery(i, j).ans;
        } else {
            int k = j + (j - i);
            int sum = a[i] + a[j];
            modify(k, sum);
        }
    }

    for (int i = 0; i < q; ++i) {
        cout << ans[i] << "\n";
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 316 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 316 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 226 ms 18120 KB Output is correct
12 Correct 225 ms 18020 KB Output is correct
13 Correct 229 ms 18152 KB Output is correct
14 Correct 218 ms 18144 KB Output is correct
15 Correct 227 ms 18264 KB Output is correct
16 Correct 225 ms 17456 KB Output is correct
17 Correct 228 ms 17376 KB Output is correct
18 Correct 238 ms 17436 KB Output is correct
19 Correct 242 ms 17916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 133 ms 12932 KB Output is correct
2 Correct 89 ms 10640 KB Output is correct
3 Correct 101 ms 11616 KB Output is correct
4 Correct 131 ms 12952 KB Output is correct
5 Correct 141 ms 12944 KB Output is correct
6 Correct 124 ms 12344 KB Output is correct
7 Correct 122 ms 12208 KB Output is correct
8 Correct 129 ms 12168 KB Output is correct
9 Correct 130 ms 12508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 316 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 226 ms 18120 KB Output is correct
12 Correct 225 ms 18020 KB Output is correct
13 Correct 229 ms 18152 KB Output is correct
14 Correct 218 ms 18144 KB Output is correct
15 Correct 227 ms 18264 KB Output is correct
16 Correct 225 ms 17456 KB Output is correct
17 Correct 228 ms 17376 KB Output is correct
18 Correct 238 ms 17436 KB Output is correct
19 Correct 242 ms 17916 KB Output is correct
20 Correct 133 ms 12932 KB Output is correct
21 Correct 89 ms 10640 KB Output is correct
22 Correct 101 ms 11616 KB Output is correct
23 Correct 131 ms 12952 KB Output is correct
24 Correct 141 ms 12944 KB Output is correct
25 Correct 124 ms 12344 KB Output is correct
26 Correct 122 ms 12208 KB Output is correct
27 Correct 129 ms 12168 KB Output is correct
28 Correct 130 ms 12508 KB Output is correct
29 Correct 710 ms 51480 KB Output is correct
30 Correct 514 ms 45524 KB Output is correct
31 Correct 550 ms 47608 KB Output is correct
32 Correct 721 ms 51544 KB Output is correct
33 Correct 689 ms 51660 KB Output is correct
34 Correct 877 ms 49160 KB Output is correct
35 Correct 684 ms 49000 KB Output is correct
36 Correct 823 ms 48868 KB Output is correct
37 Correct 792 ms 50356 KB Output is correct
38 Correct 650 ms 51544 KB Output is correct
39 Correct 620 ms 51524 KB Output is correct
40 Correct 584 ms 48208 KB Output is correct
41 Correct 561 ms 47724 KB Output is correct
42 Correct 597 ms 47724 KB Output is correct
43 Correct 600 ms 49396 KB Output is correct
44 Correct 669 ms 51556 KB Output is correct
45 Correct 610 ms 51496 KB Output is correct
46 Correct 620 ms 48380 KB Output is correct
47 Correct 614 ms 47996 KB Output is correct
48 Correct 652 ms 47996 KB Output is correct
49 Correct 614 ms 49988 KB Output is correct
50 Correct 632 ms 51632 KB Output is correct
51 Correct 622 ms 51596 KB Output is correct
52 Correct 612 ms 49188 KB Output is correct
53 Correct 627 ms 48796 KB Output is correct
54 Correct 583 ms 48952 KB Output is correct
55 Correct 618 ms 50404 KB Output is correct