/*
-------------- | /
| | /
| | /
| * |/ | | ------ *
| | | | / \
| | |\ | | | |\ |
\ | | | \ | | | | \ |
\ | | | \ | | \ / \ |
V | | \ \__/| ----- \ |
*/
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << "\e[1;31m" << #x << " = " << (x) << "\e[0m\n"
#define print(x) emilia_mata_tenshi(#x, begin(x), end(x))
template<typename T> void emilia_mata_tenshi(const char *s, T l, T r) {
cerr << "\e[1;33m" << s << " = [";
while(l != r) {
cerr << *l;
cerr << (++l == r ? ']' : ',');
}
cerr << "\e[0m\n";
}
#define EmiliaMyWife ios::sync_with_stdio(0); cin.tie(NULL);
using ll = int64_t;
using ull = uint64_t;
using ld = long double;
using uint = uint32_t;
const double EPS = 1e-7;
const int INF = 0x3F3F3F3F;
const ll LINF = 4611686018427387903;
const int MOD = 1e9+7;
static int Lamy_is_cute = []() {
EmiliaMyWife
return 48763;
}();
/*--------------------------------------------------------------------------------------*/
struct dat {
priority_queue<ll> slope;
ll l = 0, r = 0;
ll zero_ans = 0;
int zero_slope = 0;
inline int size() { return slope.size(); }
void shift(int c) {
while(slope.size() > zero_slope)
slope.pop();
if(zero_slope == 0) {
zero_slope++;
}
else
slope.pop();
l += c, r += c;
slope.push(l);
slope.push(r);
zero_ans += c;
}
void update() {
while(slope.size() > zero_slope + 1)
slope.pop();
ll a = slope.top(); slope.pop();
l = slope.top();
r = a;
slope.push(a);
}
void merge(dat b) {
while(!b.slope.empty()) {
slope.push(b.slope.top());
b.slope.pop();
}
zero_ans += b.zero_ans;
zero_slope += b.zero_slope;
}
};
signed main() {
int n, m;
cin >> n >> m;
n += m;
vector<vector<pair<int, int>>> edge(n + 1);
for(int i = 2; i <= n; i++) {
int p, c;
cin >> p >> c;
edge[p].push_back({i, c});
}
vector<dat> arr(n + 1);
function<void(int)> dfs = [&](int u) {
if(edge[u].empty())
return;
for(const auto &[v, c] : edge[u]) {
dfs(v);
arr[v].shift(c);
if(arr[u].slope.empty()) {
swap(arr[u], arr[v]);
continue;
}
if(arr[v].size() > arr[u].size())
swap(arr[u], arr[v]);
arr[u].merge(arr[v]);
}
arr[u].update();
/* debug
debug(u);
ll cur = arr[u].zero_ans, sl = -arr[u].zero_slope;
vector<ll> ch;
while(!arr[u].slope.empty()) {
ch.push_back(arr[u].slope.top());
arr[u].slope.pop();
}
for(ll a : ch)
arr[u].slope.push(a);
reverse(ch.begin(), ch.end());
ll prv = 0;
print(ch);
debug(cur);
debug(sl);
for(ll a : ch) {
cur += (a - prv) * sl;
sl++;
cerr << a << ' ' << cur << '\n';
if(sl == 0)
break;
prv = a;
}
cerr << "-------------------\n";
*/
};
dfs(1);
ll cur = arr[1].zero_ans, sl = -arr[1].zero_slope;
vector<ll> ch;
while(!arr[1].slope.empty()) {
ch.push_back(arr[1].slope.top());
arr[1].slope.pop();
}
reverse(ch.begin(), ch.end());
ll prv = 0;
/* debug
print(ch);
debug(cur);
debug(sl);
*/
for(ll a : ch) {
cur += (a - prv) * sl;
sl++;
//cerr << a << ' ' << cur << '\n';
if(sl == 0)
break;
prv = a;
}
cout << cur << '\n';
}
Compilation message
fireworks.cpp: In member function 'void dat::shift(int)':
fireworks.cpp:47:22: warning: comparison of integer expressions of different signedness: 'std::priority_queue<long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
47 | while(slope.size() > zero_slope)
| ~~~~~~~~~~~~~^~~~~~~~~~~~
fireworks.cpp: In member function 'void dat::update()':
fireworks.cpp:60:22: warning: comparison of integer expressions of different signedness: 'std::priority_queue<long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
60 | while(slope.size() > zero_slope + 1)
| ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
312 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 |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
312 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
308 KB |
Output is correct |
14 |
Correct |
1 ms |
316 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
316 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
312 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
312 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
316 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
308 KB |
Output is correct |
24 |
Correct |
1 ms |
316 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
316 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
304 KB |
Output is correct |
31 |
Correct |
1 ms |
460 KB |
Output is correct |
32 |
Correct |
1 ms |
460 KB |
Output is correct |
33 |
Correct |
2 ms |
580 KB |
Output is correct |
34 |
Correct |
2 ms |
716 KB |
Output is correct |
35 |
Correct |
2 ms |
844 KB |
Output is correct |
36 |
Correct |
3 ms |
960 KB |
Output is correct |
37 |
Correct |
4 ms |
1072 KB |
Output is correct |
38 |
Correct |
4 ms |
1100 KB |
Output is correct |
39 |
Correct |
3 ms |
1068 KB |
Output is correct |
40 |
Correct |
3 ms |
1996 KB |
Output is correct |
41 |
Correct |
3 ms |
1992 KB |
Output is correct |
42 |
Correct |
2 ms |
972 KB |
Output is correct |
43 |
Correct |
4 ms |
1484 KB |
Output is correct |
44 |
Correct |
3 ms |
1356 KB |
Output is correct |
45 |
Correct |
5 ms |
1356 KB |
Output is correct |
46 |
Correct |
4 ms |
1228 KB |
Output is correct |
47 |
Correct |
4 ms |
1228 KB |
Output is correct |
48 |
Correct |
3 ms |
1100 KB |
Output is correct |
49 |
Correct |
3 ms |
1228 KB |
Output is correct |
50 |
Correct |
5 ms |
972 KB |
Output is correct |
51 |
Correct |
3 ms |
964 KB |
Output is correct |
52 |
Correct |
4 ms |
1100 KB |
Output is correct |
53 |
Correct |
3 ms |
1100 KB |
Output is correct |
54 |
Correct |
3 ms |
1100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
312 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
312 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
316 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
308 KB |
Output is correct |
24 |
Correct |
1 ms |
316 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
316 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
304 KB |
Output is correct |
31 |
Correct |
1 ms |
460 KB |
Output is correct |
32 |
Correct |
1 ms |
460 KB |
Output is correct |
33 |
Correct |
2 ms |
580 KB |
Output is correct |
34 |
Correct |
2 ms |
716 KB |
Output is correct |
35 |
Correct |
2 ms |
844 KB |
Output is correct |
36 |
Correct |
3 ms |
960 KB |
Output is correct |
37 |
Correct |
4 ms |
1072 KB |
Output is correct |
38 |
Correct |
4 ms |
1100 KB |
Output is correct |
39 |
Correct |
3 ms |
1068 KB |
Output is correct |
40 |
Correct |
3 ms |
1996 KB |
Output is correct |
41 |
Correct |
3 ms |
1992 KB |
Output is correct |
42 |
Correct |
2 ms |
972 KB |
Output is correct |
43 |
Correct |
4 ms |
1484 KB |
Output is correct |
44 |
Correct |
3 ms |
1356 KB |
Output is correct |
45 |
Correct |
5 ms |
1356 KB |
Output is correct |
46 |
Correct |
4 ms |
1228 KB |
Output is correct |
47 |
Correct |
4 ms |
1228 KB |
Output is correct |
48 |
Correct |
3 ms |
1100 KB |
Output is correct |
49 |
Correct |
3 ms |
1228 KB |
Output is correct |
50 |
Correct |
5 ms |
972 KB |
Output is correct |
51 |
Correct |
3 ms |
964 KB |
Output is correct |
52 |
Correct |
4 ms |
1100 KB |
Output is correct |
53 |
Correct |
3 ms |
1100 KB |
Output is correct |
54 |
Correct |
3 ms |
1100 KB |
Output is correct |
55 |
Correct |
8 ms |
2328 KB |
Output is correct |
56 |
Correct |
33 ms |
8124 KB |
Output is correct |
57 |
Correct |
58 ms |
13328 KB |
Output is correct |
58 |
Correct |
83 ms |
17244 KB |
Output is correct |
59 |
Correct |
115 ms |
22572 KB |
Output is correct |
60 |
Correct |
144 ms |
27828 KB |
Output is correct |
61 |
Correct |
174 ms |
32224 KB |
Output is correct |
62 |
Correct |
222 ms |
35564 KB |
Output is correct |
63 |
Correct |
234 ms |
42232 KB |
Output is correct |
64 |
Correct |
239 ms |
44272 KB |
Output is correct |
65 |
Correct |
133 ms |
101488 KB |
Output is correct |
66 |
Correct |
136 ms |
101384 KB |
Output is correct |
67 |
Correct |
122 ms |
40552 KB |
Output is correct |
68 |
Correct |
188 ms |
75000 KB |
Output is correct |
69 |
Correct |
239 ms |
70492 KB |
Output is correct |
70 |
Correct |
241 ms |
70308 KB |
Output is correct |
71 |
Correct |
225 ms |
60744 KB |
Output is correct |
72 |
Correct |
270 ms |
60648 KB |
Output is correct |
73 |
Correct |
201 ms |
57040 KB |
Output is correct |
74 |
Correct |
205 ms |
57240 KB |
Output is correct |
75 |
Correct |
221 ms |
55576 KB |
Output is correct |
76 |
Correct |
213 ms |
55708 KB |
Output is correct |
77 |
Correct |
215 ms |
54116 KB |
Output is correct |
78 |
Correct |
200 ms |
52984 KB |
Output is correct |
79 |
Correct |
232 ms |
52444 KB |
Output is correct |