답안 #1034975

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1034975 2024-07-26T01:34:43 Z underwaterkillerwhale Magic Tree (CEOI19_magictree) C++17
100 / 100
89 ms 35156 KB
#include <bits/stdc++.h>
#define se              second
#define fs              first
#define mp              make_pair
#define pb              push_back
#define ll              long long
#define ii              pair<ll,ll>
#define ld              long double
#define SZ(v)           (int)v.size()
#define ALL(v)          v.begin(), v.end()
#define bit(msk, i)     ((msk >> i) & 1)
#define iter(id, v)     for(auto id : v)
#define rep(i,m,n)      for(int i=(m); i<=(n); i++)
#define reb(i,m,n)      for(int i=(m); i>=(n); i--)
using namespace std;

mt19937_64 rd(chrono :: steady_clock :: now().time_since_epoch().count());
ll Rand(ll l, ll r) { return uniform_int_distribution<ll> (l, r)(rd); }

const int N = 1e5 + 7;
const int Mod = 1e9 + 7;
const int szBL = 320;
const int  INF = 1e9;
const int BASE = 137;

int n, m, K;
int D[N];
ll W[N];
vector<int> ke[N];
map<int, ll> dp[N];
int szC[N];

void dfs (int u, int p) {
    szC[u] = 1;
    int mxV = -1;
    iter (&v, ke[u]) {
        if (v == p) continue;
        dfs(v, u);
        if (mxV == -1 || szC[v] > szC[mxV]) mxV = v;
        szC[u] += szC[v];
    }
    if (mxV != -1) {
        swap(dp[u], dp[mxV]);
    }
    iter (&v, ke[u]) {
        if (v == p || v == mxV) continue;
        iter (&id, dp[v]) {
            dp[u][id.fs] += id.se;
        }
        map<int,ll> ().swap(dp[v]);
    }
    ll val = W[u];
    dp[u][D[u]] += val;
    auto it = dp[u].lower_bound(D[u] + 1);
    while (it != dp[u].end()) {
        if (it->se <= val) {
            val -= it->se;
            it = dp[u].erase(it);
        }
        else {
            it->se -= val;
            break;
        }
    }
}
/*
    (i, dp[i] - dp[i - 1]);


*/

void solution () {
    cin >> n >> m >> K;
    rep (i, 1, n - 1) {
        int u, v;
        cin >> v;
        u = i + 1;
        ke[u].push_back(v);
        ke[v].push_back(u);
    }
    rep (i, 1, m) {
        ll v, d, w;
        cin >> v >> d >> w;
        D[v] = d;
        W[v] = w;
    }
    dfs(1, 0);
    dp[1][K] += 0;
    for(auto it = dp[1].begin(); it != prev(dp[1].end()); ++it) {
        next(it)->se += it->se;
    }
    cout << dp[1][K] <<"\n";
}

#define file(name) freopen(name".inp", "r", stdin); \
freopen(name".out", "w", stdout);

int main () {
//    file("c");
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    ll num_Test = 1;
//    cin >> num_Test;
    while(num_Test--)
        solution();
}
/*
no bug challenge

*/
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7516 KB Output is correct
2 Correct 3 ms 7516 KB Output is correct
3 Correct 3 ms 7516 KB Output is correct
4 Correct 3 ms 7516 KB Output is correct
5 Correct 4 ms 7516 KB Output is correct
6 Correct 3 ms 7516 KB Output is correct
7 Correct 3 ms 7328 KB Output is correct
8 Correct 3 ms 7476 KB Output is correct
9 Correct 3 ms 7512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 15372 KB Output is correct
2 Correct 34 ms 20824 KB Output is correct
3 Correct 85 ms 20900 KB Output is correct
4 Correct 53 ms 20936 KB Output is correct
5 Correct 69 ms 20308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7516 KB Output is correct
2 Correct 3 ms 7768 KB Output is correct
3 Correct 3 ms 7520 KB Output is correct
4 Correct 46 ms 31212 KB Output is correct
5 Correct 62 ms 35156 KB Output is correct
6 Correct 51 ms 31568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 14208 KB Output is correct
2 Correct 42 ms 14428 KB Output is correct
3 Correct 41 ms 21148 KB Output is correct
4 Correct 32 ms 20448 KB Output is correct
5 Correct 40 ms 31324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7516 KB Output is correct
2 Correct 3 ms 7516 KB Output is correct
3 Correct 3 ms 7516 KB Output is correct
4 Correct 3 ms 7516 KB Output is correct
5 Correct 4 ms 7516 KB Output is correct
6 Correct 3 ms 7516 KB Output is correct
7 Correct 3 ms 7328 KB Output is correct
8 Correct 3 ms 7476 KB Output is correct
9 Correct 3 ms 7512 KB Output is correct
10 Correct 48 ms 13660 KB Output is correct
11 Correct 46 ms 13660 KB Output is correct
12 Correct 37 ms 20572 KB Output is correct
13 Correct 30 ms 19924 KB Output is correct
14 Correct 39 ms 30896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 8540 KB Output is correct
2 Correct 29 ms 12776 KB Output is correct
3 Correct 30 ms 12884 KB Output is correct
4 Correct 29 ms 12888 KB Output is correct
5 Correct 18 ms 18896 KB Output is correct
6 Correct 29 ms 16220 KB Output is correct
7 Correct 24 ms 19692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7516 KB Output is correct
2 Correct 3 ms 7516 KB Output is correct
3 Correct 3 ms 7516 KB Output is correct
4 Correct 3 ms 7516 KB Output is correct
5 Correct 4 ms 7516 KB Output is correct
6 Correct 3 ms 7516 KB Output is correct
7 Correct 3 ms 7328 KB Output is correct
8 Correct 3 ms 7476 KB Output is correct
9 Correct 3 ms 7512 KB Output is correct
10 Correct 3 ms 7516 KB Output is correct
11 Correct 3 ms 7768 KB Output is correct
12 Correct 3 ms 7520 KB Output is correct
13 Correct 46 ms 31212 KB Output is correct
14 Correct 62 ms 35156 KB Output is correct
15 Correct 51 ms 31568 KB Output is correct
16 Correct 48 ms 13660 KB Output is correct
17 Correct 46 ms 13660 KB Output is correct
18 Correct 37 ms 20572 KB Output is correct
19 Correct 30 ms 19924 KB Output is correct
20 Correct 39 ms 30896 KB Output is correct
21 Correct 13 ms 9052 KB Output is correct
22 Correct 48 ms 13812 KB Output is correct
23 Correct 60 ms 15732 KB Output is correct
24 Correct 76 ms 17744 KB Output is correct
25 Correct 49 ms 20180 KB Output is correct
26 Correct 64 ms 21332 KB Output is correct
27 Correct 55 ms 22608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7516 KB Output is correct
2 Correct 3 ms 7516 KB Output is correct
3 Correct 3 ms 7516 KB Output is correct
4 Correct 3 ms 7516 KB Output is correct
5 Correct 4 ms 7516 KB Output is correct
6 Correct 3 ms 7516 KB Output is correct
7 Correct 3 ms 7328 KB Output is correct
8 Correct 3 ms 7476 KB Output is correct
9 Correct 3 ms 7512 KB Output is correct
10 Correct 43 ms 15372 KB Output is correct
11 Correct 34 ms 20824 KB Output is correct
12 Correct 85 ms 20900 KB Output is correct
13 Correct 53 ms 20936 KB Output is correct
14 Correct 69 ms 20308 KB Output is correct
15 Correct 3 ms 7516 KB Output is correct
16 Correct 3 ms 7768 KB Output is correct
17 Correct 3 ms 7520 KB Output is correct
18 Correct 46 ms 31212 KB Output is correct
19 Correct 62 ms 35156 KB Output is correct
20 Correct 51 ms 31568 KB Output is correct
21 Correct 45 ms 14208 KB Output is correct
22 Correct 42 ms 14428 KB Output is correct
23 Correct 41 ms 21148 KB Output is correct
24 Correct 32 ms 20448 KB Output is correct
25 Correct 40 ms 31324 KB Output is correct
26 Correct 48 ms 13660 KB Output is correct
27 Correct 46 ms 13660 KB Output is correct
28 Correct 37 ms 20572 KB Output is correct
29 Correct 30 ms 19924 KB Output is correct
30 Correct 39 ms 30896 KB Output is correct
31 Correct 7 ms 8540 KB Output is correct
32 Correct 29 ms 12776 KB Output is correct
33 Correct 30 ms 12884 KB Output is correct
34 Correct 29 ms 12888 KB Output is correct
35 Correct 18 ms 18896 KB Output is correct
36 Correct 29 ms 16220 KB Output is correct
37 Correct 24 ms 19692 KB Output is correct
38 Correct 13 ms 9052 KB Output is correct
39 Correct 48 ms 13812 KB Output is correct
40 Correct 60 ms 15732 KB Output is correct
41 Correct 76 ms 17744 KB Output is correct
42 Correct 49 ms 20180 KB Output is correct
43 Correct 64 ms 21332 KB Output is correct
44 Correct 55 ms 22608 KB Output is correct
45 Correct 16 ms 9308 KB Output is correct
46 Correct 57 ms 14072 KB Output is correct
47 Correct 56 ms 16216 KB Output is correct
48 Correct 89 ms 19024 KB Output is correct
49 Correct 53 ms 20912 KB Output is correct
50 Correct 70 ms 22436 KB Output is correct
51 Correct 61 ms 23272 KB Output is correct