답안 #964646

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
964646 2024-04-17T09:15:39 Z thinknoexit Fireworks (APIO16_fireworks) C++17
100 / 100
217 ms 94480 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 300300;
bool leaf[N];
vector<pair<int, ll>> adj[N];
ll ans[N];
priority_queue<ll> l[N];
priority_queue<ll, vector<ll>, greater<ll>> r[N];
void dfs(int v, ll p = 0ll, ll d = 0ll) {
    if (leaf[v]) {
        l[v].push(p);
        r[v].push(p);
        return;
    }
    for (auto& [x, w] : adj[v]) {
        dfs(x, w, d + w);
        ans[v] += ans[x];
        if (l[v].empty()) {
            swap(l[v], l[x]);
            swap(r[v], r[x]);
            continue;
        }
        if (l[x].size() + r[x].size() > l[v].size() + r[v].size()) {
            swap(l[v], l[x]);
            swap(r[v], r[x]);
        }
        while (!l[x].empty()) {
            ll now = l[x].top();
            l[x].pop();
            ll nowr = r[v].top();
            if (now <= nowr) l[v].push(now);
            else {
                ans[v] += now - nowr;
                l[v].push(nowr);
                r[v].pop();
                r[v].push(now);
            }
        }
        while (!r[x].empty()) {
            ll now = r[x].top();
            r[x].pop();
            ll nowl = l[v].top();
            if (nowl <= now) r[v].push(now);
            else {
                ans[v] += nowl - now;
                r[v].push(nowl);
                l[v].pop();
                l[v].push(now);
            }
        }
    }
    ll nl = l[v].top() + p;
    l[v].pop();
    l[v].push(nl);
    ll nr = r[v].top() + p;
    while (!r[v].empty()) r[v].pop();
    r[v].push(nr);
}
int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    for (int i = 2;i <= n + m;i++) {
        int p;
        ll c;
        cin >> p >> c;
        adj[p].push_back({ i, c });
    }
    for (int i = n + 1;i <= n + m;i++) leaf[i] = 1;
    dfs(1);
    cout << ans[1] << '\n';
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 28508 KB Output is correct
2 Correct 7 ms 28508 KB Output is correct
3 Correct 6 ms 28688 KB Output is correct
4 Correct 8 ms 28504 KB Output is correct
5 Correct 8 ms 28504 KB Output is correct
6 Correct 6 ms 28508 KB Output is correct
7 Correct 7 ms 28624 KB Output is correct
8 Correct 6 ms 28508 KB Output is correct
9 Correct 7 ms 28508 KB Output is correct
10 Correct 6 ms 28760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 28680 KB Output is correct
2 Correct 6 ms 28676 KB Output is correct
3 Correct 7 ms 28508 KB Output is correct
4 Correct 7 ms 28668 KB Output is correct
5 Correct 6 ms 28760 KB Output is correct
6 Correct 7 ms 28508 KB Output is correct
7 Correct 6 ms 28668 KB Output is correct
8 Correct 7 ms 28508 KB Output is correct
9 Correct 7 ms 28504 KB Output is correct
10 Correct 8 ms 28508 KB Output is correct
11 Correct 6 ms 28508 KB Output is correct
12 Correct 6 ms 28508 KB Output is correct
13 Correct 8 ms 28672 KB Output is correct
14 Correct 7 ms 28724 KB Output is correct
15 Correct 6 ms 28508 KB Output is correct
16 Correct 6 ms 28760 KB Output is correct
17 Correct 7 ms 28692 KB Output is correct
18 Correct 6 ms 28504 KB Output is correct
19 Correct 8 ms 28508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 28508 KB Output is correct
2 Correct 7 ms 28508 KB Output is correct
3 Correct 6 ms 28688 KB Output is correct
4 Correct 8 ms 28504 KB Output is correct
5 Correct 8 ms 28504 KB Output is correct
6 Correct 6 ms 28508 KB Output is correct
7 Correct 7 ms 28624 KB Output is correct
8 Correct 6 ms 28508 KB Output is correct
9 Correct 7 ms 28508 KB Output is correct
10 Correct 6 ms 28760 KB Output is correct
11 Correct 7 ms 28680 KB Output is correct
12 Correct 6 ms 28676 KB Output is correct
13 Correct 7 ms 28508 KB Output is correct
14 Correct 7 ms 28668 KB Output is correct
15 Correct 6 ms 28760 KB Output is correct
16 Correct 7 ms 28508 KB Output is correct
17 Correct 6 ms 28668 KB Output is correct
18 Correct 7 ms 28508 KB Output is correct
19 Correct 7 ms 28504 KB Output is correct
20 Correct 8 ms 28508 KB Output is correct
21 Correct 6 ms 28508 KB Output is correct
22 Correct 6 ms 28508 KB Output is correct
23 Correct 8 ms 28672 KB Output is correct
24 Correct 7 ms 28724 KB Output is correct
25 Correct 6 ms 28508 KB Output is correct
26 Correct 6 ms 28760 KB Output is correct
27 Correct 7 ms 28692 KB Output is correct
28 Correct 6 ms 28504 KB Output is correct
29 Correct 8 ms 28508 KB Output is correct
30 Correct 7 ms 28508 KB Output is correct
31 Correct 7 ms 28764 KB Output is correct
32 Correct 7 ms 28764 KB Output is correct
33 Correct 7 ms 29016 KB Output is correct
34 Correct 10 ms 28760 KB Output is correct
35 Correct 8 ms 28764 KB Output is correct
36 Correct 9 ms 28764 KB Output is correct
37 Correct 8 ms 29056 KB Output is correct
38 Correct 9 ms 28976 KB Output is correct
39 Correct 10 ms 29016 KB Output is correct
40 Correct 9 ms 29532 KB Output is correct
41 Correct 8 ms 29748 KB Output is correct
42 Correct 8 ms 28764 KB Output is correct
43 Correct 9 ms 29276 KB Output is correct
44 Correct 10 ms 29336 KB Output is correct
45 Correct 9 ms 29276 KB Output is correct
46 Correct 9 ms 29252 KB Output is correct
47 Correct 9 ms 29276 KB Output is correct
48 Correct 9 ms 29196 KB Output is correct
49 Correct 9 ms 29192 KB Output is correct
50 Correct 8 ms 29020 KB Output is correct
51 Correct 8 ms 29020 KB Output is correct
52 Correct 8 ms 29020 KB Output is correct
53 Correct 9 ms 29020 KB Output is correct
54 Correct 9 ms 29020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 28508 KB Output is correct
2 Correct 7 ms 28508 KB Output is correct
3 Correct 6 ms 28688 KB Output is correct
4 Correct 8 ms 28504 KB Output is correct
5 Correct 8 ms 28504 KB Output is correct
6 Correct 6 ms 28508 KB Output is correct
7 Correct 7 ms 28624 KB Output is correct
8 Correct 6 ms 28508 KB Output is correct
9 Correct 7 ms 28508 KB Output is correct
10 Correct 6 ms 28760 KB Output is correct
11 Correct 7 ms 28680 KB Output is correct
12 Correct 6 ms 28676 KB Output is correct
13 Correct 7 ms 28508 KB Output is correct
14 Correct 7 ms 28668 KB Output is correct
15 Correct 6 ms 28760 KB Output is correct
16 Correct 7 ms 28508 KB Output is correct
17 Correct 6 ms 28668 KB Output is correct
18 Correct 7 ms 28508 KB Output is correct
19 Correct 7 ms 28504 KB Output is correct
20 Correct 8 ms 28508 KB Output is correct
21 Correct 6 ms 28508 KB Output is correct
22 Correct 6 ms 28508 KB Output is correct
23 Correct 8 ms 28672 KB Output is correct
24 Correct 7 ms 28724 KB Output is correct
25 Correct 6 ms 28508 KB Output is correct
26 Correct 6 ms 28760 KB Output is correct
27 Correct 7 ms 28692 KB Output is correct
28 Correct 6 ms 28504 KB Output is correct
29 Correct 8 ms 28508 KB Output is correct
30 Correct 7 ms 28508 KB Output is correct
31 Correct 7 ms 28764 KB Output is correct
32 Correct 7 ms 28764 KB Output is correct
33 Correct 7 ms 29016 KB Output is correct
34 Correct 10 ms 28760 KB Output is correct
35 Correct 8 ms 28764 KB Output is correct
36 Correct 9 ms 28764 KB Output is correct
37 Correct 8 ms 29056 KB Output is correct
38 Correct 9 ms 28976 KB Output is correct
39 Correct 10 ms 29016 KB Output is correct
40 Correct 9 ms 29532 KB Output is correct
41 Correct 8 ms 29748 KB Output is correct
42 Correct 8 ms 28764 KB Output is correct
43 Correct 9 ms 29276 KB Output is correct
44 Correct 10 ms 29336 KB Output is correct
45 Correct 9 ms 29276 KB Output is correct
46 Correct 9 ms 29252 KB Output is correct
47 Correct 9 ms 29276 KB Output is correct
48 Correct 9 ms 29196 KB Output is correct
49 Correct 9 ms 29192 KB Output is correct
50 Correct 8 ms 29020 KB Output is correct
51 Correct 8 ms 29020 KB Output is correct
52 Correct 8 ms 29020 KB Output is correct
53 Correct 9 ms 29020 KB Output is correct
54 Correct 9 ms 29020 KB Output is correct
55 Correct 14 ms 29532 KB Output is correct
56 Correct 30 ms 32480 KB Output is correct
57 Correct 50 ms 35364 KB Output is correct
58 Correct 73 ms 37044 KB Output is correct
59 Correct 105 ms 39980 KB Output is correct
60 Correct 117 ms 42904 KB Output is correct
61 Correct 156 ms 45100 KB Output is correct
62 Correct 172 ms 46796 KB Output is correct
63 Correct 204 ms 50040 KB Output is correct
64 Correct 217 ms 51172 KB Output is correct
65 Correct 108 ms 94480 KB Output is correct
66 Correct 103 ms 94380 KB Output is correct
67 Correct 91 ms 43056 KB Output is correct
68 Correct 122 ms 76728 KB Output is correct
69 Correct 158 ms 73260 KB Output is correct
70 Correct 160 ms 73080 KB Output is correct
71 Correct 167 ms 71612 KB Output is correct
72 Correct 163 ms 71644 KB Output is correct
73 Correct 146 ms 67144 KB Output is correct
74 Correct 162 ms 66344 KB Output is correct
75 Correct 155 ms 64960 KB Output is correct
76 Correct 157 ms 65556 KB Output is correct
77 Correct 171 ms 64072 KB Output is correct
78 Correct 168 ms 62944 KB Output is correct
79 Correct 143 ms 64224 KB Output is correct