답안 #851872

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
851872 2023-09-20T17:44:19 Z danikoynov 별자리 3 (JOI20_constellation3) C++14
100 / 100
347 ms 68688 KB
/**
 ____ ____ ____ ____ ____ ____
||l |||e |||i |||n |||a |||d ||
||__|||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|/__\|

**/

#include<bits/stdc++.h>
#define endl '\n'

using namespace std;
typedef long long ll;

void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const int maxn = 2e5 + 10;

struct point
{
    int x, y;
    ll c;
};

bool cmp(point p1, point p2)
{
    return p1.y < p2.y;
}

point star[maxn];
int n, m, a[maxn];
vector < pair < int, ll > > loc_star[maxn];
void input()
{
    cin >> n;
    for (int i = 1; i <= n; i ++)
        cin >> a[i];

    cin >> m;
    for (int i = 1; i <= m; i ++)
    {
        cin >> star[i].x >> star[i].y >> star[i].c;
        loc_star[star[i].x].push_back({star[i].y, star[i].c});
    }

    ///sort(star + 1, star + m + 1, cmp);
}

int lc[maxn], rc[maxn], par[maxn];
int cartesian_tree()
{
    for (int i = 1; i <= n; i ++)
    {
        lc[i] = rc[i] = -1;
        par[i] = 0;
    }

    stack < int > st;
    int root = -1;
    for (int i = 1; i <= n; i ++)
    {
        while(!st.empty() && a[st.top()] < a[i])
        {
            lc[i] = st.top();
            st.pop();
        }

        if (st.empty())
            root = i;
        else
        {
            par[i] = st.top();
            rc[st.top()] = i;
        }

        if (lc[i] != -1)
            par[lc[i]] = i;

        st.push(i);
    }

    return root;
    /**cout << "cartesian" << endl;

    for (int i = 1; i <= n; i ++)
        cout << i << " " << lc[i] << " " << rc[i] << endl;*/

}

const int maxlog = 21;

int tin[maxn], tout[maxn], timer;
int jump[maxlog][maxn];

vector < pair < int, ll > > links[maxn];
void create_links(int v)
{
    jump[0][v] = par[v];
    for (int j = 1; j < maxlog; j ++)
        jump[j][v] = jump[j - 1][jump[j - 1][v]];

    for (pair < int, ll > cur : loc_star[v])
    {
        int to = v;
        for (int bit = maxlog - 1; bit >= 0; bit --)
        {
            if (jump[bit][to] == 0)
                continue;
            if (a[jump[bit][to]] < cur.first)
                to = jump[bit][to];
        }

        links[to].push_back({v, cur.second});
    }

    tin[v] = ++ timer;
    if (lc[v] != -1)
        create_links(lc[v]);
    if (rc[v] != -1)
        create_links(rc[v]);
    tout[v] = timer;
}

struct bit
{
    ll fen[maxn];

    void update(int v, ll val)
    {
        for (int i = v; i < maxn; i += (i & -i))
            fen[i] += val;
    }

    ll query(int v)
    {
        ll sum = 0;
        for (int i = v; i > 0; i -= (i & -i))
            sum += fen[i];
        return sum;
    }

    void range_update(int l, int r, ll val)
    {
        update(l, val);
        update(r + 1, - val);
    }
};

bit tree_above, tree_under;

ll dp[maxn];
void dfs(int v)
{

    ll sum_dp = 0;
    if (lc[v] != -1)
    {
        dfs(lc[v]);
        sum_dp += dp[lc[v]];
    }
    if (rc[v] != -1)
    {
        dfs(rc[v]);
        sum_dp += dp[rc[v]];
    }

    dp[v] = sum_dp;
    tree_under.range_update(tin[v], tout[v], sum_dp);

    for (pair < int, ll > cur : links[v])
    {
        //cout << v << " : " << cur.first << " : " << cur.second << endl;
        dp[v] = max(dp[v], cur.second + tree_under.query(tin[cur.first]) - tree_above.query(tin[cur.first]));
    }
    //cout << "vertex " << v << " " << dp[v] << endl;
    tree_above.range_update(tin[v], tout[v], dp[v]);
}
void solve()
{
    input();
    int root = cartesian_tree();
    create_links(root);
    dfs(root);
    /// remember to subtract
    ll ans = 0;
    for (int i = 1; i <= m; i ++)
        ans += star[i].c;
    ans -= dp[root];
    cout << ans << endl;
}

int main()
{
    speed();
    solve();
    return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 37212 KB Output is correct
2 Correct 5 ms 37208 KB Output is correct
3 Correct 6 ms 37208 KB Output is correct
4 Correct 5 ms 37208 KB Output is correct
5 Correct 5 ms 37208 KB Output is correct
6 Correct 6 ms 37208 KB Output is correct
7 Correct 6 ms 37208 KB Output is correct
8 Correct 5 ms 37408 KB Output is correct
9 Correct 5 ms 37208 KB Output is correct
10 Correct 6 ms 37208 KB Output is correct
11 Correct 5 ms 37208 KB Output is correct
12 Correct 5 ms 37208 KB Output is correct
13 Correct 6 ms 37208 KB Output is correct
14 Correct 5 ms 37208 KB Output is correct
15 Correct 5 ms 37208 KB Output is correct
16 Correct 5 ms 37208 KB Output is correct
17 Correct 5 ms 37208 KB Output is correct
18 Correct 5 ms 37208 KB Output is correct
19 Correct 5 ms 37212 KB Output is correct
20 Correct 5 ms 37208 KB Output is correct
21 Correct 5 ms 37208 KB Output is correct
22 Correct 5 ms 37464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 37212 KB Output is correct
2 Correct 5 ms 37208 KB Output is correct
3 Correct 6 ms 37208 KB Output is correct
4 Correct 5 ms 37208 KB Output is correct
5 Correct 5 ms 37208 KB Output is correct
6 Correct 6 ms 37208 KB Output is correct
7 Correct 6 ms 37208 KB Output is correct
8 Correct 5 ms 37408 KB Output is correct
9 Correct 5 ms 37208 KB Output is correct
10 Correct 6 ms 37208 KB Output is correct
11 Correct 5 ms 37208 KB Output is correct
12 Correct 5 ms 37208 KB Output is correct
13 Correct 6 ms 37208 KB Output is correct
14 Correct 5 ms 37208 KB Output is correct
15 Correct 5 ms 37208 KB Output is correct
16 Correct 5 ms 37208 KB Output is correct
17 Correct 5 ms 37208 KB Output is correct
18 Correct 5 ms 37208 KB Output is correct
19 Correct 5 ms 37212 KB Output is correct
20 Correct 5 ms 37208 KB Output is correct
21 Correct 5 ms 37208 KB Output is correct
22 Correct 5 ms 37464 KB Output is correct
23 Correct 7 ms 37464 KB Output is correct
24 Correct 7 ms 37468 KB Output is correct
25 Correct 7 ms 37468 KB Output is correct
26 Correct 7 ms 37464 KB Output is correct
27 Correct 6 ms 37464 KB Output is correct
28 Correct 6 ms 37464 KB Output is correct
29 Correct 6 ms 37464 KB Output is correct
30 Correct 6 ms 37464 KB Output is correct
31 Correct 7 ms 37464 KB Output is correct
32 Correct 6 ms 37464 KB Output is correct
33 Correct 6 ms 37664 KB Output is correct
34 Correct 7 ms 37464 KB Output is correct
35 Correct 6 ms 37464 KB Output is correct
36 Correct 6 ms 37464 KB Output is correct
37 Correct 6 ms 37468 KB Output is correct
38 Correct 6 ms 37464 KB Output is correct
39 Correct 7 ms 37472 KB Output is correct
40 Correct 6 ms 37464 KB Output is correct
41 Correct 6 ms 37464 KB Output is correct
42 Correct 6 ms 37464 KB Output is correct
43 Correct 6 ms 37464 KB Output is correct
44 Correct 7 ms 37464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 37212 KB Output is correct
2 Correct 5 ms 37208 KB Output is correct
3 Correct 6 ms 37208 KB Output is correct
4 Correct 5 ms 37208 KB Output is correct
5 Correct 5 ms 37208 KB Output is correct
6 Correct 6 ms 37208 KB Output is correct
7 Correct 6 ms 37208 KB Output is correct
8 Correct 5 ms 37408 KB Output is correct
9 Correct 5 ms 37208 KB Output is correct
10 Correct 6 ms 37208 KB Output is correct
11 Correct 5 ms 37208 KB Output is correct
12 Correct 5 ms 37208 KB Output is correct
13 Correct 6 ms 37208 KB Output is correct
14 Correct 5 ms 37208 KB Output is correct
15 Correct 5 ms 37208 KB Output is correct
16 Correct 5 ms 37208 KB Output is correct
17 Correct 5 ms 37208 KB Output is correct
18 Correct 5 ms 37208 KB Output is correct
19 Correct 5 ms 37212 KB Output is correct
20 Correct 5 ms 37208 KB Output is correct
21 Correct 5 ms 37208 KB Output is correct
22 Correct 5 ms 37464 KB Output is correct
23 Correct 7 ms 37464 KB Output is correct
24 Correct 7 ms 37468 KB Output is correct
25 Correct 7 ms 37468 KB Output is correct
26 Correct 7 ms 37464 KB Output is correct
27 Correct 6 ms 37464 KB Output is correct
28 Correct 6 ms 37464 KB Output is correct
29 Correct 6 ms 37464 KB Output is correct
30 Correct 6 ms 37464 KB Output is correct
31 Correct 7 ms 37464 KB Output is correct
32 Correct 6 ms 37464 KB Output is correct
33 Correct 6 ms 37664 KB Output is correct
34 Correct 7 ms 37464 KB Output is correct
35 Correct 6 ms 37464 KB Output is correct
36 Correct 6 ms 37464 KB Output is correct
37 Correct 6 ms 37468 KB Output is correct
38 Correct 6 ms 37464 KB Output is correct
39 Correct 7 ms 37472 KB Output is correct
40 Correct 6 ms 37464 KB Output is correct
41 Correct 6 ms 37464 KB Output is correct
42 Correct 6 ms 37464 KB Output is correct
43 Correct 6 ms 37464 KB Output is correct
44 Correct 7 ms 37464 KB Output is correct
45 Correct 131 ms 49016 KB Output is correct
46 Correct 148 ms 48468 KB Output is correct
47 Correct 138 ms 48976 KB Output is correct
48 Correct 134 ms 48428 KB Output is correct
49 Correct 129 ms 48536 KB Output is correct
50 Correct 128 ms 48464 KB Output is correct
51 Correct 128 ms 48720 KB Output is correct
52 Correct 137 ms 48980 KB Output is correct
53 Correct 129 ms 48664 KB Output is correct
54 Correct 347 ms 61264 KB Output is correct
55 Correct 247 ms 57936 KB Output is correct
56 Correct 225 ms 56068 KB Output is correct
57 Correct 288 ms 54604 KB Output is correct
58 Correct 157 ms 57680 KB Output is correct
59 Correct 160 ms 57424 KB Output is correct
60 Correct 119 ms 66896 KB Output is correct
61 Correct 133 ms 48328 KB Output is correct
62 Correct 254 ms 63312 KB Output is correct
63 Correct 130 ms 53192 KB Output is correct
64 Correct 141 ms 52824 KB Output is correct
65 Correct 245 ms 68688 KB Output is correct
66 Correct 143 ms 53576 KB Output is correct