#include<bits/stdc++.h>
using namespace std;
#define int long long
#define tii tuple<int,int,int,int>
#define pii pair<int,int>
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
const int maxn = 1e5 + 5;
vector<int> g[maxn];
int tin[maxn], tout[maxn], pai[maxn], ans_arr[maxn], depth[maxn];
vector<tii> fruits;
int n;
int timer=1;
void elr_tr(int u, int p){
tin[u] = timer++;
depth[u] = depth[p]+1;
for(int v : g[u]) if(v!=p){
elr_tr(v,u);
}
tout[u] = timer;
}
struct SEG{
int tree[4*maxn];
void fupd(int node, int tl, int tr, int id, int val){
if(tl>id || tr<id) return;
if(tl==tr){tree[node] = val; return;}
int mid = (tl+tr)>>1;
int f1 = node*2, f2 = node*2+1;
fupd(f1, tl, mid, id, val), fupd(f2, mid+1, tr, id, val);
tree[node] = max(tree[f1], tree[f2]);
}
void upd(int id, int val){fupd(1, 1, n, id, val);}
int fquery(int node, int tl, int tr, int l, int r){
if(tl>r || tr<l) return 0;
if(tl>=l && tr<=r) return tree[node];
int mid = (tl+tr)>>1;
int f1 = node*2, f2 = node*2+1;
return max(fquery(f1, tl, mid, l, r), fquery(f2, mid+1, tr, l, r));
}
int query(int l, int r){return fquery(1, 1, n, l, r);}
}seg;
int solve(int u, int p){
int aux = 0;
for(int v : g[u]) if(v!=p){
aux += solve(v,u);
}
return max(ans_arr[u], aux);
}
int32_t main(){
int m, k; cin >> n >> m >> k;
for(int i = 2; i <= n; i++){
int p; cin >> p;
pai[i] = p;
g[i].push_back(p);
g[p].push_back(i);
}
elr_tr(1,1);
for(int i = 0; i < m; i++){
int v, d, w; cin >> v >> d >> w;
fruits.push_back({d,-depth[v],v,w});
}
sort(all(fruits));
for(auto[d,dep,u,w] : fruits){
ans_arr[u] = w;
for(int v : g[u]) if(v!=pai[u]) ans_arr[u] += seg.query(tin[v], tout[v]);
seg.upd(tin[u], ans_arr[u]);
}
cout << solve(1,1) << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
80 ms |
14408 KB |
Output is correct |
2 |
Correct |
51 ms |
13380 KB |
Output is correct |
3 |
Correct |
130 ms |
18148 KB |
Output is correct |
4 |
Correct |
127 ms |
17576 KB |
Output is correct |
5 |
Correct |
122 ms |
18380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
2 ms |
2772 KB |
Output is correct |
3 |
Correct |
3 ms |
2772 KB |
Output is correct |
4 |
Correct |
105 ms |
21596 KB |
Output is correct |
5 |
Correct |
102 ms |
21584 KB |
Output is correct |
6 |
Correct |
149 ms |
21572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
147 ms |
17464 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
4820 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |