답안 #874891

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
874891 2023-11-18T03:41:14 Z maomao90 Magic Tree (CEOI19_magictree) C++17
100 / 100
131 ms 49492 KB
// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h> 
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define RREP(i, s, e) for (int i = (s); i >= (e); i--)
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define SZ(_a) (int) _a.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<iii> viii;

#ifndef DEBUG
#define cerr if (0) cerr
#endif

const int INF = 1000000005;
const ll LINF = 1000000000000000005ll;
const int MAXN = 100005;

int n, m, k;
int p[MAXN];
map<int, ll> base[MAXN], dp[MAXN];
ll ans;

int main() {
#ifndef DEBUG
    ios::sync_with_stdio(0), cin.tie(0);
#endif
    cin >> n >> m >> k;
    REP (i, 2, n + 1) {
        cin >> p[i];
    }
    while (m--) {
        int v, d, w; cin >> v >> d >> w;
        base[v][d] += w;
    }
    REP (i, 1, n + 1) {
        if (SZ(base[i]) <= 1) {
            continue;
        }
        auto ptr = base[i].begin();
        ll val = 0;
        while (ptr != base[i].end()) {
            if (ptr -> SE <= val) {
                ptr = base[i].erase(ptr);
            } else {
                ptr -> SE -= val;
                val += ptr -> SE;
            }
        }
    }
    RREP (i, n, 2) {
        for (auto [d, w] : base[i]) {
            dp[i][d] += w;
            auto ptr = dp[i].upper_bound(d);
            ll tw = w;
            while (ptr != dp[i].end() && tw) {
                ll mn = min(tw, ptr -> SE);
                tw -= mn;
                ptr -> SE -= mn;
                if (ptr -> SE == 0) {
                    ptr = dp[i].erase(ptr);
                }
            }
        }
        if (SZ(dp[p[i]]) < SZ(dp[i])) {
            swap(dp[i], dp[p[i]]);
        }
        for (auto [d, w] : dp[i]) {
            dp[p[i]][d] += w;
        }
    }
    for (auto [d, w] : dp[1]) {
        ans += w;
    }
    cout << ans << '\n';
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9820 KB Output is correct
2 Correct 2 ms 9860 KB Output is correct
3 Correct 2 ms 9856 KB Output is correct
4 Correct 2 ms 9820 KB Output is correct
5 Correct 2 ms 9820 KB Output is correct
6 Correct 2 ms 9820 KB Output is correct
7 Correct 2 ms 9864 KB Output is correct
8 Correct 2 ms 9820 KB Output is correct
9 Correct 2 ms 9820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 22492 KB Output is correct
2 Correct 29 ms 15608 KB Output is correct
3 Correct 131 ms 49492 KB Output is correct
4 Correct 84 ms 27148 KB Output is correct
5 Correct 90 ms 32584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9816 KB Output is correct
2 Correct 2 ms 9816 KB Output is correct
3 Correct 2 ms 9820 KB Output is correct
4 Correct 33 ms 18168 KB Output is correct
5 Correct 55 ms 22308 KB Output is correct
6 Correct 46 ms 18268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 21588 KB Output is correct
2 Correct 48 ms 20816 KB Output is correct
3 Correct 50 ms 19072 KB Output is correct
4 Correct 48 ms 23120 KB Output is correct
5 Correct 30 ms 18004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9820 KB Output is correct
2 Correct 2 ms 9860 KB Output is correct
3 Correct 2 ms 9856 KB Output is correct
4 Correct 2 ms 9820 KB Output is correct
5 Correct 2 ms 9820 KB Output is correct
6 Correct 2 ms 9820 KB Output is correct
7 Correct 2 ms 9864 KB Output is correct
8 Correct 2 ms 9820 KB Output is correct
9 Correct 2 ms 9820 KB Output is correct
10 Correct 59 ms 24736 KB Output is correct
11 Correct 54 ms 23380 KB Output is correct
12 Correct 41 ms 18416 KB Output is correct
13 Correct 42 ms 22356 KB Output is correct
14 Correct 37 ms 17364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 10312 KB Output is correct
2 Correct 10 ms 10852 KB Output is correct
3 Correct 9 ms 10844 KB Output is correct
4 Correct 10 ms 11032 KB Output is correct
5 Correct 10 ms 10584 KB Output is correct
6 Correct 10 ms 10844 KB Output is correct
7 Correct 9 ms 10844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9820 KB Output is correct
2 Correct 2 ms 9860 KB Output is correct
3 Correct 2 ms 9856 KB Output is correct
4 Correct 2 ms 9820 KB Output is correct
5 Correct 2 ms 9820 KB Output is correct
6 Correct 2 ms 9820 KB Output is correct
7 Correct 2 ms 9864 KB Output is correct
8 Correct 2 ms 9820 KB Output is correct
9 Correct 2 ms 9820 KB Output is correct
10 Correct 2 ms 9816 KB Output is correct
11 Correct 2 ms 9816 KB Output is correct
12 Correct 2 ms 9820 KB Output is correct
13 Correct 33 ms 18168 KB Output is correct
14 Correct 55 ms 22308 KB Output is correct
15 Correct 46 ms 18268 KB Output is correct
16 Correct 59 ms 24736 KB Output is correct
17 Correct 54 ms 23380 KB Output is correct
18 Correct 41 ms 18416 KB Output is correct
19 Correct 42 ms 22356 KB Output is correct
20 Correct 37 ms 17364 KB Output is correct
21 Correct 17 ms 14428 KB Output is correct
22 Correct 50 ms 23636 KB Output is correct
23 Correct 65 ms 27368 KB Output is correct
24 Correct 105 ms 40780 KB Output is correct
25 Correct 68 ms 26592 KB Output is correct
26 Correct 82 ms 26868 KB Output is correct
27 Correct 60 ms 20560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9820 KB Output is correct
2 Correct 2 ms 9860 KB Output is correct
3 Correct 2 ms 9856 KB Output is correct
4 Correct 2 ms 9820 KB Output is correct
5 Correct 2 ms 9820 KB Output is correct
6 Correct 2 ms 9820 KB Output is correct
7 Correct 2 ms 9864 KB Output is correct
8 Correct 2 ms 9820 KB Output is correct
9 Correct 2 ms 9820 KB Output is correct
10 Correct 42 ms 22492 KB Output is correct
11 Correct 29 ms 15608 KB Output is correct
12 Correct 131 ms 49492 KB Output is correct
13 Correct 84 ms 27148 KB Output is correct
14 Correct 90 ms 32584 KB Output is correct
15 Correct 2 ms 9816 KB Output is correct
16 Correct 2 ms 9816 KB Output is correct
17 Correct 2 ms 9820 KB Output is correct
18 Correct 33 ms 18168 KB Output is correct
19 Correct 55 ms 22308 KB Output is correct
20 Correct 46 ms 18268 KB Output is correct
21 Correct 46 ms 21588 KB Output is correct
22 Correct 48 ms 20816 KB Output is correct
23 Correct 50 ms 19072 KB Output is correct
24 Correct 48 ms 23120 KB Output is correct
25 Correct 30 ms 18004 KB Output is correct
26 Correct 59 ms 24736 KB Output is correct
27 Correct 54 ms 23380 KB Output is correct
28 Correct 41 ms 18416 KB Output is correct
29 Correct 42 ms 22356 KB Output is correct
30 Correct 37 ms 17364 KB Output is correct
31 Correct 5 ms 10312 KB Output is correct
32 Correct 10 ms 10852 KB Output is correct
33 Correct 9 ms 10844 KB Output is correct
34 Correct 10 ms 11032 KB Output is correct
35 Correct 10 ms 10584 KB Output is correct
36 Correct 10 ms 10844 KB Output is correct
37 Correct 9 ms 10844 KB Output is correct
38 Correct 17 ms 14428 KB Output is correct
39 Correct 50 ms 23636 KB Output is correct
40 Correct 65 ms 27368 KB Output is correct
41 Correct 105 ms 40780 KB Output is correct
42 Correct 68 ms 26592 KB Output is correct
43 Correct 82 ms 26868 KB Output is correct
44 Correct 60 ms 20560 KB Output is correct
45 Correct 20 ms 14932 KB Output is correct
46 Correct 62 ms 24912 KB Output is correct
47 Correct 66 ms 29184 KB Output is correct
48 Correct 127 ms 44008 KB Output is correct
49 Correct 75 ms 27220 KB Output is correct
50 Correct 91 ms 27728 KB Output is correct
51 Correct 73 ms 21436 KB Output is correct