Submission #916083

# Submission time Handle Problem Language Result Execution time Memory
916083 2024-01-25T09:13:43 Z boris_mihov Constellation 3 (JOI20_constellation3) C++17
35 / 100
1500 ms 93268 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;
    }
};

int x[MAXN];
int y[MAXN];
int c[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;
Sparse sparse;

llong getSum(int node, int to)
{
    if (node == to)
    {
        return sum[node] - dp[node];
    }

    return sum[node] - dp[node] + getSum(par[node], to);
}

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

    llong res = sum[node];
    for (const int &idx : stars[node])
    {
        res = std::max(res, c[idx] + getSum(par[leftSegm[x[idx]]], node));
    }

    dp[node] = res;
}

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];
    }

    dfs(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 11 ms 51804 KB Output is correct
2 Correct 11 ms 51804 KB Output is correct
3 Correct 11 ms 49752 KB Output is correct
4 Correct 11 ms 51804 KB Output is correct
5 Correct 11 ms 51764 KB Output is correct
6 Correct 11 ms 49756 KB Output is correct
7 Correct 11 ms 51800 KB Output is correct
8 Correct 10 ms 51804 KB Output is correct
9 Correct 11 ms 51804 KB Output is correct
10 Correct 11 ms 49756 KB Output is correct
11 Correct 11 ms 49756 KB Output is correct
12 Correct 11 ms 49756 KB Output is correct
13 Correct 12 ms 51800 KB Output is correct
14 Correct 10 ms 49756 KB Output is correct
15 Correct 11 ms 49756 KB Output is correct
16 Correct 11 ms 49756 KB Output is correct
17 Correct 12 ms 51800 KB Output is correct
18 Correct 11 ms 49756 KB Output is correct
19 Correct 12 ms 49712 KB Output is correct
20 Correct 12 ms 52056 KB Output is correct
21 Correct 11 ms 51804 KB Output is correct
22 Correct 11 ms 51804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 51804 KB Output is correct
2 Correct 11 ms 51804 KB Output is correct
3 Correct 11 ms 49752 KB Output is correct
4 Correct 11 ms 51804 KB Output is correct
5 Correct 11 ms 51764 KB Output is correct
6 Correct 11 ms 49756 KB Output is correct
7 Correct 11 ms 51800 KB Output is correct
8 Correct 10 ms 51804 KB Output is correct
9 Correct 11 ms 51804 KB Output is correct
10 Correct 11 ms 49756 KB Output is correct
11 Correct 11 ms 49756 KB Output is correct
12 Correct 11 ms 49756 KB Output is correct
13 Correct 12 ms 51800 KB Output is correct
14 Correct 10 ms 49756 KB Output is correct
15 Correct 11 ms 49756 KB Output is correct
16 Correct 11 ms 49756 KB Output is correct
17 Correct 12 ms 51800 KB Output is correct
18 Correct 11 ms 49756 KB Output is correct
19 Correct 12 ms 49712 KB Output is correct
20 Correct 12 ms 52056 KB Output is correct
21 Correct 11 ms 51804 KB Output is correct
22 Correct 11 ms 51804 KB Output is correct
23 Correct 13 ms 55900 KB Output is correct
24 Correct 13 ms 55960 KB Output is correct
25 Correct 13 ms 55900 KB Output is correct
26 Correct 13 ms 55896 KB Output is correct
27 Correct 13 ms 55900 KB Output is correct
28 Correct 14 ms 53852 KB Output is correct
29 Correct 13 ms 53848 KB Output is correct
30 Correct 12 ms 53824 KB Output is correct
31 Correct 13 ms 53852 KB Output is correct
32 Correct 14 ms 55944 KB Output is correct
33 Correct 14 ms 55900 KB Output is correct
34 Correct 13 ms 53920 KB Output is correct
35 Correct 14 ms 55896 KB Output is correct
36 Correct 12 ms 51812 KB Output is correct
37 Correct 12 ms 51800 KB Output is correct
38 Correct 13 ms 54076 KB Output is correct
39 Correct 13 ms 55896 KB Output is correct
40 Correct 14 ms 55896 KB Output is correct
41 Correct 14 ms 55852 KB Output is correct
42 Correct 14 ms 55896 KB Output is correct
43 Correct 14 ms 55896 KB Output is correct
44 Correct 13 ms 55900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 51804 KB Output is correct
2 Correct 11 ms 51804 KB Output is correct
3 Correct 11 ms 49752 KB Output is correct
4 Correct 11 ms 51804 KB Output is correct
5 Correct 11 ms 51764 KB Output is correct
6 Correct 11 ms 49756 KB Output is correct
7 Correct 11 ms 51800 KB Output is correct
8 Correct 10 ms 51804 KB Output is correct
9 Correct 11 ms 51804 KB Output is correct
10 Correct 11 ms 49756 KB Output is correct
11 Correct 11 ms 49756 KB Output is correct
12 Correct 11 ms 49756 KB Output is correct
13 Correct 12 ms 51800 KB Output is correct
14 Correct 10 ms 49756 KB Output is correct
15 Correct 11 ms 49756 KB Output is correct
16 Correct 11 ms 49756 KB Output is correct
17 Correct 12 ms 51800 KB Output is correct
18 Correct 11 ms 49756 KB Output is correct
19 Correct 12 ms 49712 KB Output is correct
20 Correct 12 ms 52056 KB Output is correct
21 Correct 11 ms 51804 KB Output is correct
22 Correct 11 ms 51804 KB Output is correct
23 Correct 13 ms 55900 KB Output is correct
24 Correct 13 ms 55960 KB Output is correct
25 Correct 13 ms 55900 KB Output is correct
26 Correct 13 ms 55896 KB Output is correct
27 Correct 13 ms 55900 KB Output is correct
28 Correct 14 ms 53852 KB Output is correct
29 Correct 13 ms 53848 KB Output is correct
30 Correct 12 ms 53824 KB Output is correct
31 Correct 13 ms 53852 KB Output is correct
32 Correct 14 ms 55944 KB Output is correct
33 Correct 14 ms 55900 KB Output is correct
34 Correct 13 ms 53920 KB Output is correct
35 Correct 14 ms 55896 KB Output is correct
36 Correct 12 ms 51812 KB Output is correct
37 Correct 12 ms 51800 KB Output is correct
38 Correct 13 ms 54076 KB Output is correct
39 Correct 13 ms 55896 KB Output is correct
40 Correct 14 ms 55896 KB Output is correct
41 Correct 14 ms 55852 KB Output is correct
42 Correct 14 ms 55896 KB Output is correct
43 Correct 14 ms 55896 KB Output is correct
44 Correct 13 ms 55900 KB Output is correct
45 Correct 749 ms 83284 KB Output is correct
46 Correct 721 ms 83460 KB Output is correct
47 Correct 780 ms 83416 KB Output is correct
48 Correct 720 ms 83136 KB Output is correct
49 Correct 775 ms 82764 KB Output is correct
50 Correct 722 ms 82924 KB Output is correct
51 Correct 765 ms 82952 KB Output is correct
52 Correct 789 ms 83232 KB Output is correct
53 Correct 744 ms 83020 KB Output is correct
54 Execution timed out 1554 ms 93268 KB Time limit exceeded
55 Halted 0 ms 0 KB -