답안 #770197

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
770197 2023-07-01T00:00:47 Z PurpleCrayon 별자리 3 (JOI20_constellation3) C++17
100 / 100
345 ms 61932 KB
#include <bits/stdc++.h>
using namespace std;

#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 2e5+10, MOD = 998244353;
const int L = 18;

struct FT {
    vector<ll> bit;
    FT() {}
    FT(int n) {
        bit.resize(n);
    }

    void upd(int i, ll x) {
        for (; i < sz(bit); i |= i+1) bit[i] += x;
    }

    ll qry(int i) {
        ll ans = 0;
        for (++i; i > 0; i &= i-1) ans += bit[i-1];
        return ans;
    }
} bit;

int n, m, a[N], st[N], en[N], bro[N], anc[N][L];
int par[N];
vector<int> child[N];
vector<pair<int, ll>> can[N];
ll dp[N];

int tt = 0;
void dfs_st(int c) {
    st[c] = tt++;
    for (int nxt : child[c]) {
        dfs_st(nxt);
    }
    en[c] = tt-1;
}

void build_tree() {
    memset(par, -1, sizeof(par));
    vector<int> st;
    for (int i = 0; i < n; i++) {
        int last = -1;
        while (sz(st) && a[st.back()] < a[i]) {
            last = st.back();
            st.pop_back();
        }

        if (sz(st)) par[i] = st.back();
        if (last != -1) par[last] = i;
        st.push_back(i);
    }
}

ll f(int bot, int top) {
    return bit.qry(st[bot]) - bit.qry(st[top]);
}

void dfs(int c) {
    for (int nxt : child[c]) {
        dfs(nxt);
    }
    for (int nxt : child[c]) {
        bit.upd(st[nxt], dp[bro[nxt]]);
        bit.upd(en[nxt]+1, -dp[bro[nxt]]);
    }

    dp[c] = 0;
    for (int nxt : child[c]) dp[c] += dp[nxt];
    for (auto [v, x] : can[c]) {
        ll cur = x + f(v, c);
        for (int nxt : child[v]) cur += dp[nxt];
        dp[c] = max(dp[c], cur);
    }
}

void solve() {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];

    bit = FT(n);
    build_tree();
    int root = -1;
    for (int i = 0; i < n; i++) {
        if (par[i] == -1) {
            root = i;
        }
    }
    for (int i = 0; i < n; i++) if (i != root) {
        child[par[i]].push_back(i);
    }

    memset(bro, -1, sizeof(bro));
    for (int i = 0; i < n; i++) if (i != root) {
        for (int nxt : child[par[i]]) if (nxt != i) {
            bro[i] = nxt;
        }
    }
    
    dfs_st(root);

    for (int i = 0; i < n; i++) {
        anc[i][0] = par[i];
    }

    for (int k = 1; k < L; k++) {
        for (int i = 0; i < n; i++) {
            anc[i][k] = anc[i][k-1] == -1 ? -1 : anc[anc[i][k-1]][k-1];
        }
    }

    cin >> m;
    ll base = 0;
    for (int i = 0; i < m; i++) {
        int x, y; ll c; cin >> x >> y >> c, --x; base += c;

        int top = x;
        for (int k = L-1; k >= 0; k--) {
            if (anc[top][k] != -1 && a[anc[top][k]] < y) {
                top = anc[top][k];
            }
        }

        can[top].push_back({x, c});
    }


    dfs(root);
    cout << base - dp[root] << '\n';
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int T = 1;
    // cin >> T;
    while (T--) solve();
}


# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 11348 KB Output is correct
2 Correct 7 ms 11220 KB Output is correct
3 Correct 5 ms 11220 KB Output is correct
4 Correct 5 ms 11220 KB Output is correct
5 Correct 5 ms 11220 KB Output is correct
6 Correct 5 ms 11260 KB Output is correct
7 Correct 5 ms 11220 KB Output is correct
8 Correct 5 ms 11220 KB Output is correct
9 Correct 5 ms 11324 KB Output is correct
10 Correct 5 ms 11348 KB Output is correct
11 Correct 5 ms 11348 KB Output is correct
12 Correct 5 ms 11348 KB Output is correct
13 Correct 5 ms 11348 KB Output is correct
14 Correct 5 ms 11348 KB Output is correct
15 Correct 5 ms 11348 KB Output is correct
16 Correct 5 ms 11348 KB Output is correct
17 Correct 5 ms 11220 KB Output is correct
18 Correct 6 ms 11348 KB Output is correct
19 Correct 5 ms 11348 KB Output is correct
20 Correct 5 ms 11220 KB Output is correct
21 Correct 5 ms 11348 KB Output is correct
22 Correct 6 ms 11348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 11348 KB Output is correct
2 Correct 7 ms 11220 KB Output is correct
3 Correct 5 ms 11220 KB Output is correct
4 Correct 5 ms 11220 KB Output is correct
5 Correct 5 ms 11220 KB Output is correct
6 Correct 5 ms 11260 KB Output is correct
7 Correct 5 ms 11220 KB Output is correct
8 Correct 5 ms 11220 KB Output is correct
9 Correct 5 ms 11324 KB Output is correct
10 Correct 5 ms 11348 KB Output is correct
11 Correct 5 ms 11348 KB Output is correct
12 Correct 5 ms 11348 KB Output is correct
13 Correct 5 ms 11348 KB Output is correct
14 Correct 5 ms 11348 KB Output is correct
15 Correct 5 ms 11348 KB Output is correct
16 Correct 5 ms 11348 KB Output is correct
17 Correct 5 ms 11220 KB Output is correct
18 Correct 6 ms 11348 KB Output is correct
19 Correct 5 ms 11348 KB Output is correct
20 Correct 5 ms 11220 KB Output is correct
21 Correct 5 ms 11348 KB Output is correct
22 Correct 6 ms 11348 KB Output is correct
23 Correct 8 ms 11524 KB Output is correct
24 Correct 7 ms 11476 KB Output is correct
25 Correct 7 ms 11500 KB Output is correct
26 Correct 7 ms 11564 KB Output is correct
27 Correct 6 ms 11476 KB Output is correct
28 Correct 7 ms 11476 KB Output is correct
29 Correct 6 ms 11528 KB Output is correct
30 Correct 6 ms 11476 KB Output is correct
31 Correct 6 ms 11580 KB Output is correct
32 Correct 6 ms 11604 KB Output is correct
33 Correct 6 ms 11604 KB Output is correct
34 Correct 6 ms 11604 KB Output is correct
35 Correct 6 ms 11640 KB Output is correct
36 Correct 8 ms 11604 KB Output is correct
37 Correct 6 ms 11604 KB Output is correct
38 Correct 6 ms 11708 KB Output is correct
39 Correct 7 ms 11476 KB Output is correct
40 Correct 7 ms 11648 KB Output is correct
41 Correct 6 ms 11596 KB Output is correct
42 Correct 6 ms 11476 KB Output is correct
43 Correct 6 ms 11604 KB Output is correct
44 Correct 6 ms 11532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 11348 KB Output is correct
2 Correct 7 ms 11220 KB Output is correct
3 Correct 5 ms 11220 KB Output is correct
4 Correct 5 ms 11220 KB Output is correct
5 Correct 5 ms 11220 KB Output is correct
6 Correct 5 ms 11260 KB Output is correct
7 Correct 5 ms 11220 KB Output is correct
8 Correct 5 ms 11220 KB Output is correct
9 Correct 5 ms 11324 KB Output is correct
10 Correct 5 ms 11348 KB Output is correct
11 Correct 5 ms 11348 KB Output is correct
12 Correct 5 ms 11348 KB Output is correct
13 Correct 5 ms 11348 KB Output is correct
14 Correct 5 ms 11348 KB Output is correct
15 Correct 5 ms 11348 KB Output is correct
16 Correct 5 ms 11348 KB Output is correct
17 Correct 5 ms 11220 KB Output is correct
18 Correct 6 ms 11348 KB Output is correct
19 Correct 5 ms 11348 KB Output is correct
20 Correct 5 ms 11220 KB Output is correct
21 Correct 5 ms 11348 KB Output is correct
22 Correct 6 ms 11348 KB Output is correct
23 Correct 8 ms 11524 KB Output is correct
24 Correct 7 ms 11476 KB Output is correct
25 Correct 7 ms 11500 KB Output is correct
26 Correct 7 ms 11564 KB Output is correct
27 Correct 6 ms 11476 KB Output is correct
28 Correct 7 ms 11476 KB Output is correct
29 Correct 6 ms 11528 KB Output is correct
30 Correct 6 ms 11476 KB Output is correct
31 Correct 6 ms 11580 KB Output is correct
32 Correct 6 ms 11604 KB Output is correct
33 Correct 6 ms 11604 KB Output is correct
34 Correct 6 ms 11604 KB Output is correct
35 Correct 6 ms 11640 KB Output is correct
36 Correct 8 ms 11604 KB Output is correct
37 Correct 6 ms 11604 KB Output is correct
38 Correct 6 ms 11708 KB Output is correct
39 Correct 7 ms 11476 KB Output is correct
40 Correct 7 ms 11648 KB Output is correct
41 Correct 6 ms 11596 KB Output is correct
42 Correct 6 ms 11476 KB Output is correct
43 Correct 6 ms 11604 KB Output is correct
44 Correct 6 ms 11532 KB Output is correct
45 Correct 172 ms 39872 KB Output is correct
46 Correct 174 ms 39440 KB Output is correct
47 Correct 206 ms 39116 KB Output is correct
48 Correct 169 ms 39712 KB Output is correct
49 Correct 167 ms 38848 KB Output is correct
50 Correct 175 ms 39152 KB Output is correct
51 Correct 178 ms 39116 KB Output is correct
52 Correct 226 ms 39616 KB Output is correct
53 Correct 179 ms 39512 KB Output is correct
54 Correct 298 ms 50140 KB Output is correct
55 Correct 304 ms 52380 KB Output is correct
56 Correct 292 ms 50444 KB Output is correct
57 Correct 279 ms 49264 KB Output is correct
58 Correct 203 ms 53456 KB Output is correct
59 Correct 189 ms 53188 KB Output is correct
60 Correct 126 ms 61932 KB Output is correct
61 Correct 158 ms 44816 KB Output is correct
62 Correct 345 ms 57972 KB Output is correct
63 Correct 152 ms 44148 KB Output is correct
64 Correct 156 ms 43704 KB Output is correct
65 Correct 296 ms 58156 KB Output is correct
66 Correct 164 ms 43900 KB Output is correct