#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define X first
#define Y second
#define MP make_pair
using namespace std;
const int N = 1e5 + 2;
const int INF = 1e9;
typedef map<ll, int> DP;
void merge(DP& oth, DP& cur) {
if(cur.size() < oth.size())
cur.swap(oth);
for(auto [pos, val]: oth) {
cur[pos] += val;
}
oth.clear();
}
void insert(DP& cur, int pos, ll val) {
cur[pos] += val;
DP::iterator it = cur.upper_bound(pos);
ll sum = 0;
while(it != cur.end()) {
if(it->second + sum > val) {
it->second += sum - val;
break;
}
else {
sum += it->second;
it = cur.erase(it);
}
}
}
int n, m, k;
DP dp[N];
vector<int> g[N];
pair<int, int> fruit[N];
void dfs(int v) {
for(int to: g[v]) {
dfs(to);
merge(dp[to], dp[v]);
}
insert(dp[v], fruit[v].X, fruit[v].Y);
}
int main () {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> k;
for(int i = 2, v;i <= n;i++) {
cin >> v;
g[v].push_back(i);
}
for(int i = 1;i <= m;i++) {
int v, d, w;
cin >> v >> d >> w;
fruit[v] = {d, w};
}
dfs(1);
ll res = 0;
for(auto [pos, val]: dp[1])
res += val;
cout << res;
}
Compilation message
magictree.cpp: In function 'void merge(DP&, DP&)':
magictree.cpp:21:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
21 | for(auto [pos, val]: oth) {
| ^
magictree.cpp: In function 'int main()':
magictree.cpp:77:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
77 | for(auto [pos, val]: dp[1])
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
5 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7376 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7372 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
59 ms |
12504 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7360 KB |
Output is correct |
2 |
Correct |
6 ms |
7372 KB |
Output is correct |
3 |
Correct |
6 ms |
7440 KB |
Output is correct |
4 |
Correct |
65 ms |
16816 KB |
Output is correct |
5 |
Correct |
71 ms |
20756 KB |
Output is correct |
6 |
Correct |
59 ms |
16896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
68 ms |
11792 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
5 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7376 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7372 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
83 ms |
11228 KB |
Output is correct |
11 |
Correct |
68 ms |
11136 KB |
Output is correct |
12 |
Correct |
52 ms |
13480 KB |
Output is correct |
13 |
Correct |
32 ms |
9672 KB |
Output is correct |
14 |
Correct |
39 ms |
16400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
8020 KB |
Output is correct |
2 |
Correct |
37 ms |
10356 KB |
Output is correct |
3 |
Correct |
49 ms |
10308 KB |
Output is correct |
4 |
Correct |
50 ms |
10316 KB |
Output is correct |
5 |
Correct |
17 ms |
8816 KB |
Output is correct |
6 |
Correct |
30 ms |
10812 KB |
Output is correct |
7 |
Correct |
27 ms |
12620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
5 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7376 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7372 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
5 ms |
7360 KB |
Output is correct |
11 |
Correct |
6 ms |
7372 KB |
Output is correct |
12 |
Correct |
6 ms |
7440 KB |
Output is correct |
13 |
Correct |
65 ms |
16816 KB |
Output is correct |
14 |
Correct |
71 ms |
20756 KB |
Output is correct |
15 |
Correct |
59 ms |
16896 KB |
Output is correct |
16 |
Correct |
83 ms |
11228 KB |
Output is correct |
17 |
Correct |
68 ms |
11136 KB |
Output is correct |
18 |
Correct |
52 ms |
13480 KB |
Output is correct |
19 |
Correct |
32 ms |
9672 KB |
Output is correct |
20 |
Correct |
39 ms |
16400 KB |
Output is correct |
21 |
Correct |
19 ms |
8400 KB |
Output is correct |
22 |
Correct |
72 ms |
11332 KB |
Output is correct |
23 |
Correct |
95 ms |
13112 KB |
Output is correct |
24 |
Correct |
114 ms |
14800 KB |
Output is correct |
25 |
Correct |
61 ms |
13572 KB |
Output is correct |
26 |
Correct |
79 ms |
15980 KB |
Output is correct |
27 |
Correct |
69 ms |
15360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
5 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7376 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7372 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Incorrect |
59 ms |
12504 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |