Submission #1010682

# Submission time Handle Problem Language Result Execution time Memory
1010682 2024-06-29T09:31:07 Z danikoynov Triple Jump (JOI19_jumps) C++14
100 / 100
905 ms 109236 KB
#include<bits/stdc++.h>
#define endl '\n'
const int MAX_N = 5e5 + 10;


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

std::vector < std::pair < int, int > > fun;

void precompute_pairs()
{
    std::stack < int > st;
    for (int i = n; i > 0; i --)
    {
        while(!st.empty() && a[st.top()] <= a[i])
        {
            fun.push_back({i, st.top()});
            st.pop();
        }
        st.push(i);
    }

    while(!st.empty())
        st.pop();
    for (int i = 1; i <= n; i ++)
    {
        while(!st.empty() && a[st.top()] <= a[i])
        {
            ///std::cout << "pop " << st.top() << " from " << i << endl;
            fun.push_back({st.top(), i});
            st.pop();
        }
        st.push(i);
    }  
}

int fp[MAX_N];

void single_query(int l, int r)
{
    fp[r + 1] = 0;
    for (int i = r; i >= l; i --)
    {
        fp[i] = std::max(fp[i + 1], a[i]);
    }

    int res = 0;
    for (std::pair < int, int > cur : fun)
    {
        int id = 2 * cur.second - cur.first;
        if (id > r || cur.first < l)
            continue;
        //std::cout << "query " << cur.first << " " << cur.second << " " << id << endl;
        res = std::max(res, a[cur.first] + a[cur.second] + fp[id]);
    }

    /**for (int i = l; i <= r; i ++)
        for (int f = i + 1; f <= r; f ++)
            for (int j = 2 * f - i; j <= r; j ++)
            {
                if (a[i] + a[j] + a[f] == 174)
                    std::cout << "winner " << i << " " << f << " " << j << endl;
            }*/
    std::cout << res << endl;
}

struct Query
{
    int l, r, id;

    Query(int _l = 0, int _r = 0, int _id = 0)
    {
        l = _l;
        r = _r;
        id = _id;
    }
};

const int INF = 1e9 + 10; 

std::vector < std::pair < int, int > > task[MAX_N];
int ans[MAX_N];

/**tree[4 * MAX_N];

void range_update(int root, int left, int right, int qleft, int qright, int val)
{
    push_lazy(root, left, right);

    if (left > qright || right < qleft)
        return;

    if (left >= qleft && right <= qright)
    {
        lazy[root] = val;
        push_lazy(root, left, right);
        return;
    }

    int mid = (left + right) / 2;
    range_update(root * 2, left, mid, qleft, qright);
    range_update(root * 2 + 1, mid + 1, right, qleft, qright);

    tree[root] = std::max(tree[root * 2], tree[root * 2 + 1]);
}*/

struct Node
{
    int p, d, res;

    Node(int _p = -INF, int _d = -INF, int _res = 0)
    {
        p = _p;
        d = _d;
        res = _res;
    }
};
Node tree[4 * MAX_N];

Node merge(Node lf, Node rf)
{
    Node mf;
    mf.d = std::max(lf.d, rf.d);
    mf.p = std::max(lf.p, rf.p);
    mf.res = std::max(lf.res, rf.res);
    mf.res = std::max(mf.res, lf.p + rf.d);
    return mf;
}

void build(int root, int left, int right)
{
    if (left == right)
    {
        tree[root].d = a[left];
        return;
    }

    int mid = (left + right) / 2;
    build(root * 2, left, mid);
    build(root * 2 + 1, mid + 1, right);

    tree[root] = merge(tree[root * 2], tree[root * 2 + 1]);
}

void update(int root, int left, int right, int pivot, int val)
{
    if (left == right)
    {
        tree[root].p = std::max(tree[root].p, val);
        tree[root].res = tree[root].d + tree[root].p;
        return;
    }

    int mid = (left + right) / 2;
    if (pivot <= mid)
        update(root * 2, left, mid, pivot, val);
    else
        update(root * 2 + 1, mid + 1, right, pivot, val);

    tree[root] = merge(tree[root * 2], tree[root * 2 + 1]);
}

Node query(int root, int left, int right, int qleft, int qright)
{
    if (left > qright || right < qleft)
        return Node();
    if (left >= qleft && right <= qright)   
        return tree[root];

    int mid = (left + right) / 2;
    return merge(query(root * 2, left, mid, qleft, qright),
                query(root * 2 + 1, mid + 1, right, qleft, qright));
}

std::vector < std::pair < int, int > > spot[MAX_N];
void answer_queries()
{
    std::cin >> q;
    for (int i = 1; i <= q; i ++)
    {
        int l, r;
        std::cin >> l >> r;
        task[l].push_back({r, i});
    }

    for (std::pair < int, int > cur : fun)
    {
        spot[cur.first].push_back({2 * cur.second - cur.first, a[cur.first] + a[cur.second]});
    }

    build(1, 1, n);
    for (int i = n; i > 0; i --)
    {
        for (std::pair < int, int > cur : spot[i])
        {
            if (cur.first > n)
                continue;

            update(1, 1, n, cur.first, cur.second);
            ///std::cout << "update " << i << " " << cur.first << " " << cur.second << endl;
        }   

        for (std::pair < int, int > cur : task[i])
        {
            Node tk = query(1, 1, n, i, cur.first);
            ans[cur.second] = tk.res;
        }
    }

    for (int i = 1; i <= q; i ++)
        std::cout << ans[i] << endl;
}


void solve()
{
    input();
    precompute_pairs();
    answer_queries();
}

void speed()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
}
int main()
{   
    speed();
    solve();
    return 0;
}
/*
15
12 96 100 61 54 66 37 34 58 21 21 1 13 50 81
1
6 14
*/
# Verdict Execution time Memory Grader output
1 Correct 20 ms 47192 KB Output is correct
2 Correct 18 ms 47452 KB Output is correct
3 Correct 19 ms 47352 KB Output is correct
4 Correct 20 ms 47452 KB Output is correct
5 Correct 23 ms 47444 KB Output is correct
6 Correct 18 ms 47452 KB Output is correct
7 Correct 20 ms 47448 KB Output is correct
8 Correct 25 ms 47452 KB Output is correct
9 Correct 24 ms 47448 KB Output is correct
10 Correct 19 ms 47452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 47192 KB Output is correct
2 Correct 18 ms 47452 KB Output is correct
3 Correct 19 ms 47352 KB Output is correct
4 Correct 20 ms 47452 KB Output is correct
5 Correct 23 ms 47444 KB Output is correct
6 Correct 18 ms 47452 KB Output is correct
7 Correct 20 ms 47448 KB Output is correct
8 Correct 25 ms 47452 KB Output is correct
9 Correct 24 ms 47448 KB Output is correct
10 Correct 19 ms 47452 KB Output is correct
11 Correct 250 ms 66036 KB Output is correct
12 Correct 308 ms 66056 KB Output is correct
13 Correct 254 ms 65948 KB Output is correct
14 Correct 313 ms 66004 KB Output is correct
15 Correct 242 ms 66132 KB Output is correct
16 Correct 277 ms 65364 KB Output is correct
17 Correct 226 ms 65364 KB Output is correct
18 Correct 247 ms 65248 KB Output is correct
19 Correct 303 ms 65936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 132 ms 60400 KB Output is correct
2 Correct 95 ms 57684 KB Output is correct
3 Correct 81 ms 57544 KB Output is correct
4 Correct 133 ms 60360 KB Output is correct
5 Correct 125 ms 60412 KB Output is correct
6 Correct 159 ms 59844 KB Output is correct
7 Correct 149 ms 59588 KB Output is correct
8 Correct 149 ms 59672 KB Output is correct
9 Correct 149 ms 60100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 47192 KB Output is correct
2 Correct 18 ms 47452 KB Output is correct
3 Correct 19 ms 47352 KB Output is correct
4 Correct 20 ms 47452 KB Output is correct
5 Correct 23 ms 47444 KB Output is correct
6 Correct 18 ms 47452 KB Output is correct
7 Correct 20 ms 47448 KB Output is correct
8 Correct 25 ms 47452 KB Output is correct
9 Correct 24 ms 47448 KB Output is correct
10 Correct 19 ms 47452 KB Output is correct
11 Correct 250 ms 66036 KB Output is correct
12 Correct 308 ms 66056 KB Output is correct
13 Correct 254 ms 65948 KB Output is correct
14 Correct 313 ms 66004 KB Output is correct
15 Correct 242 ms 66132 KB Output is correct
16 Correct 277 ms 65364 KB Output is correct
17 Correct 226 ms 65364 KB Output is correct
18 Correct 247 ms 65248 KB Output is correct
19 Correct 303 ms 65936 KB Output is correct
20 Correct 132 ms 60400 KB Output is correct
21 Correct 95 ms 57684 KB Output is correct
22 Correct 81 ms 57544 KB Output is correct
23 Correct 133 ms 60360 KB Output is correct
24 Correct 125 ms 60412 KB Output is correct
25 Correct 159 ms 59844 KB Output is correct
26 Correct 149 ms 59588 KB Output is correct
27 Correct 149 ms 59672 KB Output is correct
28 Correct 149 ms 60100 KB Output is correct
29 Correct 834 ms 103660 KB Output is correct
30 Correct 701 ms 96676 KB Output is correct
31 Correct 759 ms 96676 KB Output is correct
32 Correct 861 ms 103628 KB Output is correct
33 Correct 880 ms 103604 KB Output is correct
34 Correct 860 ms 101428 KB Output is correct
35 Correct 789 ms 101020 KB Output is correct
36 Correct 836 ms 101120 KB Output is correct
37 Correct 905 ms 102576 KB Output is correct
38 Correct 664 ms 109232 KB Output is correct
39 Correct 691 ms 109236 KB Output is correct
40 Correct 763 ms 106036 KB Output is correct
41 Correct 730 ms 105580 KB Output is correct
42 Correct 643 ms 105464 KB Output is correct
43 Correct 665 ms 107188 KB Output is correct
44 Correct 793 ms 108616 KB Output is correct
45 Correct 708 ms 108872 KB Output is correct
46 Correct 701 ms 105652 KB Output is correct
47 Correct 787 ms 105140 KB Output is correct
48 Correct 684 ms 105056 KB Output is correct
49 Correct 697 ms 107188 KB Output is correct
50 Correct 834 ms 106680 KB Output is correct
51 Correct 809 ms 106676 KB Output is correct
52 Correct 723 ms 104076 KB Output is correct
53 Correct 805 ms 103956 KB Output is correct
54 Correct 781 ms 103876 KB Output is correct
55 Correct 810 ms 105496 KB Output is correct