/**
____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N_MAX = 100000;
const int K_MAX = 100000;
int N, M, K;
int parent[N_MAX + 2];
vector <int> sons[N_MAX + 2];
int ripeTime[N_MAX + 2];
ll juice[N_MAX + 2];
struct Fenwick {
map <int, ll> mp;
map <int, ll> upds;
void update (int pos, ll val) {
upds[pos] += val;
for (int i = pos; i <= K; i += i & -i) {
mp[i] += val;
}
}
ll query (int pos) {
ll val = 0;
for (int i = pos; i >= 1; i -= i & -i) {
if (mp.find(i) != mp.end()) {
val += mp[i];
}
}
return val;
}
void maxify (int pos, ll val) {
update(pos, val);
while (val > 0) {
map <int, ll> :: iterator it = upds.lower_bound(pos + 1);
if (it == upds.end()) {
break;
}
int itpos = it->first;
ll itval = it->second;
if (itval <= val) {
update(itpos, -itval);
val -= itval;
upds.erase(itpos);
} else {
update(itpos, -val);
val = 0;
}
}
}
};
Fenwick Fen[K_MAX + 2];
void dfs (int u) {
int heavy = -1;
for (int v : sons[u]) {
dfs(v);
if (heavy == -1 || (int) Fen[heavy].upds.size() < (int) Fen[v].upds.size()) {
heavy = v;
}
}
if (heavy != -1) {
swap(Fen[u], Fen[heavy]);
}
for (int v : sons[u]) {
if (v != heavy) {
for (pair <int, ll> upd : Fen[v].upds) {
Fen[u].update(upd.first, upd.second);
}
Fen[v].upds.clear();
Fen[v].mp.clear();
}
}
if (juice[u] != 0) {
Fen[u].maxify(ripeTime[u], juice[u]);
}
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N >> M >> K;
for (int u = 2; u <= N; u++) {
cin >> parent[u];
sons[parent[u]].push_back(u);
}
for (int i = 1; i <= M; i++) {
int u;
cin >> u;
cin >> ripeTime[u] >> juice[u];
}
dfs(1);
cout << Fen[1].query(K) << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
12084 KB |
Output is correct |
3 |
Correct |
6 ms |
12020 KB |
Output is correct |
4 |
Correct |
6 ms |
12076 KB |
Output is correct |
5 |
Correct |
6 ms |
12072 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
7 ms |
12076 KB |
Output is correct |
8 |
Correct |
8 ms |
11988 KB |
Output is correct |
9 |
Correct |
6 ms |
11988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
184 ms |
21580 KB |
Output is correct |
2 |
Correct |
88 ms |
31916 KB |
Output is correct |
3 |
Correct |
469 ms |
30240 KB |
Output is correct |
4 |
Correct |
290 ms |
69192 KB |
Output is correct |
5 |
Correct |
294 ms |
25704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12372 KB |
Output is correct |
2 |
Correct |
7 ms |
12372 KB |
Output is correct |
3 |
Correct |
6 ms |
12372 KB |
Output is correct |
4 |
Correct |
135 ms |
49932 KB |
Output is correct |
5 |
Correct |
112 ms |
53884 KB |
Output is correct |
6 |
Correct |
286 ms |
49940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
15316 KB |
Output is correct |
2 |
Correct |
72 ms |
17452 KB |
Output is correct |
3 |
Correct |
60 ms |
29532 KB |
Output is correct |
4 |
Correct |
53 ms |
29756 KB |
Output is correct |
5 |
Correct |
61 ms |
46960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
12084 KB |
Output is correct |
3 |
Correct |
6 ms |
12020 KB |
Output is correct |
4 |
Correct |
6 ms |
12076 KB |
Output is correct |
5 |
Correct |
6 ms |
12072 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
7 ms |
12076 KB |
Output is correct |
8 |
Correct |
8 ms |
11988 KB |
Output is correct |
9 |
Correct |
6 ms |
11988 KB |
Output is correct |
10 |
Correct |
98 ms |
15452 KB |
Output is correct |
11 |
Correct |
80 ms |
15280 KB |
Output is correct |
12 |
Correct |
64 ms |
27504 KB |
Output is correct |
13 |
Correct |
59 ms |
35528 KB |
Output is correct |
14 |
Correct |
59 ms |
44980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
13040 KB |
Output is correct |
2 |
Correct |
45 ms |
15648 KB |
Output is correct |
3 |
Correct |
45 ms |
15564 KB |
Output is correct |
4 |
Correct |
46 ms |
15720 KB |
Output is correct |
5 |
Correct |
17 ms |
14716 KB |
Output is correct |
6 |
Correct |
37 ms |
21140 KB |
Output is correct |
7 |
Correct |
44 ms |
27852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
12084 KB |
Output is correct |
3 |
Correct |
6 ms |
12020 KB |
Output is correct |
4 |
Correct |
6 ms |
12076 KB |
Output is correct |
5 |
Correct |
6 ms |
12072 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
7 ms |
12076 KB |
Output is correct |
8 |
Correct |
8 ms |
11988 KB |
Output is correct |
9 |
Correct |
6 ms |
11988 KB |
Output is correct |
10 |
Correct |
7 ms |
12372 KB |
Output is correct |
11 |
Correct |
7 ms |
12372 KB |
Output is correct |
12 |
Correct |
6 ms |
12372 KB |
Output is correct |
13 |
Correct |
135 ms |
49932 KB |
Output is correct |
14 |
Correct |
112 ms |
53884 KB |
Output is correct |
15 |
Correct |
286 ms |
49940 KB |
Output is correct |
16 |
Correct |
98 ms |
15452 KB |
Output is correct |
17 |
Correct |
80 ms |
15280 KB |
Output is correct |
18 |
Correct |
64 ms |
27504 KB |
Output is correct |
19 |
Correct |
59 ms |
35528 KB |
Output is correct |
20 |
Correct |
59 ms |
44980 KB |
Output is correct |
21 |
Correct |
45 ms |
13480 KB |
Output is correct |
22 |
Correct |
132 ms |
16648 KB |
Output is correct |
23 |
Correct |
252 ms |
22908 KB |
Output is correct |
24 |
Correct |
383 ms |
26152 KB |
Output is correct |
25 |
Correct |
289 ms |
69196 KB |
Output is correct |
26 |
Correct |
262 ms |
32460 KB |
Output is correct |
27 |
Correct |
266 ms |
36392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
12084 KB |
Output is correct |
3 |
Correct |
6 ms |
12020 KB |
Output is correct |
4 |
Correct |
6 ms |
12076 KB |
Output is correct |
5 |
Correct |
6 ms |
12072 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
7 ms |
12076 KB |
Output is correct |
8 |
Correct |
8 ms |
11988 KB |
Output is correct |
9 |
Correct |
6 ms |
11988 KB |
Output is correct |
10 |
Correct |
184 ms |
21580 KB |
Output is correct |
11 |
Correct |
88 ms |
31916 KB |
Output is correct |
12 |
Correct |
469 ms |
30240 KB |
Output is correct |
13 |
Correct |
290 ms |
69192 KB |
Output is correct |
14 |
Correct |
294 ms |
25704 KB |
Output is correct |
15 |
Correct |
7 ms |
12372 KB |
Output is correct |
16 |
Correct |
7 ms |
12372 KB |
Output is correct |
17 |
Correct |
6 ms |
12372 KB |
Output is correct |
18 |
Correct |
135 ms |
49932 KB |
Output is correct |
19 |
Correct |
112 ms |
53884 KB |
Output is correct |
20 |
Correct |
286 ms |
49940 KB |
Output is correct |
21 |
Correct |
69 ms |
15316 KB |
Output is correct |
22 |
Correct |
72 ms |
17452 KB |
Output is correct |
23 |
Correct |
60 ms |
29532 KB |
Output is correct |
24 |
Correct |
53 ms |
29756 KB |
Output is correct |
25 |
Correct |
61 ms |
46960 KB |
Output is correct |
26 |
Correct |
98 ms |
15452 KB |
Output is correct |
27 |
Correct |
80 ms |
15280 KB |
Output is correct |
28 |
Correct |
64 ms |
27504 KB |
Output is correct |
29 |
Correct |
59 ms |
35528 KB |
Output is correct |
30 |
Correct |
59 ms |
44980 KB |
Output is correct |
31 |
Correct |
14 ms |
13040 KB |
Output is correct |
32 |
Correct |
45 ms |
15648 KB |
Output is correct |
33 |
Correct |
45 ms |
15564 KB |
Output is correct |
34 |
Correct |
46 ms |
15720 KB |
Output is correct |
35 |
Correct |
17 ms |
14716 KB |
Output is correct |
36 |
Correct |
37 ms |
21140 KB |
Output is correct |
37 |
Correct |
44 ms |
27852 KB |
Output is correct |
38 |
Correct |
45 ms |
13480 KB |
Output is correct |
39 |
Correct |
132 ms |
16648 KB |
Output is correct |
40 |
Correct |
252 ms |
22908 KB |
Output is correct |
41 |
Correct |
383 ms |
26152 KB |
Output is correct |
42 |
Correct |
289 ms |
69196 KB |
Output is correct |
43 |
Correct |
262 ms |
32460 KB |
Output is correct |
44 |
Correct |
266 ms |
36392 KB |
Output is correct |
45 |
Correct |
46 ms |
14016 KB |
Output is correct |
46 |
Correct |
151 ms |
17896 KB |
Output is correct |
47 |
Correct |
256 ms |
23920 KB |
Output is correct |
48 |
Correct |
440 ms |
29520 KB |
Output is correct |
49 |
Correct |
338 ms |
71276 KB |
Output is correct |
50 |
Correct |
307 ms |
35144 KB |
Output is correct |
51 |
Correct |
347 ms |
39020 KB |
Output is correct |