This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define ll long long
#define maksim gay
#define int ll
#define pb push_back
#define sz(s) (int)s.size()
#define pii pair<int,int>
#define all(v) v.begin(),v.end()
#define mem(a,i) memset(a,i,sizeof(a))
#define in insert
#define lb lower_bound
#define ub upper_bound
const int MAX=1e5+20;
const int inf=1e10;
const int N=2e5;
int n,m,k;
int d[MAX],w[MAX];
vector<int> g[MAX];
int tin[MAX],tout[MAX],timer;
int ob[MAX];
int s[MAX],big[MAX];
map<int,int> dp[MAX];
void dfs(int v){
tin[v]=++timer;
ob[timer]=v;
big[v]=0;
s[v]=1;
for(auto to:g[v]){
dfs(to);
s[v]+=s[to];
if(s[to]>s[big[v]])big[v]=to;
}
tout[v]=timer;
}
struct DO{
int sum,add,er;
DO(){
sum=add=er=0;
}
}t[4*MAX];
void push(int v,int tl,int tr){
if(t[v].er){
t[2*v].sum=t[2*v+1].sum=t[2*v].add=t[2*v+1].add=0;
t[2*v].er=t[2*v+1].er=1;
}
t[2*v].add+=t[v].add;
t[2*v+1].add+=t[v].add;
int tm=(tr+tl)/2;
t[2*v].sum+=t[v].add*(tm-tl+1);
t[2*v+1].sum+=t[v].add*(tr-tm);
t[v].er=t[v].add=0;
}
void update(int v,int tl,int tr,int l,int r,int x){
if(l>r||tl>r||l>tr)return;
if(l<=tl&&tr<=r){
t[v].sum=(tr-tl+1)*x;
t[v].add=x;
t[v].er=1;
return;
}
push(v,tl,tr);
int tm=(tl+tr)/2;
update(2*v,tl,tm,l,r,x);
update(2*v+1,tm+1,tr,l,r,x);
t[v].sum=t[2*v].sum+t[2*v+1].sum;
}
void update1(int v,int tl,int tr,int l,int r,int x){
if(l>r||tl>r||l>tr)return;
if(l<=tl&&tr<=r){
t[v].sum+=(tr-tl+1)*x;
t[v].add+=x;
return;
}
push(v,tl,tr);
int tm=(tl+tr)/2;
update1(2*v,tl,tm,l,r,x);
update1(2*v+1,tm+1,tr,l,r,x);
t[v].sum=t[2*v].sum+t[2*v+1].sum;
}
int get(int v,int tl,int tr,int l,int r){
if(l>r||tl>r||l>tr)return 0;
if(l<=tl&&tr<=r){
return t[v].sum;
}
push(v,tl,tr);
int tm=(tl+tr)/2;
return get(2*v,tl,tm,l,r)+get(2*v+1,tm+1,tr,l,r);
}
void calc(int v,int keep=1){
for(auto to:g[v]){
if(to==big[v])continue;
calc(to,0);
}
if(big[v])calc(big[v],1);
// cout<<v<<"\n";
// for(int i=1;i<=k;i++){
// cout<<get(1,1,k,i)<<" ";
// }
// cout<<"\n";
for(auto to:g[v]){
if(to==big[v])continue;
vector<pii> vec;
for(pii x:dp[to]){
int u=x.F;
int val=x.S;
vec.pb({u,val});
}
int r=k+1;
for(int i=sz(vec)-1;i>=0;i--){
int u=vec[i].F;
int val=vec[i].S;
update1(1,1,k,u,r-1,val);
r=u;
}
}
if(d[v]){
int u=d[v];
int val=w[v]+get(1,1,k,u,u);
int l=u,r=k,res=u;
while(l<=r){
int m=(l+r)/2;
if(get(1,1,k,m,m)<=val){
l=m+1;
res=m;
}
else r=m-1;
}
update(1,1,k,u,res,val);
}
// cout<<v<<"\n";
// for(int i=1;i<=k;i++){
// cout<<get(1,1,k,i,i)<<" ";
// }
// cout<<"\n";
if(!keep){
for(int i=tin[v];i<=tout[v];i++){
int u=ob[i];
if(d[u]){
dp[v][d[u]]=get(1,1,k,d[u],d[u]);
}
}
update(1,1,k,1,k,0);
}
}
// !! 3
// 0 0 0 0 5 5 5 5 5 5 5
// !! 2
// 0 0 0 0 5 5 5 5 5 5 5
// !! 5
// 0 0 0 0 1 1 1 1 1 1 1
// !! 6
// 0 0 0 0 0 0 0 0 0 3 3
// !! 4
// 0 0 0 0 1 1 1 3 3 4 4
// !! 1
// 0 0 0 0 6 6 6 8 8 9 9
// 9
void solve(){
cin>>n>>m>>k;
for(int i=2;i<=n;i++){
int p;
cin>>p;
g[p].pb(i);
}
for(int i=1;i<=m;i++){
int v;
cin>>v;
cin>>d[v]>>w[v];
}
dfs(1);
calc(1);
cout<<get(1,1,k,k,k)<<"\n";
}
main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
// cin>>t;
while(t--){
solve();
}
}
Compilation message (stderr)
magictree.cpp:193:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
193 | main(){
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |