Submission #502752

#TimeUsernameProblemLanguageResultExecution timeMemory
502752tengiz05Triple Jump (JOI19_jumps)C++17
100 / 100
1081 ms67752 KiB
#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;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...