/**
____ ____ ____ ____ ____
||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;
multiset <pair <int, ll>> upds;
void update (int pos, ll val) {
upds.insert(make_pair(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) {
multiset <pair <int, ll>> :: iterator it = upds.lower_bound(make_pair(pos + 1, 0));
if (it == upds.end()) {
break;
}
int itpos = it->first;
ll itval = it->second;
if (itval <= val) {
update(itpos, -itval);
val -= itval;
upds.erase(upds.find(make_pair(itpos, -itval)));
upds.erase(upds.find(make_pair(itpos, itval)));
} 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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
12088 KB |
Output is correct |
3 |
Correct |
6 ms |
11960 KB |
Output is correct |
4 |
Correct |
7 ms |
11972 KB |
Output is correct |
5 |
Correct |
6 ms |
11988 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
6 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
12000 KB |
Output is correct |
9 |
Correct |
7 ms |
11988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
159 ms |
21612 KB |
Output is correct |
2 |
Correct |
80 ms |
28160 KB |
Output is correct |
3 |
Correct |
447 ms |
31616 KB |
Output is correct |
4 |
Correct |
322 ms |
69072 KB |
Output is correct |
5 |
Correct |
271 ms |
26628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12244 KB |
Output is correct |
2 |
Correct |
9 ms |
12344 KB |
Output is correct |
3 |
Correct |
9 ms |
12288 KB |
Output is correct |
4 |
Correct |
141 ms |
40292 KB |
Output is correct |
5 |
Correct |
161 ms |
46604 KB |
Output is correct |
6 |
Correct |
337 ms |
40396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
146 ms |
21888 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
12088 KB |
Output is correct |
3 |
Correct |
6 ms |
11960 KB |
Output is correct |
4 |
Correct |
7 ms |
11972 KB |
Output is correct |
5 |
Correct |
6 ms |
11988 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
6 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
12000 KB |
Output is correct |
9 |
Correct |
7 ms |
11988 KB |
Output is correct |
10 |
Correct |
139 ms |
19544 KB |
Output is correct |
11 |
Correct |
169 ms |
20180 KB |
Output is correct |
12 |
Correct |
103 ms |
25548 KB |
Output is correct |
13 |
Correct |
83 ms |
35312 KB |
Output is correct |
14 |
Correct |
92 ms |
35812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
12944 KB |
Output is correct |
2 |
Correct |
63 ms |
15488 KB |
Output is correct |
3 |
Correct |
49 ms |
15472 KB |
Output is correct |
4 |
Correct |
48 ms |
15552 KB |
Output is correct |
5 |
Correct |
16 ms |
14548 KB |
Output is correct |
6 |
Correct |
39 ms |
19108 KB |
Output is correct |
7 |
Correct |
42 ms |
23880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
12088 KB |
Output is correct |
3 |
Correct |
6 ms |
11960 KB |
Output is correct |
4 |
Correct |
7 ms |
11972 KB |
Output is correct |
5 |
Correct |
6 ms |
11988 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
6 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
12000 KB |
Output is correct |
9 |
Correct |
7 ms |
11988 KB |
Output is correct |
10 |
Correct |
7 ms |
12244 KB |
Output is correct |
11 |
Correct |
9 ms |
12344 KB |
Output is correct |
12 |
Correct |
9 ms |
12288 KB |
Output is correct |
13 |
Correct |
141 ms |
40292 KB |
Output is correct |
14 |
Correct |
161 ms |
46604 KB |
Output is correct |
15 |
Correct |
337 ms |
40396 KB |
Output is correct |
16 |
Correct |
139 ms |
19544 KB |
Output is correct |
17 |
Correct |
169 ms |
20180 KB |
Output is correct |
18 |
Correct |
103 ms |
25548 KB |
Output is correct |
19 |
Correct |
83 ms |
35312 KB |
Output is correct |
20 |
Correct |
92 ms |
35812 KB |
Output is correct |
21 |
Correct |
64 ms |
14144 KB |
Output is correct |
22 |
Correct |
179 ms |
18416 KB |
Output is correct |
23 |
Correct |
280 ms |
22884 KB |
Output is correct |
24 |
Correct |
407 ms |
26544 KB |
Output is correct |
25 |
Correct |
371 ms |
69056 KB |
Output is correct |
26 |
Correct |
338 ms |
31460 KB |
Output is correct |
27 |
Correct |
321 ms |
32560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
12088 KB |
Output is correct |
3 |
Correct |
6 ms |
11960 KB |
Output is correct |
4 |
Correct |
7 ms |
11972 KB |
Output is correct |
5 |
Correct |
6 ms |
11988 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
6 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
12000 KB |
Output is correct |
9 |
Correct |
7 ms |
11988 KB |
Output is correct |
10 |
Correct |
159 ms |
21612 KB |
Output is correct |
11 |
Correct |
80 ms |
28160 KB |
Output is correct |
12 |
Correct |
447 ms |
31616 KB |
Output is correct |
13 |
Correct |
322 ms |
69072 KB |
Output is correct |
14 |
Correct |
271 ms |
26628 KB |
Output is correct |
15 |
Correct |
7 ms |
12244 KB |
Output is correct |
16 |
Correct |
9 ms |
12344 KB |
Output is correct |
17 |
Correct |
9 ms |
12288 KB |
Output is correct |
18 |
Correct |
141 ms |
40292 KB |
Output is correct |
19 |
Correct |
161 ms |
46604 KB |
Output is correct |
20 |
Correct |
337 ms |
40396 KB |
Output is correct |
21 |
Incorrect |
146 ms |
21888 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |