답안 #655442

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
655442 2022-11-04T11:37:52 Z piOOE 3단 점프 (JOI19_jumps) C++17
100 / 100
1212 ms 60424 KB
#include <bits/stdc++.h>

using namespace std;

constexpr int inf = 1e9 + 7;

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

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

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];
    int mid = (lx + rx) >> 1;
    push(x);
    return max(rangeQuery(l, r, x << 1, lx, mid), rangeQuery(l, r, x << 1 | 1, mid, rx));
}

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);
                st.erase(prev(it));
                rangeAdd(k, y, -s);
                st.insert({x, k, s});
            }
            rangeAdd(k, r, sum);
            st.insert({k, r, sum});
        }
    }

    for (int i = 0; i < q; ++i) {
        cout << ans[i] << "\n";
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 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 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 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 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 295 ms 13420 KB Output is correct
12 Correct 287 ms 13656 KB Output is correct
13 Correct 285 ms 13272 KB Output is correct
14 Correct 299 ms 13424 KB Output is correct
15 Correct 288 ms 13464 KB Output is correct
16 Correct 292 ms 12780 KB Output is correct
17 Correct 296 ms 12768 KB Output is correct
18 Correct 332 ms 12780 KB Output is correct
19 Correct 301 ms 13368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 289 ms 10668 KB Output is correct
2 Correct 214 ms 19964 KB Output is correct
3 Correct 215 ms 9348 KB Output is correct
4 Correct 273 ms 10672 KB Output is correct
5 Correct 284 ms 10664 KB Output is correct
6 Correct 276 ms 10672 KB Output is correct
7 Correct 288 ms 10596 KB Output is correct
8 Correct 291 ms 10680 KB Output is correct
9 Correct 272 ms 10672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 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 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 295 ms 13420 KB Output is correct
12 Correct 287 ms 13656 KB Output is correct
13 Correct 285 ms 13272 KB Output is correct
14 Correct 299 ms 13424 KB Output is correct
15 Correct 288 ms 13464 KB Output is correct
16 Correct 292 ms 12780 KB Output is correct
17 Correct 296 ms 12768 KB Output is correct
18 Correct 332 ms 12780 KB Output is correct
19 Correct 301 ms 13368 KB Output is correct
20 Correct 289 ms 10668 KB Output is correct
21 Correct 214 ms 19964 KB Output is correct
22 Correct 215 ms 9348 KB Output is correct
23 Correct 273 ms 10672 KB Output is correct
24 Correct 284 ms 10664 KB Output is correct
25 Correct 276 ms 10672 KB Output is correct
26 Correct 288 ms 10596 KB Output is correct
27 Correct 291 ms 10680 KB Output is correct
28 Correct 272 ms 10672 KB Output is correct
29 Correct 1183 ms 37012 KB Output is correct
30 Correct 953 ms 60424 KB Output is correct
31 Correct 887 ms 33068 KB Output is correct
32 Correct 1168 ms 37000 KB Output is correct
33 Correct 1192 ms 37004 KB Output is correct
34 Correct 1170 ms 36396 KB Output is correct
35 Correct 1116 ms 36244 KB Output is correct
36 Correct 1121 ms 36244 KB Output is correct
37 Correct 1212 ms 36888 KB Output is correct
38 Correct 1034 ms 36964 KB Output is correct
39 Correct 1038 ms 36968 KB Output is correct
40 Correct 1046 ms 35360 KB Output is correct
41 Correct 1061 ms 35084 KB Output is correct
42 Correct 1000 ms 35180 KB Output is correct
43 Correct 1018 ms 35988 KB Output is correct
44 Correct 1028 ms 37092 KB Output is correct
45 Correct 1052 ms 38412 KB Output is correct
46 Correct 1048 ms 37008 KB Output is correct
47 Correct 1045 ms 36748 KB Output is correct
48 Correct 1051 ms 36748 KB Output is correct
49 Correct 1065 ms 37908 KB Output is correct
50 Correct 1076 ms 38412 KB Output is correct
51 Correct 1087 ms 38292 KB Output is correct
52 Correct 1076 ms 37272 KB Output is correct
53 Correct 1098 ms 37196 KB Output is correct
54 Correct 1085 ms 37188 KB Output is correct
55 Correct 1094 ms 37988 KB Output is correct