#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ent "\n"
const int maxn = 1e5 + 100;
const ll INF = (ll)1e18 + 100;
const int MOD = 998244353;
int n, m, k;
vector<int> g[maxn];
vector<pii> q[maxn];
int f[maxn];
ll d[maxn * 4];
void upd(int i, ll x, int v = 1, int tl = 0, int tr = k){
if(tl == tr) d[v] = x;
else{
int mid = (tl + tr) >> 1;
if(i <= mid) upd(i, x, v<<1, tl, mid);
else upd(i, x, v<<1|1, mid+1, tr);
d[v] = max(d[v<<1], d[v<<1|1]);
}
}
ll get(int l, int r, int v = 1, int tl = 0, int tr = k){
if(tr < l || tl > r) return -INF;
if(l <= tl && tr <= r) return d[v];
int mid = (tl + tr) >> 1;
return max(get(l, r, v<<1, tl, mid)
, get(l, r, v<<1|1, mid+1, tr));
}
void calc(int v, int i){
if(f[v]) return;
f[v] = i;
for(int to: g[v]){
calc(to, i);
}
}
void test(){
cin >> n >> m >> k;
for(int i = 2; i <= n; i++){
int p; cin >> p;
g[p].push_back(i);
}
for(int i = 1; i <= m; i++){
int v, d, w;
cin >> v >> d >> w;
q[d].push_back({v, w});
}
for(int i = 1; i <= k; i++){
vector<pii> s = {{0, 0}};
for(auto [v, d]: q[i]){
calc(v, i);
s.push_back({f[v], d});
}
ll mx = 0, sum = 0;
s.push_back({i, 0});
sort(s.begin(), s.end());
for(int i = s.size() - 1; i > 0; i--){
sum += s[i].second;
if(s[i-1].first < s[i].first){
mx = max(mx, get(s[i-1].first, s[i].first - 1) + sum);
}
}
upd(i, mx);
}
cout << get(k, k);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
int t; t = 1;
while(t--) test();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
6236 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
10408 KB |
Output is correct |
2 |
Correct |
39 ms |
11308 KB |
Output is correct |
3 |
Correct |
74 ms |
11148 KB |
Output is correct |
4 |
Correct |
58 ms |
10704 KB |
Output is correct |
5 |
Correct |
59 ms |
11084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Incorrect |
2 ms |
6492 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
37 ms |
9636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
6236 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
17 ms |
7772 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
6236 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
6236 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |