Submission #725609

#TimeUsernameProblemLanguageResultExecution timeMemory
725609Rafi22Magic Tree (CEOI19_magictree)C++14
100 / 100
153 ms37076 KiB
#include <bits/stdc++.h> using namespace std; #define endl '\n' #define st first #define nd second #define pb push_back #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() #define ll long long ll mod=1000000007; int inf=1000000007; ll infl=1000000000000000007; const int N=100007; vector<int>G[N]; ll w[N]; int a[N]; int s[N]; int nx[N]; set<pair<int,ll>>S[N]; void dfs(int v) { s[v]=1; nx[v]=v; int mx=0; for(auto u:G[v]) { dfs(u); s[v]+=s[u]; if(s[u]>mx) { mx=s[u]; nx[v]=nx[u]; } } S[nx[v]].insert({inf,infl}); for(auto u:G[v]) { if(nx[u]==nx[v]) continue; for(auto x:S[nx[u]]) { pair<int,ll>p=*S[nx[v]].lower_bound({x.st,0}); if(p.st==x.st) { S[nx[v]].erase(p); S[nx[v]].insert({p.st,p.nd+x.nd}); } else S[nx[v]].insert(x); } } if(a[v]>0) { ll r=w[v]; while(r>0) { pair<int,ll>p=*S[nx[v]].lower_bound({a[v],infl}); if(p.st==inf) break; S[nx[v]].erase(p); if(r>=p.nd) r-=p.nd; else { S[nx[v]].insert({p.st,p.nd-r}); break; } } pair<int,ll>p=*S[nx[v]].lower_bound({a[v],0}); if(p.st==a[v]) { S[nx[v]].erase(p); S[nx[v]].insert({p.st,p.nd+w[v]}); } else S[nx[v]].insert({a[v],w[v]}); } S[nx[v]].erase({inf,infl}); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,m,k,p; cin>>n>>m>>k; for(int i=2;i<=n;i++) { cin>>p; G[p].pb(i); } while(m--) { int i; cin>>i>>a[i]>>w[i]; } dfs(1); ll ans=0; for(auto x:S[nx[1]]) ans+=x.nd; 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...