답안 #502752

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
502752 2022-01-06T14:29:58 Z tengiz05 3단 점프 (JOI19_jumps) C++17
100 / 100
1081 ms 67752 KB
#include <bits/stdc++.h>

using i64 = long long;

struct Info {
    int x;
    int mx;
    Info(int x = 0, int mx = 0) : x(x), mx(std::max(x, mx)) {}
};
Info operator+(const Info &a, const Info b) {
    return Info(std::max(a.x, b.x), std::max(a.mx, b.mx));
}
struct Tag {
    int x;
    Tag(int x = 0) : x(x) {}
};
void apply(Tag &a, const Tag &b) {
    a.x = std::max(a.x, b.x);
}
void apply(Info &a, const Tag &b) {
    a.mx = std::max(a.mx, a.x + b.x);
}
template<class Info, class Tag,
    class Merge = std::plus<Info>>
struct LazySegmentTree {
    const int n;
    const Merge merge;
    std::vector<Info> info;
    std::vector<Tag> tag;
    LazySegmentTree(int n) : n(n), merge(Merge()), info(4 << std::__lg(n)), tag(4 << std::__lg(n)) {}
    LazySegmentTree(std::vector<Info> init) : LazySegmentTree(init.size()) {
        std::function<void(int, int, int)> build = [&](int p, int l, int r) {
            if (r - l == 1) {
                info[p] = init[l];
                return;
            }
            int m = (l + r) / 2;
            build(2 * p, l, m);
            build(2 * p + 1, m, r);
            pull(p);
        };
        build(1, 0, n);
    }
    void pull(int p) {
        info[p] = merge(info[2 * p], info[2 * p + 1]);
    }
    void apply(int p, const Tag &v) {
        ::apply(info[p], v);
        ::apply(tag[p], v);
    }
    void push(int p) {
        apply(2 * p, tag[p]);
        apply(2 * p + 1, tag[p]);
        tag[p] = Tag();
    }
    void modify(int p, int l, int r, int x, Info v) {
        if (r - l == 1) {
            info[p] = v;
            return;
        }
        int m = (l + r) / 2;
        push(p);
        if (x < m) {
            modify(2 * p, l, m, x, v);
        } else {
            modify(2 * p + 1, m, r, x, v);
        }
        pull(p);
    }
    void modify(int x, Info v) {
        modify(1, 0, n, x, v);
    }
    Info rangeQuery(int p, int l, int r, int x, int y) {
        if (l >= y || r <= x) {
            return Info();
        }
        if (l >= x && r <= y) {
            return info[p];
        }
        int m = (l + r) / 2;
        push(p);
        return merge(rangeQuery(2 * p, l, m, x, y), rangeQuery(2 * p + 1, m, r, x, y));
    }
    Info rangeQuery(int l, int r) {
        return rangeQuery(1, 0, n, l, r);
    }
    void rangeApply(int p, int l, int r, int x, int y, const Tag &v) {
        if (l >= y || r <= x) {
            return;
        }
        if (l >= x && r <= y) {
            apply(p, v);
            return;
        }
        int m = (l + r) / 2;
        push(p);
        rangeApply(2 * p, l, m, x, y, v);
        rangeApply(2 * p + 1, m, r, x, y, v);
        pull(p);
    }
    void rangeApply(int l, int r, const Tag &v) {
        return rangeApply(1, 0, n, l, r, v);
    }
};

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int n;
    std::cin >> n;
    
    std::vector<int> a(n);
    for (int i = 0; i < n; i++) {
        std::cin >> a[i];
    }
    
    std::vector<std::tuple<int, int, int>> Q;
    int q;
    std::cin >> q;
    for (int i = 0; i < q; i++) {
        int l, r; std::cin >> l >> r;
        l--;
        Q.emplace_back(l, r, i);
    }
    std::sort(Q.begin(), Q.end(), std::greater());
    
    std::vector<std::vector<int>> e(n);
    std::vector<int> s;
    for (int i = 0; i < n; i++) {
        while (!s.empty() && a[s.back()] < a[i]) {
            s.pop_back();
        }
        if (!s.empty()) {
            e[s.back()].push_back(i);
        }
        s.push_back(i);
    }
    s.clear();
    for (int i = n - 1; i >= 0; i--) {
        while (!s.empty() && a[s.back()] < a[i]) {
            s.pop_back();
        }
        if (!s.empty()) {
            e[i].push_back(s.back());
        }
        s.push_back(i);
    }
    
    LazySegmentTree<Info, Tag> t(n);
    for (int i = 0; i < n; i++) {
        t.modify(i, Info(a[i]));
    }
    
    int ans[q] {};
    int k = 0;
    for (int i = n - 1; i >= 0; i--) {
        for (int j : e[i]) {
            if (2 * j - i < n) {
                t.rangeApply(2 * j - i, n, a[i] + a[j]);
            }
        }
        while (k < q && std::get<0>(Q[k]) == i) {
            auto [l, r, id] = Q[k];
            ans[id] = t.rangeQuery(l + 2, r).mx;
            k++;
        }
    }
    
    for (int i = 0; i < q; i++) {
        std::cout << ans[i] << "\n";
    }
    
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 304 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 304 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 328 ms 18336 KB Output is correct
12 Correct 326 ms 18288 KB Output is correct
13 Correct 313 ms 18232 KB Output is correct
14 Correct 304 ms 18340 KB Output is correct
15 Correct 304 ms 18444 KB Output is correct
16 Correct 339 ms 17688 KB Output is correct
17 Correct 298 ms 17552 KB Output is correct
18 Correct 297 ms 17600 KB Output is correct
19 Correct 300 ms 18180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 198 ms 18432 KB Output is correct
2 Correct 165 ms 19012 KB Output is correct
3 Correct 136 ms 20736 KB Output is correct
4 Correct 237 ms 20184 KB Output is correct
5 Correct 212 ms 20280 KB Output is correct
6 Correct 198 ms 19524 KB Output is correct
7 Correct 213 ms 19316 KB Output is correct
8 Correct 201 ms 19384 KB Output is correct
9 Correct 222 ms 19752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 304 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 328 ms 18336 KB Output is correct
12 Correct 326 ms 18288 KB Output is correct
13 Correct 313 ms 18232 KB Output is correct
14 Correct 304 ms 18340 KB Output is correct
15 Correct 304 ms 18444 KB Output is correct
16 Correct 339 ms 17688 KB Output is correct
17 Correct 298 ms 17552 KB Output is correct
18 Correct 297 ms 17600 KB Output is correct
19 Correct 300 ms 18180 KB Output is correct
20 Correct 198 ms 18432 KB Output is correct
21 Correct 165 ms 19012 KB Output is correct
22 Correct 136 ms 20736 KB Output is correct
23 Correct 237 ms 20184 KB Output is correct
24 Correct 212 ms 20280 KB Output is correct
25 Correct 198 ms 19524 KB Output is correct
26 Correct 213 ms 19316 KB Output is correct
27 Correct 201 ms 19384 KB Output is correct
28 Correct 222 ms 19752 KB Output is correct
29 Correct 1042 ms 66380 KB Output is correct
30 Correct 819 ms 67672 KB Output is correct
31 Correct 818 ms 67752 KB Output is correct
32 Correct 1081 ms 66324 KB Output is correct
33 Correct 1002 ms 66336 KB Output is correct
34 Correct 1076 ms 64060 KB Output is correct
35 Correct 1035 ms 63764 KB Output is correct
36 Correct 1018 ms 63800 KB Output is correct
37 Correct 1006 ms 65152 KB Output is correct
38 Correct 848 ms 66468 KB Output is correct
39 Correct 871 ms 66352 KB Output is correct
40 Correct 909 ms 63172 KB Output is correct
41 Correct 862 ms 62632 KB Output is correct
42 Correct 831 ms 62580 KB Output is correct
43 Correct 864 ms 64392 KB Output is correct
44 Correct 905 ms 66356 KB Output is correct
45 Correct 886 ms 66472 KB Output is correct
46 Correct 897 ms 63160 KB Output is correct
47 Correct 964 ms 62828 KB Output is correct
48 Correct 865 ms 62764 KB Output is correct
49 Correct 882 ms 64900 KB Output is correct
50 Correct 929 ms 66448 KB Output is correct
51 Correct 958 ms 66408 KB Output is correct
52 Correct 905 ms 63920 KB Output is correct
53 Correct 932 ms 63580 KB Output is correct
54 Correct 955 ms 63624 KB Output is correct
55 Correct 944 ms 65212 KB Output is correct