Submission #1083351

#TimeUsernameProblemLanguageResultExecution timeMemory
1083351LucasLeMagic Tree (CEOI19_magictree)C++17
9 / 100
81 ms18268 KiB
#include<bits/stdc++.h>
 
using namespace std;
 
#define sz(v) (int)v.size()
#define all(v) begin(v), end(v)
#define compact(v) v.erase(unique(all(v)), end(v))
#define dbg(v) "[" #v " = " << (v) << "]"
#define file(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define rep(i, l, r) for(int i = (l); i < (r); ++i)
 
using ll = long long;
using vi = vector<int>;
using vl = vector<long long>;
using ld = long double;
 
template<typename T> 
    bool minimize(T& a, const T& b){
        if(a > b){
            return a = b, true;
        }  return false;
    }
 
template<typename T>
    bool maximize(T& a, const T& b){    
        if(a < b){
            return a = b, true;
        } return false;
    }
 
template<int dimension, typename T>
struct tensor : public vector<tensor<dimension - 1, T>> {
    static_assert(dimension > 0, "Dimension must be positive !\n");
    template<typename... Args>
    tensor(int n = 0, Args... args) : vector<tensor<dimension - 1, T>> (n, tensor<dimension - 1, T>(args...)) {}
};
 
template<typename T>
struct tensor<1, T> : public vector<T> {
    tensor(int n = 0, T val = T()) : vector<T>(n, val) {}
};
 
const int MAX = 1e5 + 5;
 
int n, m, k, par[MAX], day[MAX], weight[MAX];
vector<int> adj[MAX];
map<int, ll> dp[MAX]; 

// we can prove that dp[u][t] <= dp[u][t + 1]
// notice : now the dp[u] works as the "difference array"
 
void dfs(int u) {
    for (int v : adj[u]) {
        dfs(v);
 
        if (sz(dp[u]) < sz(dp[v])) swap(dp[u], dp[v]);
        for (const auto& [pos, val] : dp[v]) {
            dp[u][pos] += val;
        }
 
        dp[v].clear();
    }
 
    if (day[u] && weight[u]) {
        
        ll sum = weight[u];
        ll lmouse = 0;
        bool f = true;

        while (true) {
            auto iter = dp[u].upper_bound(day[u]);
            if(iter == dp[u].end()) break;
 
            if (iter -> second <= sum) {
                sum -= iter -> second; //because of the max part
                lmouse += iter -> second;
                dp[u].erase(iter); //same dp[u][t]
            } else {
                // iter -> second -= sum; //can't increase
                iter -> second += lmouse;
                f = false;
                break; 
            }
        }
        
        if (f)
            dp[u][day[u]] += weight[u];
    }
}
 
// nh, yh
// u, day[u], weight[u] 
// yh += cnt[v][k] (k <= day[u])
// nh += max(cnt[v][k])
// res[u] = max(yh + weight[u], nh)

void testcase(){
    cin >> n >> m >> k;
    par[0] = -1;
    rep(i, 1, n){
        int p;
        cin >> p;
        --p;
        ::par[i] = p;
        adj[p].push_back(i);
    }
 
    rep(i, 0, m){
        int v, d, w;
        cin >> v >> d >> w;
        --v;
        day[v] = d;
        weight[v] = w;
    }
 
    dfs(0);
 
    ll sum = 0;
    for(auto it : dp[0]) sum += it.second;
    // cout << dp[0][2] << ' ' << dp[0][6] << '\n';
    cout << sum << '\n';
}
 
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    // file("task");
 
    int T = 1;
    //cin >> T;
    while(T--){
        testcase();
    }
 
    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...