#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define int long long
#define ll long long
#define pb push_back
#define fi first
#define se second
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)>>1)
#define ld long double
using namespace std;
typedef pair<int,int> pii;
typedef pair<int,pii> ipii;
const int MAXN = 1e5+10;
const int MAXA = 1e9;
const int INF = 2e9+100;
const int SQRT = 500;
const int LOG = 19;
const int MOD = 998244353;
void chmn(auto &a, auto b){ a = min(a, b); }
void chmx(auto &a, auto b){ a = max(a, b); }
int sum(int a, int b){ a %= MOD; b %= MOD; return (a+b)%MOD; }
void chsum(int &a, int b){ a %= MOD; b %= MOD; a = (a+b)%MOD; }
void chsub(int &a, int b){ a %= MOD; b %= MOD; a = (a+MOD-b)%MOD; }
int mul(int a, int b){ a %= MOD; b %= MOD; return a*b%MOD;}
void chmul(int &a, int b){ a = a*b%MOD; }
int expo(int a, int b){
if(b==0) return 1;
int te = expo(a, b/2); te = mul(te, te);
return (b%2 ? mul(te, a) : te);
}
int n, m, k, par[MAXN], d[MAXN], w[MAXN];
vector<int> adj[MAXN]; // from root decreasing days
int opt[MAXN];
void UPD(int nw, int val){
if(nw==0) return;
chmx(opt[nw], val);
UPD(par[nw], val);
}
int ANS = 0;
void sol(int nw){
int tot = 0;
for(auto nx : adj[nw]){
sol(nx); tot += opt[nx];
}
chmx(opt[nw], tot);
chmx(ANS, opt[nw]);
}
struct seg {
int st[4*MAXN], laz[4*MAXN];
void bd(int id, int l, int r){
laz[id] = -1;
if(l==r) return;
bd(lf,l,md); bd(rg,md+1,r);
}
void bnc(int id, int l, int r){
if(laz[id]==-1) return;
chmx(st[lf], laz[id]); chmx(laz[lf], laz[id]);
chmx(st[rg], laz[id]); chmx(laz[rg], laz[id]);
laz[id] = -1;
}
int que(int id, int l, int r, int x, int y){
if(x<=l && r<=y) return st[id];
if(r<x || y<l) return -1;
bnc(id,l,r);
return max(que(lf,l,md,x,y), que(rg,md+1,r,x,y));
}
int upd(int id, int l, int r, int x, int y,int p){
if(x<=l && r<=y){
chmx(laz[id], p);
chmx(st[id], p);
return st[id];
}
if(r<x || y<l) return st[id];
bnc(id,l,r);
return st[id] = max(upd(lf,l,md,x,y,p), upd(rg,md+1,r,x,y,p));
}
} A;
int sub[MAXN], in[MAXN], out[MAXN], tim, root[MAXN];
void bd(int nw){
sub[nw] = 1;
for(auto nx : adj[nw]){
bd(nx);
sub[nw] += sub[nx];
}
}
void dfs(int nw, int roo){
root[nw] = roo;
in[nw] = ++tim;
if(adj[nw].size()){
dfs(adj[nw][0], roo);
for(int j=1; j<adj[nw].size(); j++)
dfs(adj[nw][j], adj[nw][j]);
}
out[nw] = tim;
}
void UP(int nw, int val){
if(nw==0) return;
A.upd(1,1,n,in[root[nw]],in[nw],val);
UP(par[root[nw]], val);
}
signed main(){
// ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>m>>k;
for(int i=2; i<=n; i++){
cin>>par[i]; adj[par[i]].pb(i);
}
vector<pii> vec;
for(int i=1; i<=m; i++){
int id, day, wei; cin>>id>>day>>wei;
vec.pb({day, id});
d[id] = day; w[id] = wei;
}
sort(vec.begin(), vec.end()); // inc day
bd(1);
for(int i=1; i<=n; i++){
for(int j=1; j<adj[i].size(); j++)
if(sub[adj[i][j]] > sub[adj[i][0]])
swap(adj[i][j], adj[i][0]);
}
dfs(1, 1);
A.bd(1,1,n);
for(int i=0; i<vec.size(); ){
int j = vec[i].fi;
vector<int>upd;
while(i<vec.size() && vec[i].fi==j){
upd.pb(vec[i].se);
i++;
}
sort(upd.rbegin(), upd.rend()); // bawah ke atas
for(auto id : upd){
int val = 0;
for(auto nx : adj[id])
val += A.que(1,1,n,in[nx],out[nx]);
val += w[id];
// cout << val << ' ' << id <<" val\n";
UP(id, val);
}
}
for(int i=1; i<=n; i++)
opt[i] = A.que(1,1,n,in[i],in[i]);
// for(int i=1; i<=n; i++) cout << i << ' ' <<opt[i] << " opt\n";
sol(1);
cout << ANS << '\n';
}
# | 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... |