제출 #874891

#제출 시각아이디문제언어결과실행 시간메모리
874891maomao90Magic Tree (CEOI19_magictree)C++17
100 / 100
131 ms49492 KiB

// 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;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...