Submission #916097

# Submission time Handle Problem Language Result Execution time Memory
916097 2024-01-25T09:33:54 Z boris_mihov Constellation 3 (JOI20_constellation3) C++17
100 / 100
1032 ms 129528 KB
#include <algorithm>
#include <iostream>
#include <cassert>
#include <numeric>
#include <vector>
#include <stack>

typedef long long llong;
const int MAXLOG = 19;
const int MAXN = 400000 + 10;
const int INF  = 1e9;

int n, m;
int a[MAXN];
int h[MAXN];
int cntSegm;

struct Sparse
{
    int sparse[MAXLOG][MAXN];
    void build(int par[])
    {
        for (int i = 1 ; i <= cntSegm ; ++i)
        {
            sparse[0][i] = par[i];
        }

        for (int log = 1 ; (1 << log) <= cntSegm ; ++log)
        {
            for (int i = 1 ; i <= cntSegm ; ++i)
            {
                sparse[log][i] = sparse[log - 1][sparse[log - 1][i]];
            }
        }
    }

    int find(int x, int y)
    {
        for (int log = MAXLOG - 1 ; log >= 0 ; --log)
        {
            if (h[sparse[log][x]] < y)
            {
                x = sparse[log][x];
            }
        }

        return x;
    }
};

struct SegmentTree
{
    struct Node
    {
        llong value;
        llong lazy;

        Node()
        {
            value = lazy = 0;
        }
    };  

    Node tree[4*MAXN];
    void push(int node, int l, int r)
    {
        if (l == r)
        {
            tree[node].value += tree[node].lazy;
        } else
        {
            tree[2*node].lazy += tree[node].lazy;
            tree[2*node + 1].lazy += tree[node].lazy;
        }

        tree[node].lazy = 0;
    }

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

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

        int mid = (l + r) / 2;
        update(l, mid, 2*node, queryL, queryR, queryValue);
        update(mid + 1, r, 2*node + 1, queryL, queryR, queryValue);
    }

    llong query(int l, int r, int node, int queryPos)
    {
        push(node, l, r);
        if (l == r)
        {
            return tree[node].value;
        }

        int mid = (l + r) / 2;
        if (queryPos <= mid) return query(l, mid, 2*node, queryPos);
        return query(mid + 1, r, 2*node + 1, queryPos);
    }

    void update(int l, int r, llong val)
    {
        assert(l >= 0 && r <= cntSegm);
        update(0, cntSegm, 1, l, r, val);
    }

    llong query(int pos)
    {
        return query(0, cntSegm, 1, pos);
    }
};

int x[MAXN];
int y[MAXN];
int c[MAXN];
int in[MAXN];
int out[MAXN];
int par[MAXN];
llong dp[MAXN];
llong sum[MAXN];
int leftSegm[MAXN];
int rightSegm[MAXN];
int leftBigger[MAXN];
std::vector <int> g[MAXN];
std::vector <int> stars[MAXN];
std::stack <int> st;
SegmentTree tree;
Sparse sparse;

int timer;
void buildDFS(int node)
{
    in[node] = timer++;
    for (const int &u : g[node])
    {
        buildDFS(u);
    }

    out[node] = timer - 1;
}

void solveDFS(int node)
{
    for (const int &u : g[node])
    {
        solveDFS(u);
        sum[node] += dp[u];
    }

    llong res = sum[node];
    tree.update(in[node], out[node], sum[node]);

    for (const int &idx : stars[node])
    {
        res = std::max(res, c[idx] + tree.query(in[par[leftSegm[x[idx]]]]));
    }

    dp[node] = res;
    tree.update(in[node], out[node], -dp[node]);
}

void solve()
{
    a[0] = INF;
    h[0] = INF;
    st.push(0);
    for (int i = 1 ; i <= n ; ++i)
    {
        while (a[st.top()] < a[i])
        {
            st.pop();
        }

        leftBigger[i] = st.top();
        st.push(i);
    }

    while (st.size())
    {
        st.pop();
    }

    for (int i = 1 ; i <= n ; ++i)
    {
        bool wasThere = false;
        if (a[leftBigger[i]] == a[i]) 
        {
            wasThere = true;
            leftSegm[i] = rightSegm[leftBigger[i]];
        } else 
        {
            leftSegm[i] = ++cntSegm;
        }

        rightSegm[i] = ++cntSegm;
        h[leftSegm[i]] = a[i];
        h[rightSegm[i]] = a[i];

        while (st.size() && a[st.top()] < a[i])
        {
            if (!wasThere && (par[leftSegm[st.top()]] == 0 || h[par[leftSegm[st.top()]]] > a[i]))
            {
                par[rightSegm[st.top()]] = leftSegm[i];
                par[leftSegm[st.top()]] = leftSegm[i];
            }
            
            st.pop();
        }

        if (st.size())
        {
            if (a[st.top()] > a[i]) 
            {
                par[leftSegm[i]] = rightSegm[st.top()];
                par[rightSegm[i]] = rightSegm[st.top()];
            } else 
            {
                par[leftSegm[i]] = par[rightSegm[st.top()]];
                par[rightSegm[i]] = par[rightSegm[st.top()]];
            }
        }
        
        st.push(i);
    }


    for (int i = 1 ; i <= cntSegm ; ++i)
    {
        g[par[i]].push_back(i);
    }

    sparse.build(par);
    llong sumStars = 0;
    for (int i = 1 ; i <= m ; ++i)
    {
        stars[par[sparse.find(leftSegm[x[i]], y[i])]].push_back(i);
        sumStars += c[i];
    }

    buildDFS(0);
    solveDFS(0);
    std::cout << sumStars - dp[0] << '\n';
}

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

    std::cin >> m;
    for (int i = 1 ; i <= m ; ++i)
    {
        std::cin >> x[i] >> y[i] >> c[i];
    }
}

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

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

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 19 ms 78428 KB Output is correct
2 Correct 16 ms 78684 KB Output is correct
3 Correct 17 ms 76380 KB Output is correct
4 Correct 15 ms 78312 KB Output is correct
5 Correct 16 ms 78428 KB Output is correct
6 Correct 16 ms 76388 KB Output is correct
7 Correct 17 ms 78428 KB Output is correct
8 Correct 16 ms 78428 KB Output is correct
9 Correct 16 ms 78428 KB Output is correct
10 Correct 15 ms 76380 KB Output is correct
11 Correct 16 ms 76448 KB Output is correct
12 Correct 15 ms 76464 KB Output is correct
13 Correct 16 ms 78428 KB Output is correct
14 Correct 16 ms 76376 KB Output is correct
15 Correct 16 ms 76636 KB Output is correct
16 Correct 16 ms 76424 KB Output is correct
17 Correct 17 ms 78432 KB Output is correct
18 Correct 16 ms 76208 KB Output is correct
19 Correct 18 ms 76376 KB Output is correct
20 Correct 17 ms 78428 KB Output is correct
21 Correct 17 ms 78428 KB Output is correct
22 Correct 15 ms 78408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 78428 KB Output is correct
2 Correct 16 ms 78684 KB Output is correct
3 Correct 17 ms 76380 KB Output is correct
4 Correct 15 ms 78312 KB Output is correct
5 Correct 16 ms 78428 KB Output is correct
6 Correct 16 ms 76388 KB Output is correct
7 Correct 17 ms 78428 KB Output is correct
8 Correct 16 ms 78428 KB Output is correct
9 Correct 16 ms 78428 KB Output is correct
10 Correct 15 ms 76380 KB Output is correct
11 Correct 16 ms 76448 KB Output is correct
12 Correct 15 ms 76464 KB Output is correct
13 Correct 16 ms 78428 KB Output is correct
14 Correct 16 ms 76376 KB Output is correct
15 Correct 16 ms 76636 KB Output is correct
16 Correct 16 ms 76424 KB Output is correct
17 Correct 17 ms 78432 KB Output is correct
18 Correct 16 ms 76208 KB Output is correct
19 Correct 18 ms 76376 KB Output is correct
20 Correct 17 ms 78428 KB Output is correct
21 Correct 17 ms 78428 KB Output is correct
22 Correct 15 ms 78408 KB Output is correct
23 Correct 19 ms 82520 KB Output is correct
24 Correct 19 ms 82524 KB Output is correct
25 Correct 19 ms 82524 KB Output is correct
26 Correct 20 ms 82520 KB Output is correct
27 Correct 19 ms 82524 KB Output is correct
28 Correct 19 ms 80476 KB Output is correct
29 Correct 19 ms 80476 KB Output is correct
30 Correct 20 ms 80584 KB Output is correct
31 Correct 19 ms 80476 KB Output is correct
32 Correct 20 ms 82780 KB Output is correct
33 Correct 18 ms 82744 KB Output is correct
34 Correct 19 ms 80732 KB Output is correct
35 Correct 21 ms 82524 KB Output is correct
36 Correct 19 ms 78456 KB Output is correct
37 Correct 17 ms 78428 KB Output is correct
38 Correct 18 ms 80732 KB Output is correct
39 Correct 19 ms 82524 KB Output is correct
40 Correct 19 ms 82780 KB Output is correct
41 Correct 18 ms 82524 KB Output is correct
42 Correct 19 ms 82520 KB Output is correct
43 Correct 19 ms 82780 KB Output is correct
44 Correct 19 ms 82524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 78428 KB Output is correct
2 Correct 16 ms 78684 KB Output is correct
3 Correct 17 ms 76380 KB Output is correct
4 Correct 15 ms 78312 KB Output is correct
5 Correct 16 ms 78428 KB Output is correct
6 Correct 16 ms 76388 KB Output is correct
7 Correct 17 ms 78428 KB Output is correct
8 Correct 16 ms 78428 KB Output is correct
9 Correct 16 ms 78428 KB Output is correct
10 Correct 15 ms 76380 KB Output is correct
11 Correct 16 ms 76448 KB Output is correct
12 Correct 15 ms 76464 KB Output is correct
13 Correct 16 ms 78428 KB Output is correct
14 Correct 16 ms 76376 KB Output is correct
15 Correct 16 ms 76636 KB Output is correct
16 Correct 16 ms 76424 KB Output is correct
17 Correct 17 ms 78432 KB Output is correct
18 Correct 16 ms 76208 KB Output is correct
19 Correct 18 ms 76376 KB Output is correct
20 Correct 17 ms 78428 KB Output is correct
21 Correct 17 ms 78428 KB Output is correct
22 Correct 15 ms 78408 KB Output is correct
23 Correct 19 ms 82520 KB Output is correct
24 Correct 19 ms 82524 KB Output is correct
25 Correct 19 ms 82524 KB Output is correct
26 Correct 20 ms 82520 KB Output is correct
27 Correct 19 ms 82524 KB Output is correct
28 Correct 19 ms 80476 KB Output is correct
29 Correct 19 ms 80476 KB Output is correct
30 Correct 20 ms 80584 KB Output is correct
31 Correct 19 ms 80476 KB Output is correct
32 Correct 20 ms 82780 KB Output is correct
33 Correct 18 ms 82744 KB Output is correct
34 Correct 19 ms 80732 KB Output is correct
35 Correct 21 ms 82524 KB Output is correct
36 Correct 19 ms 78456 KB Output is correct
37 Correct 17 ms 78428 KB Output is correct
38 Correct 18 ms 80732 KB Output is correct
39 Correct 19 ms 82524 KB Output is correct
40 Correct 19 ms 82780 KB Output is correct
41 Correct 18 ms 82524 KB Output is correct
42 Correct 19 ms 82520 KB Output is correct
43 Correct 19 ms 82780 KB Output is correct
44 Correct 19 ms 82524 KB Output is correct
45 Correct 947 ms 106440 KB Output is correct
46 Correct 933 ms 106124 KB Output is correct
47 Correct 928 ms 105996 KB Output is correct
48 Correct 881 ms 106068 KB Output is correct
49 Correct 928 ms 105560 KB Output is correct
50 Correct 862 ms 105708 KB Output is correct
51 Correct 948 ms 105764 KB Output is correct
52 Correct 961 ms 106100 KB Output is correct
53 Correct 928 ms 106156 KB Output is correct
54 Correct 885 ms 118400 KB Output is correct
55 Correct 865 ms 119768 KB Output is correct
56 Correct 789 ms 117532 KB Output is correct
57 Correct 783 ms 116448 KB Output is correct
58 Correct 245 ms 103224 KB Output is correct
59 Correct 220 ms 103484 KB Output is correct
60 Correct 327 ms 129528 KB Output is correct
61 Correct 614 ms 108112 KB Output is correct
62 Correct 1032 ms 121748 KB Output is correct
63 Correct 670 ms 107184 KB Output is correct
64 Correct 639 ms 108060 KB Output is correct
65 Correct 779 ms 122172 KB Output is correct
66 Correct 712 ms 107044 KB Output is correct