Submission #655439

# Submission time Handle Problem Language Result Execution time Memory
655439 2022-11-04T11:35:02 Z piOOE Triple Jump (JOI19_jumps) C++17
100 / 100
1303 ms 71568 KB
#include <bits/stdc++.h>

using namespace std;

vector<int> t, tag;
int sz = 1;

void pull(int x) {
    t[x] = max(t[x << 1], t[x << 1 | 1]) + tag[x];
}

void init(int n, vector<int> a) {
    sz = 1 << (__lg(n) + bool(n & (n - 1)));
    t.assign(sz << 1, {}), tag.assign(sz << 1, {});
    for (int i = 0; i < n; ++i) {
        t[i + sz] = a[i];
    }
    for (int i = sz - 1; i > 0; --i) {
        pull(i);
    }
}

void push(int x) {
    for (int k : {x << 1, x << 1 | 1}) {
        tag[k] += tag[x];
        t[k] += tag[x];
    }
    tag[x] = 0;
}

void rangeAdd(int l, int r, int val, int x = 1, int lx = 0, int rx = sz) {
    if (l >= rx || lx >= r) return;
    if (l <= lx && rx <= r) {
        tag[x] += val;
        t[x] += val;
        return;
    }
    push(x);
    int mid = (lx + rx) >> 1;
    rangeAdd(l, r, val, x << 1, lx, mid), rangeAdd(l, r, val, x << 1 | 1, mid, rx);
    pull(x);
}

int rangeQuery(int l, int r, int x = 1, int lx = 0, int rx = sz) {
    if (l >= rx || lx >= r) return 0;
    if (l <= lx && rx <= r) return t[x];
    push(x);
    int mid = (lx + rx) >> 1;
    return max(rangeQuery(l, r, x << 1, lx, mid), rangeQuery(l, r, x << 1 | 1, mid, rx)) + tag[x];
}

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);

    set<array<int, 3>> st;

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

            auto it = st.lower_bound({k + 1, -1, -1});
            if (it != st.begin() && (*prev(it))[2] >= sum) {
                continue;
            }
            int r = n;
            while (true) {
                it = st.lower_bound({k, -1, -1});
                if (it == st.end()) {
                    r = n;
                    break;
                }
                auto [x, y, s] = *it;
                if (s > sum) {
                    r = (*it)[0];
                    break;
                }
                rangeAdd(x, y, -s);
                st.erase(it);
            }
            if (it != st.begin()) {
                auto [x, y, s] = *prev(it);
                assert(x < k);
                assert(y >= k);
                assert(s < sum);
                st.erase(prev(it));
                rangeAdd(k, y, -s);
                st.insert({x, k, s});
            }
            rangeAdd(k, r, sum);
            st.insert({k, r, sum});
//            for (auto [l, r, k]: st) {
//                cout << l << " " << r << "  " << k << endl;
//            }
//            for (int i = 0; i < n; ++i) {
//                cout << rangeQuery(i, i + 1) - a[i] << " ";
//            }
//            cout << endl;
//            cout << endl;
        }
    }

    for (int i = 0; i < q; ++i) {
        cout << ans[i] << "\n";
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 310 ms 13428 KB Output is correct
12 Correct 304 ms 13592 KB Output is correct
13 Correct 292 ms 13268 KB Output is correct
14 Correct 297 ms 13364 KB Output is correct
15 Correct 297 ms 13356 KB Output is correct
16 Correct 298 ms 12828 KB Output is correct
17 Correct 297 ms 17580 KB Output is correct
18 Correct 310 ms 17456 KB Output is correct
19 Correct 306 ms 18004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 281 ms 10676 KB Output is correct
2 Correct 200 ms 19996 KB Output is correct
3 Correct 223 ms 9328 KB Output is correct
4 Correct 269 ms 10668 KB Output is correct
5 Correct 288 ms 10680 KB Output is correct
6 Correct 268 ms 10684 KB Output is correct
7 Correct 267 ms 10664 KB Output is correct
8 Correct 279 ms 10684 KB Output is correct
9 Correct 283 ms 10680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 310 ms 13428 KB Output is correct
12 Correct 304 ms 13592 KB Output is correct
13 Correct 292 ms 13268 KB Output is correct
14 Correct 297 ms 13364 KB Output is correct
15 Correct 297 ms 13356 KB Output is correct
16 Correct 298 ms 12828 KB Output is correct
17 Correct 297 ms 17580 KB Output is correct
18 Correct 310 ms 17456 KB Output is correct
19 Correct 306 ms 18004 KB Output is correct
20 Correct 281 ms 10676 KB Output is correct
21 Correct 200 ms 19996 KB Output is correct
22 Correct 223 ms 9328 KB Output is correct
23 Correct 269 ms 10668 KB Output is correct
24 Correct 288 ms 10680 KB Output is correct
25 Correct 268 ms 10684 KB Output is correct
26 Correct 267 ms 10664 KB Output is correct
27 Correct 279 ms 10684 KB Output is correct
28 Correct 283 ms 10680 KB Output is correct
29 Correct 1220 ms 48000 KB Output is correct
30 Correct 950 ms 71568 KB Output is correct
31 Correct 896 ms 44128 KB Output is correct
32 Correct 1215 ms 48004 KB Output is correct
33 Correct 1282 ms 47996 KB Output is correct
34 Correct 1259 ms 45672 KB Output is correct
35 Correct 1284 ms 45332 KB Output is correct
36 Correct 1230 ms 45332 KB Output is correct
37 Correct 1303 ms 46800 KB Output is correct
38 Correct 1104 ms 47996 KB Output is correct
39 Correct 1086 ms 48004 KB Output is correct
40 Correct 1031 ms 44700 KB Output is correct
41 Correct 1026 ms 44184 KB Output is correct
42 Correct 1002 ms 44184 KB Output is correct
43 Correct 1008 ms 45976 KB Output is correct
44 Correct 1066 ms 48016 KB Output is correct
45 Correct 1032 ms 48104 KB Output is correct
46 Correct 1022 ms 44824 KB Output is correct
47 Correct 1036 ms 44456 KB Output is correct
48 Correct 1038 ms 44472 KB Output is correct
49 Correct 1016 ms 46432 KB Output is correct
50 Correct 1072 ms 48144 KB Output is correct
51 Correct 1074 ms 48064 KB Output is correct
52 Correct 1075 ms 45576 KB Output is correct
53 Correct 1066 ms 45348 KB Output is correct
54 Correct 1074 ms 45268 KB Output is correct
55 Correct 1078 ms 46876 KB Output is correct