#define wiwihorz
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma loop-opt(on)
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define rrep(i, a, b) for(int i = b; i >= a; i--)
#define all(x) x.begin(), x.end()
#define ceil(a, b) ((a + b - 1) / (b))
#define INF 1000000000000000000
#define MOD 998244353
#define eps (1e-9)
using namespace std;
#define int long long int
#define lld long double
#define pii pair<int, int>
#define random mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count())
#ifdef wiwihorz
#define print(a...) kout("[" + string(#a) + "] = ", a)
void vprint(auto L, auto R) {while(L < R) cerr << *L << " \n"[next(L) == R], ++L;}
void kout() { cerr << endl; }
template<class T1, class ... T2> void kout(T1 a, T2 ... e) { cerr << a << " ", kout(e...); }
#else
#define print(...) 0
#define vprint(...) 0
#endif
namespace solver {
int n;
vector<vector<int>> mp;
vector<map<int, int>> s;
vector<int> d, w;
void init_(int _n) {
n = _n;
mp.assign(n + 1, vector<int>());
s.assign(n + 1, map<int, int>());
d.assign(n + 1, 0);
w.assign(n + 1, 0);
}
void dfs(int x) {
int id = 0;
for(auto i : mp[x]) {
dfs(i);
if(s[i].size() > s[id].size()) id = i;
}
s[x].swap(s[id]);
for(auto i : mp[x]) if(i != id) {
for(auto j = s[i].begin(); j != s[i].end(); j = next(j)) {
auto k = next(j);
s[x][j->first] += j ->second;
}
s[i].clear();
}
if(w[x]) {
auto it = s[x].upper_bound(d[x]);
int cur = 0;
while(it != s[x].end()) {
cur += it -> second;
if(w[x] >= cur) {
auto it1 = it;
it = next(it);
s[x].erase(it1);
}
else {
s[x][it->first] = cur - w[x];
break;
}
}
s[x][d[x]] += w[x];
}
// print(x);
// for(auto i : s[x]) print(i.first, i.second);
return;
}
int solve() {
dfs(1);
int ans = 0;
for(auto i : s[1]) {
ans += i.second;
}
return ans;
}
};
using namespace solver;
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
int n, m, k;
cin >> n >> m >> k;
init_(n);
rep(i, 2, n) {
int x; cin >> x;
mp[x].push_back(i);
}
rep(i, 1, m) {
int a, b, c;
cin >> a >> b >> c;
w[a] = c, d[a] = b;
}
cout << solve() << "\n";
return 0;
}
Compilation message
magictree.cpp:4: warning: ignoring '#pragma loop ' [-Wunknown-pragmas]
4 | #pragma loop-opt(on)
|
magictree.cpp:24:13: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
24 | void vprint(auto L, auto R) {while(L < R) cerr << *L << " \n"[next(L) == R], ++L;}
| ^~~~
magictree.cpp:24:21: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
24 | void vprint(auto L, auto R) {while(L < R) cerr << *L << " \n"[next(L) == R], ++L;}
| ^~~~
magictree.cpp: In function 'void solver::dfs(long long int)':
magictree.cpp:52:10: warning: variable 'k' set but not used [-Wunused-but-set-variable]
52 | auto k = next(j);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
13764 KB |
Output is correct |
2 |
Correct |
55 ms |
19780 KB |
Output is correct |
3 |
Correct |
144 ms |
18476 KB |
Output is correct |
4 |
Correct |
83 ms |
17436 KB |
Output is correct |
5 |
Correct |
121 ms |
17608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
568 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
588 KB |
Output is correct |
4 |
Correct |
63 ms |
28048 KB |
Output is correct |
5 |
Correct |
87 ms |
32072 KB |
Output is correct |
6 |
Correct |
84 ms |
28084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
12876 KB |
Output is correct |
2 |
Correct |
87 ms |
12828 KB |
Output is correct |
3 |
Correct |
71 ms |
19252 KB |
Output is correct |
4 |
Correct |
53 ms |
17096 KB |
Output is correct |
5 |
Correct |
60 ms |
28228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
90 ms |
12200 KB |
Output is correct |
11 |
Correct |
85 ms |
12232 KB |
Output is correct |
12 |
Correct |
69 ms |
18612 KB |
Output is correct |
13 |
Correct |
50 ms |
16448 KB |
Output is correct |
14 |
Correct |
59 ms |
27628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
2508 KB |
Output is correct |
2 |
Correct |
42 ms |
11340 KB |
Output is correct |
3 |
Correct |
41 ms |
11396 KB |
Output is correct |
4 |
Correct |
47 ms |
11328 KB |
Output is correct |
5 |
Correct |
17 ms |
10316 KB |
Output is correct |
6 |
Correct |
43 ms |
14160 KB |
Output is correct |
7 |
Correct |
40 ms |
17608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
568 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
588 KB |
Output is correct |
13 |
Correct |
63 ms |
28048 KB |
Output is correct |
14 |
Correct |
87 ms |
32072 KB |
Output is correct |
15 |
Correct |
84 ms |
28084 KB |
Output is correct |
16 |
Correct |
90 ms |
12200 KB |
Output is correct |
17 |
Correct |
85 ms |
12232 KB |
Output is correct |
18 |
Correct |
69 ms |
18612 KB |
Output is correct |
19 |
Correct |
50 ms |
16448 KB |
Output is correct |
20 |
Correct |
59 ms |
27628 KB |
Output is correct |
21 |
Correct |
19 ms |
3048 KB |
Output is correct |
22 |
Correct |
94 ms |
12480 KB |
Output is correct |
23 |
Correct |
97 ms |
14180 KB |
Output is correct |
24 |
Correct |
134 ms |
16412 KB |
Output is correct |
25 |
Correct |
90 ms |
16880 KB |
Output is correct |
26 |
Correct |
114 ms |
19140 KB |
Output is correct |
27 |
Correct |
99 ms |
20420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
76 ms |
13764 KB |
Output is correct |
11 |
Correct |
55 ms |
19780 KB |
Output is correct |
12 |
Correct |
144 ms |
18476 KB |
Output is correct |
13 |
Correct |
83 ms |
17436 KB |
Output is correct |
14 |
Correct |
121 ms |
17608 KB |
Output is correct |
15 |
Correct |
1 ms |
568 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
63 ms |
28048 KB |
Output is correct |
19 |
Correct |
87 ms |
32072 KB |
Output is correct |
20 |
Correct |
84 ms |
28084 KB |
Output is correct |
21 |
Correct |
88 ms |
12876 KB |
Output is correct |
22 |
Correct |
87 ms |
12828 KB |
Output is correct |
23 |
Correct |
71 ms |
19252 KB |
Output is correct |
24 |
Correct |
53 ms |
17096 KB |
Output is correct |
25 |
Correct |
60 ms |
28228 KB |
Output is correct |
26 |
Correct |
90 ms |
12200 KB |
Output is correct |
27 |
Correct |
85 ms |
12232 KB |
Output is correct |
28 |
Correct |
69 ms |
18612 KB |
Output is correct |
29 |
Correct |
50 ms |
16448 KB |
Output is correct |
30 |
Correct |
59 ms |
27628 KB |
Output is correct |
31 |
Correct |
7 ms |
2508 KB |
Output is correct |
32 |
Correct |
42 ms |
11340 KB |
Output is correct |
33 |
Correct |
41 ms |
11396 KB |
Output is correct |
34 |
Correct |
47 ms |
11328 KB |
Output is correct |
35 |
Correct |
17 ms |
10316 KB |
Output is correct |
36 |
Correct |
43 ms |
14160 KB |
Output is correct |
37 |
Correct |
40 ms |
17608 KB |
Output is correct |
38 |
Correct |
19 ms |
3048 KB |
Output is correct |
39 |
Correct |
94 ms |
12480 KB |
Output is correct |
40 |
Correct |
97 ms |
14180 KB |
Output is correct |
41 |
Correct |
134 ms |
16412 KB |
Output is correct |
42 |
Correct |
90 ms |
16880 KB |
Output is correct |
43 |
Correct |
114 ms |
19140 KB |
Output is correct |
44 |
Correct |
99 ms |
20420 KB |
Output is correct |
45 |
Correct |
23 ms |
3276 KB |
Output is correct |
46 |
Correct |
92 ms |
12700 KB |
Output is correct |
47 |
Correct |
101 ms |
14660 KB |
Output is correct |
48 |
Correct |
149 ms |
17436 KB |
Output is correct |
49 |
Correct |
85 ms |
17380 KB |
Output is correct |
50 |
Correct |
133 ms |
20164 KB |
Output is correct |
51 |
Correct |
129 ms |
21260 KB |
Output is correct |