Submission #547669

#TimeUsernameProblemLanguageResultExecution timeMemory
547669gimabd30Magic Tree (CEOI19_magictree)C++17
100 / 100
171 ms39884 KiB
//#define _GLIBCXX_DEBUG

//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>

using namespace std;

#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;

template<typename T>
using ordered_set = tree<T, null_type, less < T>, rb_tree_tag, tree_order_statistics_node_update>;

template<typename T>
using normal_queue = priority_queue <T, vector<T>, greater<>>;

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

#define ll long long
#define trace(x) cout << #x << ": " << (x) << endl;
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define uniq(x) x.resize(unique(all(x)) - begin(x))
#define ld long double
#define sz(s) ((int) size(s))
#define pii pair<int, int>
#define mp(x, y) make_pair(x, y)
#define int128 __int128
#define pb push_back
#define eb emplace_back


template<typename T>
bool ckmn(T &x, T y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}

template<typename T>
bool ckmx(T &x, T y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}

int bit(int x, int b) {
    return (x >> b) & 1;
}


int rand(int l, int r) { return (int) ((ll) rnd() % (r - l + 1)) + l; }

//soryan za musor sverhu

const ll infL = 3e18;
const int infI = 1e9 + 7;
const int N = 100001;
const ll mod = 1e9 + 7;
const ld eps = 1e-9;

int t[N], w[N], p[N];

map<int, ll> mp[N]; //dp[v][t] - answer if the max time when we cut in subtree of v is <= t


int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m, k;
    cin >> n >> m >> k;
    for (int i = 1; i < n; ++i) {
        cin >> p[i];
        --p[i];
    }
    for (int i = 0; i < m; ++i) {
        int v, d, ww;
        cin >> v >> d >> ww;
        --v;
        t[v] = d, w[v] = ww;
    }
    for (int i = n - 1; i > 0; --i) {
        if (w[i] != 0) {
            mp[i][t[i]] += w[i];
            while (true) {
                auto it = mp[i].upper_bound(t[i]);
                if (it == end(mp[i])) break;
                if (it->second > w[i]) {
                    it->second -= w[i];
                    break;
                } else {
                    w[i] -= it->second;
                    mp[i].erase(it);
                }
            }
        }
        if (sz(mp[p[i]]) < sz(mp[i])) swap(mp[p[i]], mp[i]);
        for (auto [aa, bb] : mp[i]) mp[p[i]][aa] += bb;
    }
    ll ans = 0;
    for (auto [aa, bb] : mp[0]) ans += bb;
    cout << ans;
    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...