Submission #925833

# Submission time Handle Problem Language Result Execution time Memory
925833 2024-02-12T09:41:35 Z boris_mihov Triple Jump (JOI19_jumps) C++17
100 / 100
801 ms 106764 KB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <set>

typedef long long llong;
const int MAXN = 500000 + 10;
const int INF  = 2e9;

int n, q;
struct SegmentTree
{
    struct Node
    {
        int maxAB;
        int maxC;
        int lazy;
        int max;

        Node()
        {
            maxAB = maxC = lazy = max = 0;
        }

        friend Node operator + (const Node &left, const Node &right)
        {
            Node res;
            res.maxC = std::max(left.maxC, right.maxC);
            res.max = std::max(left.max, right.max);
            return res;
        }
    };

    Node tree[4*MAXN];
    void push(int node, int l, int r)
    {
        if (tree[node].lazy == 0)
        {
            return;
        }

        tree[node].maxAB = std::max(tree[node].maxAB, tree[node].lazy);
        if (tree[node].maxAB != 0)
        {
            tree[node].max = std::max(tree[node].max, tree[node].maxAB + tree[node].maxC);
        }

        if (l < r)
        {
            tree[2*node].lazy = std::max(tree[2*node].lazy, tree[node].lazy);
            tree[2*node + 1].lazy = std::max(tree[2*node + 1].lazy, tree[node].lazy);
        }

        tree[node].lazy = 0;
    }

    void build(int l, int r, int node, int a[])
    {
        tree[node] = Node();
        if (l == r)
        {
            tree[node].maxC = a[l];
            return;
        }

        int mid = (l + r) / 2;
        build(l, mid, 2*node, a);
        build(mid + 1, r, 2*node + 1, a);
        tree[node] = tree[2*node] + tree[2*node + 1];
    }

    void update(int l, int r, int node, int queryL, int queryR, int queryVal)
    {
        push(node, l, r);
        if (queryR < l || r < queryL)
        {
            return;
        }

        if (queryL <= l && r <= queryR)
        {
            tree[node].lazy = queryVal;
            push(node, l, r);
            return;
        }

        int mid = (l + r) / 2;
        update(l, mid, 2*node, queryL, queryR, queryVal);
        update(mid + 1, r, 2*node + 1, queryL, queryR, queryVal);
        tree[node] = tree[2*node] + tree[2*node + 1];
    }

    int query(int l, int r, int node, int queryL, int queryR)
    {
        push(node, l, r);
        if (queryL <= l && r <= queryR)
        {
            return tree[node].max;
        }

        int res = 0;
        int mid = (l + r) / 2;
        if (queryL <= mid) res = std::max(res, query(l, mid, 2*node, queryL, queryR));
        if (mid + 1 <= queryR) res = std::max(res, query(mid + 1, r, 2*node + 1, queryL, queryR));
        return res;
    }

    void build(int a[])
    {
        build(1, n, 1, a);
    }

    void update(int l, int r, int val)
    {
        update(1, n, 1, l, r, val);
    }

    int query(int l, int r)
    {
        return query(1, n, 1, l, r);
    }
};

int a[MAXN];

int answer[MAXN];
std::vector <int> pairs[MAXN];
std::vector <std::pair <int,int>> queries[MAXN];
std::stack <int> st;
SegmentTree tree;

void solve()
{
    for (int i = n ; i >= 1 ; --i)
    {
        while (st.size() && a[st.top()] <= a[i])
        {
            pairs[i].push_back(st.top());
            st.pop();
        }

        if (st.size())
        {
            pairs[i].push_back(st.top());
        }

        st.push(i);
    }

    tree.build(a);
    for (int i = n ; i >= 1 ; --i)
    {
        for (const int &next : pairs[i])
        {
            if (2 * next - i <= n)
            {
                tree.update(2 * next - i, n, a[i] + a[next]);
            }
        }

        for (const auto &[r, idx] : queries[i])
        {
            answer[idx] = tree.query(i, r);
        }
    }
}

void print()
{
    for (int i = 1 ; i <= q ; ++i)
    {
        std::cout << answer[i] << '\n';
    }
}

void input()
{
    std::cin >> n;
    for (int i = 1 ; i <= n ; ++i)
    {
        std::cin >> a[i];
    }

    std::cin >> q;
    for (int i = 1 ; i <= q ; ++i)
    {
        int l, r;
        std::cin >> l >> r;
        queries[l].push_back({r, i});
    }
}

void fastIOI()
{
    std::ios_base :: sync_with_stdio(0);
    std::cout.tie(nullptr);
    std::cin.tie(nullptr);
}   

int main()
{
    fastIOI();
    input();
    solve();
    print();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 21 ms 58460 KB Output is correct
2 Correct 12 ms 57948 KB Output is correct
3 Correct 12 ms 58040 KB Output is correct
4 Correct 11 ms 57948 KB Output is correct
5 Correct 14 ms 57948 KB Output is correct
6 Correct 13 ms 57948 KB Output is correct
7 Correct 12 ms 57948 KB Output is correct
8 Correct 12 ms 57944 KB Output is correct
9 Correct 13 ms 57948 KB Output is correct
10 Correct 12 ms 57948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 58460 KB Output is correct
2 Correct 12 ms 57948 KB Output is correct
3 Correct 12 ms 58040 KB Output is correct
4 Correct 11 ms 57948 KB Output is correct
5 Correct 14 ms 57948 KB Output is correct
6 Correct 13 ms 57948 KB Output is correct
7 Correct 12 ms 57948 KB Output is correct
8 Correct 12 ms 57944 KB Output is correct
9 Correct 13 ms 57948 KB Output is correct
10 Correct 12 ms 57948 KB Output is correct
11 Correct 201 ms 74740 KB Output is correct
12 Correct 196 ms 74572 KB Output is correct
13 Correct 206 ms 74716 KB Output is correct
14 Correct 193 ms 74716 KB Output is correct
15 Correct 213 ms 74576 KB Output is correct
16 Correct 196 ms 74068 KB Output is correct
17 Correct 208 ms 73808 KB Output is correct
18 Correct 201 ms 73960 KB Output is correct
19 Correct 207 ms 74576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 135 ms 66264 KB Output is correct
2 Correct 70 ms 66788 KB Output is correct
3 Correct 68 ms 65872 KB Output is correct
4 Correct 144 ms 66344 KB Output is correct
5 Correct 134 ms 66128 KB Output is correct
6 Correct 137 ms 65624 KB Output is correct
7 Correct 131 ms 65504 KB Output is correct
8 Correct 138 ms 65528 KB Output is correct
9 Correct 130 ms 65616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 58460 KB Output is correct
2 Correct 12 ms 57948 KB Output is correct
3 Correct 12 ms 58040 KB Output is correct
4 Correct 11 ms 57948 KB Output is correct
5 Correct 14 ms 57948 KB Output is correct
6 Correct 13 ms 57948 KB Output is correct
7 Correct 12 ms 57948 KB Output is correct
8 Correct 12 ms 57944 KB Output is correct
9 Correct 13 ms 57948 KB Output is correct
10 Correct 12 ms 57948 KB Output is correct
11 Correct 201 ms 74740 KB Output is correct
12 Correct 196 ms 74572 KB Output is correct
13 Correct 206 ms 74716 KB Output is correct
14 Correct 193 ms 74716 KB Output is correct
15 Correct 213 ms 74576 KB Output is correct
16 Correct 196 ms 74068 KB Output is correct
17 Correct 208 ms 73808 KB Output is correct
18 Correct 201 ms 73960 KB Output is correct
19 Correct 207 ms 74576 KB Output is correct
20 Correct 135 ms 66264 KB Output is correct
21 Correct 70 ms 66788 KB Output is correct
22 Correct 68 ms 65872 KB Output is correct
23 Correct 144 ms 66344 KB Output is correct
24 Correct 134 ms 66128 KB Output is correct
25 Correct 137 ms 65624 KB Output is correct
26 Correct 131 ms 65504 KB Output is correct
27 Correct 138 ms 65528 KB Output is correct
28 Correct 130 ms 65616 KB Output is correct
29 Correct 801 ms 101092 KB Output is correct
30 Correct 609 ms 102508 KB Output is correct
31 Correct 596 ms 100748 KB Output is correct
32 Correct 776 ms 101200 KB Output is correct
33 Correct 787 ms 101100 KB Output is correct
34 Correct 787 ms 98944 KB Output is correct
35 Correct 738 ms 98432 KB Output is correct
36 Correct 790 ms 98624 KB Output is correct
37 Correct 728 ms 99896 KB Output is correct
38 Correct 546 ms 106724 KB Output is correct
39 Correct 550 ms 106764 KB Output is correct
40 Correct 538 ms 103764 KB Output is correct
41 Correct 569 ms 103084 KB Output is correct
42 Correct 533 ms 102992 KB Output is correct
43 Correct 547 ms 104680 KB Output is correct
44 Correct 571 ms 106132 KB Output is correct
45 Correct 572 ms 106244 KB Output is correct
46 Correct 572 ms 102996 KB Output is correct
47 Correct 585 ms 102736 KB Output is correct
48 Correct 558 ms 102704 KB Output is correct
49 Correct 624 ms 104652 KB Output is correct
50 Correct 621 ms 104068 KB Output is correct
51 Correct 667 ms 104060 KB Output is correct
52 Correct 626 ms 101656 KB Output is correct
53 Correct 612 ms 101180 KB Output is correct
54 Correct 616 ms 101396 KB Output is correct
55 Correct 625 ms 102996 KB Output is correct