Submission #1082270

#TimeUsernameProblemLanguageResultExecution timeMemory
1082270Zero_OPMagic Tree (CEOI19_magictree)C++14
100 / 100
88 ms30036 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];
        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
                dp[u].erase(iter); //same dp[u][t]
            } else{
                iter -> second -= sum; //can't increase
                break; 
            }
        }
        
        dp[u][day[u]] += weight[u];
     }
}

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 << 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;
}

Compilation message (stderr)

magictree.cpp: In function 'void dfs(int)':
magictree.cpp:56:25: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   56 |         for(const auto& [pos, val] : dp[v]){
      |                         ^
magictree.cpp: In function 'int main()':
magictree.cpp:9:55: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 | #define file(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
magictree.cpp:112:5: note: in expansion of macro 'file'
  112 |     file("task");
      |     ^~~~
magictree.cpp:9:88: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 | #define file(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
magictree.cpp:112:5: note: in expansion of macro 'file'
  112 |     file("task");
      |     ^~~~
#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...