#include <bits/stdc++.h>
#ifndef TEST
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
#endif
using namespace std;
typedef long long ll;
struct Fruit{
int v, d; ll w;
Fruit(){}
Fruit(int v, int d, ll w): v(v), d(d), w(w){}
};
int n, m, k;
int par[100002];
vector<int> child[100002];
int state[100002];
Fruit fruit[100002];
int day[100002];
ll weight[100002];
int root[100002];
map<int, ll> mp[100002];
void renumber(){
vector<int> dVec;
for(int i=1; i<=m; i++) dVec.push_back(fruit[i].d);
sort(dVec.begin(), dVec.end());
dVec.erase(unique(dVec.begin(), dVec.end()), dVec.end());
for(int i=1; i<=m; i++) fruit[i].d = lower_bound(dVec.begin(), dVec.end(), fruit[i].d) - dVec.begin() + 1;
k = (int)dVec.size();
}
void update(int i, int x, ll v){ /// v만큼 더한다
mp[i][x] += v;
}
void nextRemove(int i, int x, ll v){
auto it = mp[i].upper_bound(x);
while(v && it != mp[i].end()){
pair<int, ll> tp = *it;
ll tmp = min(tp.second, v);
mp[i][tp.first] -= tmp;
v -= tmp;
if(!mp[i][tp.first]) mp[i].erase(tp.first);
it = mp[i].upper_bound(x);
}
}
void mergeSet(int l, int r){
if(mp[l].size() < mp[r].size()) mp[l].swap(mp[r]);
for(auto x: mp[r]){
mp[l][x.first] += x.second;
}
mp[r].clear();
}
void dfs(int x){
for(auto y: child[x]){
dfs(y);
mergeSet(x, y);
}
if(day[x]){
update(x, day[x], weight[x]);
nextRemove(x, day[x], weight[x]);
}
}
int main(){
scanf("%d %d %d", &n, &m, &k);
for(int i=2; i<=n; i++){
scanf("%d", &par[i]);
child[par[i]].push_back(i);
}
for(int i=1; i<=m; i++){
int v, d, w;
scanf("%d %d %d", &v, &d, &w);
fruit[i] = Fruit(v, d, w);
}
renumber();
for(int i=1; i<=m; i++){
day[fruit[i].v] = fruit[i].d;
weight[fruit[i].v] = fruit[i].w;
}
dfs(1);
ll ans = 0;
for(auto x: mp[1]) ans += x.second;
printf("%lld", ans);
}
Compilation message
magictree.cpp: In function 'int main()':
magictree.cpp:76:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | scanf("%d %d %d", &n, &m, &k);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
magictree.cpp:78:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | scanf("%d", &par[i]);
| ~~~~~^~~~~~~~~~~~~~~
magictree.cpp:84:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
84 | scanf("%d %d %d", &v, &d, &w);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
5 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
6 ms |
7328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
12624 KB |
Output is correct |
2 |
Correct |
46 ms |
17276 KB |
Output is correct |
3 |
Correct |
155 ms |
18224 KB |
Output is correct |
4 |
Correct |
97 ms |
16480 KB |
Output is correct |
5 |
Correct |
113 ms |
18008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7508 KB |
Output is correct |
2 |
Correct |
5 ms |
7508 KB |
Output is correct |
3 |
Correct |
4 ms |
7508 KB |
Output is correct |
4 |
Correct |
74 ms |
25060 KB |
Output is correct |
5 |
Correct |
96 ms |
28804 KB |
Output is correct |
6 |
Correct |
115 ms |
25044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
12068 KB |
Output is correct |
2 |
Correct |
73 ms |
14188 KB |
Output is correct |
3 |
Correct |
61 ms |
18740 KB |
Output is correct |
4 |
Correct |
58 ms |
12496 KB |
Output is correct |
5 |
Correct |
61 ms |
25044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
5 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
6 ms |
7328 KB |
Output is correct |
10 |
Correct |
94 ms |
13480 KB |
Output is correct |
11 |
Correct |
77 ms |
13484 KB |
Output is correct |
12 |
Correct |
66 ms |
18172 KB |
Output is correct |
13 |
Correct |
60 ms |
11932 KB |
Output is correct |
14 |
Correct |
68 ms |
24428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
8020 KB |
Output is correct |
2 |
Correct |
35 ms |
11068 KB |
Output is correct |
3 |
Correct |
32 ms |
11032 KB |
Output is correct |
4 |
Correct |
34 ms |
11128 KB |
Output is correct |
5 |
Correct |
15 ms |
9676 KB |
Output is correct |
6 |
Correct |
30 ms |
12796 KB |
Output is correct |
7 |
Correct |
31 ms |
15752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
5 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
6 ms |
7328 KB |
Output is correct |
10 |
Correct |
5 ms |
7508 KB |
Output is correct |
11 |
Correct |
5 ms |
7508 KB |
Output is correct |
12 |
Correct |
4 ms |
7508 KB |
Output is correct |
13 |
Correct |
74 ms |
25060 KB |
Output is correct |
14 |
Correct |
96 ms |
28804 KB |
Output is correct |
15 |
Correct |
115 ms |
25044 KB |
Output is correct |
16 |
Correct |
94 ms |
13480 KB |
Output is correct |
17 |
Correct |
77 ms |
13484 KB |
Output is correct |
18 |
Correct |
66 ms |
18172 KB |
Output is correct |
19 |
Correct |
60 ms |
11932 KB |
Output is correct |
20 |
Correct |
68 ms |
24428 KB |
Output is correct |
21 |
Correct |
30 ms |
8836 KB |
Output is correct |
22 |
Correct |
92 ms |
12764 KB |
Output is correct |
23 |
Correct |
91 ms |
14608 KB |
Output is correct |
24 |
Correct |
124 ms |
16996 KB |
Output is correct |
25 |
Correct |
87 ms |
15824 KB |
Output is correct |
26 |
Correct |
127 ms |
19296 KB |
Output is correct |
27 |
Correct |
129 ms |
19852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
5 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
6 ms |
7328 KB |
Output is correct |
10 |
Correct |
70 ms |
12624 KB |
Output is correct |
11 |
Correct |
46 ms |
17276 KB |
Output is correct |
12 |
Correct |
155 ms |
18224 KB |
Output is correct |
13 |
Correct |
97 ms |
16480 KB |
Output is correct |
14 |
Correct |
113 ms |
18008 KB |
Output is correct |
15 |
Correct |
5 ms |
7508 KB |
Output is correct |
16 |
Correct |
5 ms |
7508 KB |
Output is correct |
17 |
Correct |
4 ms |
7508 KB |
Output is correct |
18 |
Correct |
74 ms |
25060 KB |
Output is correct |
19 |
Correct |
96 ms |
28804 KB |
Output is correct |
20 |
Correct |
115 ms |
25044 KB |
Output is correct |
21 |
Correct |
72 ms |
12068 KB |
Output is correct |
22 |
Correct |
73 ms |
14188 KB |
Output is correct |
23 |
Correct |
61 ms |
18740 KB |
Output is correct |
24 |
Correct |
58 ms |
12496 KB |
Output is correct |
25 |
Correct |
61 ms |
25044 KB |
Output is correct |
26 |
Correct |
94 ms |
13480 KB |
Output is correct |
27 |
Correct |
77 ms |
13484 KB |
Output is correct |
28 |
Correct |
66 ms |
18172 KB |
Output is correct |
29 |
Correct |
60 ms |
11932 KB |
Output is correct |
30 |
Correct |
68 ms |
24428 KB |
Output is correct |
31 |
Correct |
9 ms |
8020 KB |
Output is correct |
32 |
Correct |
35 ms |
11068 KB |
Output is correct |
33 |
Correct |
32 ms |
11032 KB |
Output is correct |
34 |
Correct |
34 ms |
11128 KB |
Output is correct |
35 |
Correct |
15 ms |
9676 KB |
Output is correct |
36 |
Correct |
30 ms |
12796 KB |
Output is correct |
37 |
Correct |
31 ms |
15752 KB |
Output is correct |
38 |
Correct |
30 ms |
8836 KB |
Output is correct |
39 |
Correct |
92 ms |
12764 KB |
Output is correct |
40 |
Correct |
91 ms |
14608 KB |
Output is correct |
41 |
Correct |
124 ms |
16996 KB |
Output is correct |
42 |
Correct |
87 ms |
15824 KB |
Output is correct |
43 |
Correct |
127 ms |
19296 KB |
Output is correct |
44 |
Correct |
129 ms |
19852 KB |
Output is correct |
45 |
Correct |
24 ms |
8968 KB |
Output is correct |
46 |
Correct |
80 ms |
13028 KB |
Output is correct |
47 |
Correct |
91 ms |
14900 KB |
Output is correct |
48 |
Correct |
161 ms |
18000 KB |
Output is correct |
49 |
Correct |
104 ms |
16468 KB |
Output is correct |
50 |
Correct |
134 ms |
20152 KB |
Output is correct |
51 |
Correct |
133 ms |
20908 KB |
Output is correct |