답안 #770196

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
770196 2023-06-30T23:59:28 Z PurpleCrayon 별자리 3 (JOI20_constellation3) C++17
35 / 100
1500 ms 53624 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, int 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]);
    if (bot == top) return 0;
    return dp[bro[bot]] + f(par[bot], 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 5 ms 11344 KB Output is correct
2 Correct 5 ms 11220 KB Output is correct
3 Correct 5 ms 11228 KB Output is correct
4 Correct 5 ms 11348 KB Output is correct
5 Correct 5 ms 11288 KB Output is correct
6 Correct 5 ms 11220 KB Output is correct
7 Correct 5 ms 11220 KB Output is correct
8 Correct 5 ms 11352 KB Output is correct
9 Correct 5 ms 11220 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 11304 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 11348 KB Output is correct
18 Correct 5 ms 11236 KB Output is correct
19 Correct 5 ms 11348 KB Output is correct
20 Correct 5 ms 11348 KB Output is correct
21 Correct 5 ms 11348 KB Output is correct
22 Correct 5 ms 11220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 11344 KB Output is correct
2 Correct 5 ms 11220 KB Output is correct
3 Correct 5 ms 11228 KB Output is correct
4 Correct 5 ms 11348 KB Output is correct
5 Correct 5 ms 11288 KB Output is correct
6 Correct 5 ms 11220 KB Output is correct
7 Correct 5 ms 11220 KB Output is correct
8 Correct 5 ms 11352 KB Output is correct
9 Correct 5 ms 11220 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 11304 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 11348 KB Output is correct
18 Correct 5 ms 11236 KB Output is correct
19 Correct 5 ms 11348 KB Output is correct
20 Correct 5 ms 11348 KB Output is correct
21 Correct 5 ms 11348 KB Output is correct
22 Correct 5 ms 11220 KB Output is correct
23 Correct 6 ms 11476 KB Output is correct
24 Correct 6 ms 11568 KB Output is correct
25 Correct 6 ms 11476 KB Output is correct
26 Correct 6 ms 11476 KB Output is correct
27 Correct 6 ms 11476 KB Output is correct
28 Correct 6 ms 11580 KB Output is correct
29 Correct 6 ms 11476 KB Output is correct
30 Correct 6 ms 11540 KB Output is correct
31 Correct 6 ms 11476 KB Output is correct
32 Correct 7 ms 11604 KB Output is correct
33 Correct 8 ms 11604 KB Output is correct
34 Correct 7 ms 11604 KB Output is correct
35 Correct 7 ms 11604 KB Output is correct
36 Correct 8 ms 11640 KB Output is correct
37 Correct 7 ms 11604 KB Output is correct
38 Correct 6 ms 11656 KB Output is correct
39 Correct 6 ms 11476 KB Output is correct
40 Correct 8 ms 11604 KB Output is correct
41 Correct 6 ms 11548 KB Output is correct
42 Correct 6 ms 11476 KB Output is correct
43 Correct 7 ms 11608 KB Output is correct
44 Correct 6 ms 11476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 11344 KB Output is correct
2 Correct 5 ms 11220 KB Output is correct
3 Correct 5 ms 11228 KB Output is correct
4 Correct 5 ms 11348 KB Output is correct
5 Correct 5 ms 11288 KB Output is correct
6 Correct 5 ms 11220 KB Output is correct
7 Correct 5 ms 11220 KB Output is correct
8 Correct 5 ms 11352 KB Output is correct
9 Correct 5 ms 11220 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 11304 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 11348 KB Output is correct
18 Correct 5 ms 11236 KB Output is correct
19 Correct 5 ms 11348 KB Output is correct
20 Correct 5 ms 11348 KB Output is correct
21 Correct 5 ms 11348 KB Output is correct
22 Correct 5 ms 11220 KB Output is correct
23 Correct 6 ms 11476 KB Output is correct
24 Correct 6 ms 11568 KB Output is correct
25 Correct 6 ms 11476 KB Output is correct
26 Correct 6 ms 11476 KB Output is correct
27 Correct 6 ms 11476 KB Output is correct
28 Correct 6 ms 11580 KB Output is correct
29 Correct 6 ms 11476 KB Output is correct
30 Correct 6 ms 11540 KB Output is correct
31 Correct 6 ms 11476 KB Output is correct
32 Correct 7 ms 11604 KB Output is correct
33 Correct 8 ms 11604 KB Output is correct
34 Correct 7 ms 11604 KB Output is correct
35 Correct 7 ms 11604 KB Output is correct
36 Correct 8 ms 11640 KB Output is correct
37 Correct 7 ms 11604 KB Output is correct
38 Correct 6 ms 11656 KB Output is correct
39 Correct 6 ms 11476 KB Output is correct
40 Correct 8 ms 11604 KB Output is correct
41 Correct 6 ms 11548 KB Output is correct
42 Correct 6 ms 11476 KB Output is correct
43 Correct 7 ms 11608 KB Output is correct
44 Correct 6 ms 11476 KB Output is correct
45 Correct 167 ms 39824 KB Output is correct
46 Correct 172 ms 39480 KB Output is correct
47 Correct 168 ms 39280 KB Output is correct
48 Correct 163 ms 39740 KB Output is correct
49 Correct 162 ms 38840 KB Output is correct
50 Correct 159 ms 38888 KB Output is correct
51 Correct 166 ms 39080 KB Output is correct
52 Correct 172 ms 39628 KB Output is correct
53 Correct 166 ms 39532 KB Output is correct
54 Execution timed out 1582 ms 53624 KB Time limit exceeded
55 Halted 0 ms 0 KB -