#include<bits/stdc++.h>
using namespace std;
const long long maxn=100000+10;
vector<long long>adj[maxn];
long long n,m,k;
pair<long long,long long>allq[maxn];
set<pair<long long,long long>>st[maxn];
void insert(long long u){
if(allq[u].second==0){
return ;
}
auto x=allq[u];
pair<long long,long long>y;
if((long long)st[u].size()>0&&(*st[u].rbegin()).first>=x.first){
y=*st[u].lower_bound(make_pair(x.first,-1));
}
else{
y.first=-1;
}
if(y.first==x.first){
x.second+=y.second;
st[u].erase(y);
}
st[u].insert(x);
long long z=x.first;
while((long long)st[u].size()>0&&(*st[u].rbegin()).first>z){
y=*st[u].lower_bound(make_pair(z+1,-1));
if(y.second<allq[u].second){
st[u].erase(y);
allq[u].second-=y.second;
}
else{
st[u].erase(y);
y.second-=allq[u].second;
st[u].insert(y);
allq[u].second=0;
break;
}
}
}
void merge(long long u,long long v){
long long fu=u,fv=v;
if((long long)st[u].size()<(long long)st[v].size()){
swap(u,v);
}
for(auto x:st[v]){
auto y=*st[u].lower_bound(make_pair(x.first,-1));
if((long long)st[u].size()==0||(*st[u].rbegin()).first<x.first||y.first!=x.first){
st[u].insert(x);
}
else{
st[u].erase(y);
y.second+=x.second;
st[u].insert(y);
}
}
if(u!=fu){
swap(st[u],st[fu]);
}
}
/*void co(long long u){
cout<<"u: "<<u<<" \n";
for(auto x:st[u]){
cout<<x.first<<" "<<x.second<<"\n";
}
}*/
void solve(long long u=1){
if((long long)adj[u].size()==0){
insert(u);
//co(u);
return ;
}
solve(adj[u][0]);
swap(st[u],st[adj[u][0]]);
for(long long i=1;i<(long long)adj[u].size();i++){
solve(adj[u][i]);
merge(u,adj[u][i]);
}
insert(u);
//co(u);
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m>>k;
for(long long i=2;i<=n;i++){
long long d;
cin>>d;
adj[d].push_back(i);
}
for(long long i=0;i<m;i++){
long long v,d,w;
cin>>v>>d>>w;
allq[v]=make_pair(d,w);
}
solve();
long long res=0;
for(auto x:st[1]){
res+=x.second;
}
cout<<res<<'\n';
}
Compilation message
magictree.cpp: In function 'void merge(long long int, long long int)':
magictree.cpp:44:17: warning: unused variable 'fv' [-Wunused-variable]
44 | long long fu=u,fv=v;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9052 KB |
Output is correct |
2 |
Correct |
1 ms |
9052 KB |
Output is correct |
3 |
Correct |
1 ms |
9052 KB |
Output is correct |
4 |
Correct |
2 ms |
9052 KB |
Output is correct |
5 |
Correct |
2 ms |
9052 KB |
Output is correct |
6 |
Correct |
2 ms |
9052 KB |
Output is correct |
7 |
Correct |
2 ms |
9052 KB |
Output is correct |
8 |
Correct |
2 ms |
9052 KB |
Output is correct |
9 |
Correct |
2 ms |
9052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
18516 KB |
Output is correct |
2 |
Correct |
29 ms |
16468 KB |
Output is correct |
3 |
Correct |
117 ms |
35932 KB |
Output is correct |
4 |
Correct |
70 ms |
19052 KB |
Output is correct |
5 |
Correct |
92 ms |
19900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9052 KB |
Output is correct |
2 |
Correct |
2 ms |
9052 KB |
Output is correct |
3 |
Correct |
2 ms |
9048 KB |
Output is correct |
4 |
Correct |
49 ms |
16872 KB |
Output is correct |
5 |
Correct |
54 ms |
20784 KB |
Output is correct |
6 |
Correct |
83 ms |
16920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
14504 KB |
Output is correct |
2 |
Correct |
43 ms |
15800 KB |
Output is correct |
3 |
Correct |
40 ms |
16724 KB |
Output is correct |
4 |
Correct |
36 ms |
17352 KB |
Output is correct |
5 |
Correct |
38 ms |
18952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9052 KB |
Output is correct |
2 |
Correct |
1 ms |
9052 KB |
Output is correct |
3 |
Correct |
1 ms |
9052 KB |
Output is correct |
4 |
Correct |
2 ms |
9052 KB |
Output is correct |
5 |
Correct |
2 ms |
9052 KB |
Output is correct |
6 |
Correct |
2 ms |
9052 KB |
Output is correct |
7 |
Correct |
2 ms |
9052 KB |
Output is correct |
8 |
Correct |
2 ms |
9052 KB |
Output is correct |
9 |
Correct |
2 ms |
9052 KB |
Output is correct |
10 |
Correct |
60 ms |
18608 KB |
Output is correct |
11 |
Correct |
53 ms |
17336 KB |
Output is correct |
12 |
Correct |
47 ms |
14420 KB |
Output is correct |
13 |
Correct |
34 ms |
15308 KB |
Output is correct |
14 |
Correct |
47 ms |
16800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9556 KB |
Output is correct |
2 |
Correct |
15 ms |
10956 KB |
Output is correct |
3 |
Correct |
15 ms |
10996 KB |
Output is correct |
4 |
Correct |
16 ms |
11172 KB |
Output is correct |
5 |
Correct |
7 ms |
10200 KB |
Output is correct |
6 |
Correct |
20 ms |
11612 KB |
Output is correct |
7 |
Correct |
27 ms |
13404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9052 KB |
Output is correct |
2 |
Correct |
1 ms |
9052 KB |
Output is correct |
3 |
Correct |
1 ms |
9052 KB |
Output is correct |
4 |
Correct |
2 ms |
9052 KB |
Output is correct |
5 |
Correct |
2 ms |
9052 KB |
Output is correct |
6 |
Correct |
2 ms |
9052 KB |
Output is correct |
7 |
Correct |
2 ms |
9052 KB |
Output is correct |
8 |
Correct |
2 ms |
9052 KB |
Output is correct |
9 |
Correct |
2 ms |
9052 KB |
Output is correct |
10 |
Correct |
2 ms |
9052 KB |
Output is correct |
11 |
Correct |
2 ms |
9052 KB |
Output is correct |
12 |
Correct |
2 ms |
9048 KB |
Output is correct |
13 |
Correct |
49 ms |
16872 KB |
Output is correct |
14 |
Correct |
54 ms |
20784 KB |
Output is correct |
15 |
Correct |
83 ms |
16920 KB |
Output is correct |
16 |
Correct |
60 ms |
18608 KB |
Output is correct |
17 |
Correct |
53 ms |
17336 KB |
Output is correct |
18 |
Correct |
47 ms |
14420 KB |
Output is correct |
19 |
Correct |
34 ms |
15308 KB |
Output is correct |
20 |
Correct |
47 ms |
16800 KB |
Output is correct |
21 |
Correct |
19 ms |
12840 KB |
Output is correct |
22 |
Correct |
62 ms |
20300 KB |
Output is correct |
23 |
Correct |
76 ms |
24404 KB |
Output is correct |
24 |
Correct |
141 ms |
35728 KB |
Output is correct |
25 |
Correct |
78 ms |
19076 KB |
Output is correct |
26 |
Correct |
107 ms |
19900 KB |
Output is correct |
27 |
Correct |
94 ms |
16908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9052 KB |
Output is correct |
2 |
Correct |
1 ms |
9052 KB |
Output is correct |
3 |
Correct |
1 ms |
9052 KB |
Output is correct |
4 |
Correct |
2 ms |
9052 KB |
Output is correct |
5 |
Correct |
2 ms |
9052 KB |
Output is correct |
6 |
Correct |
2 ms |
9052 KB |
Output is correct |
7 |
Correct |
2 ms |
9052 KB |
Output is correct |
8 |
Correct |
2 ms |
9052 KB |
Output is correct |
9 |
Correct |
2 ms |
9052 KB |
Output is correct |
10 |
Correct |
56 ms |
18516 KB |
Output is correct |
11 |
Correct |
29 ms |
16468 KB |
Output is correct |
12 |
Correct |
117 ms |
35932 KB |
Output is correct |
13 |
Correct |
70 ms |
19052 KB |
Output is correct |
14 |
Correct |
92 ms |
19900 KB |
Output is correct |
15 |
Correct |
2 ms |
9052 KB |
Output is correct |
16 |
Correct |
2 ms |
9052 KB |
Output is correct |
17 |
Correct |
2 ms |
9048 KB |
Output is correct |
18 |
Correct |
49 ms |
16872 KB |
Output is correct |
19 |
Correct |
54 ms |
20784 KB |
Output is correct |
20 |
Correct |
83 ms |
16920 KB |
Output is correct |
21 |
Correct |
43 ms |
14504 KB |
Output is correct |
22 |
Correct |
43 ms |
15800 KB |
Output is correct |
23 |
Correct |
40 ms |
16724 KB |
Output is correct |
24 |
Correct |
36 ms |
17352 KB |
Output is correct |
25 |
Correct |
38 ms |
18952 KB |
Output is correct |
26 |
Correct |
60 ms |
18608 KB |
Output is correct |
27 |
Correct |
53 ms |
17336 KB |
Output is correct |
28 |
Correct |
47 ms |
14420 KB |
Output is correct |
29 |
Correct |
34 ms |
15308 KB |
Output is correct |
30 |
Correct |
47 ms |
16800 KB |
Output is correct |
31 |
Correct |
5 ms |
9556 KB |
Output is correct |
32 |
Correct |
15 ms |
10956 KB |
Output is correct |
33 |
Correct |
15 ms |
10996 KB |
Output is correct |
34 |
Correct |
16 ms |
11172 KB |
Output is correct |
35 |
Correct |
7 ms |
10200 KB |
Output is correct |
36 |
Correct |
20 ms |
11612 KB |
Output is correct |
37 |
Correct |
27 ms |
13404 KB |
Output is correct |
38 |
Correct |
19 ms |
12840 KB |
Output is correct |
39 |
Correct |
62 ms |
20300 KB |
Output is correct |
40 |
Correct |
76 ms |
24404 KB |
Output is correct |
41 |
Correct |
141 ms |
35728 KB |
Output is correct |
42 |
Correct |
78 ms |
19076 KB |
Output is correct |
43 |
Correct |
107 ms |
19900 KB |
Output is correct |
44 |
Correct |
94 ms |
16908 KB |
Output is correct |
45 |
Correct |
19 ms |
12636 KB |
Output is correct |
46 |
Correct |
62 ms |
20816 KB |
Output is correct |
47 |
Correct |
69 ms |
24240 KB |
Output is correct |
48 |
Correct |
123 ms |
34880 KB |
Output is correct |
49 |
Correct |
69 ms |
21196 KB |
Output is correct |
50 |
Correct |
119 ms |
21584 KB |
Output is correct |
51 |
Correct |
88 ms |
18824 KB |
Output is correct |