#include <stdio.h>
#include <vector>
#include <queue>
#include <algorithm>
#include <iostream>
#include <string>
#include <bitset>
#include <map>
#include <set>
#include <tuple>
#include <string.h>
#include <math.h>
#include <random>
#include <functional>
#include <assert.h>
#include <math.h>
#define all(x) (x).begin(), (x).end()
#define xx first
#define yy second
using namespace std;
template<typename T, typename Pr = less<T>>
using pq = priority_queue<T, vector<T>, Pr>;
using i64 = long long int;
using ii = pair<int, int>;
using ii64 = pair<i64, i64>;
vector<ii64> edge[300005];
struct Data
{
pq<i64> lside;
pq<i64, greater<i64>> rside;
i64 val = 0;
void apply(i64 len)
{
if (lside.empty())
{
lside.push(len);
rside.push(len);
return;
}
auto x = lside.top();
lside.pop();
lside.push(x + len);
auto pt = rside.top();
rside = pq<i64, greater<i64>>();
rside.push(pt + len);
}
void merge(Data& r)
{
if (lside.empty())
{
swap(lside, r.lside);
swap(rside, r.rside);
val = r.val;
return;
}
val += r.val;
while (!r.lside.empty())
{
auto t = r.lside.top();
r.lside.pop();
if (rside.top() > t)
{
lside.push(t);
continue;
}
auto pt = rside.top();
val += t - pt;
lside.push(pt);
rside.pop();
rside.push(t);
}
while (!r.rside.empty())
{
auto t = r.rside.top();
r.rside.pop();
if (lside.top() < t)
{
rside.push(t);
continue;
}
auto pt = lside.top();
val += pt - t;
rside.push(pt);
lside.pop();
lside.push(t);
}
}
};
Data dfs(int root)
{
Data result;
for (auto& [e, c] : edge[root])
{
auto x = dfs(e);
x.apply(c);
result.merge(x);
}
return result;
}
int main()
{
int n, m;
scanf("%d %d", &n, &m);
for (int i = 2; i <= n + m; i++)
{
i64 p, c;
scanf("%lld %lld", &p, &c);
edge[p].emplace_back(i, c);
}
printf("%lld\n", dfs(1).val);
return 0;
}
Compilation message
fireworks.cpp: In function 'int main()':
fireworks.cpp:124:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
124 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
fireworks.cpp:129:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
129 | scanf("%lld %lld", &p, &c);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7244 KB |
Output is correct |
2 |
Correct |
5 ms |
7336 KB |
Output is correct |
3 |
Correct |
5 ms |
7244 KB |
Output is correct |
4 |
Correct |
5 ms |
7244 KB |
Output is correct |
5 |
Correct |
5 ms |
7244 KB |
Output is correct |
6 |
Correct |
5 ms |
7244 KB |
Output is correct |
7 |
Correct |
5 ms |
7244 KB |
Output is correct |
8 |
Correct |
5 ms |
7244 KB |
Output is correct |
9 |
Correct |
5 ms |
7244 KB |
Output is correct |
10 |
Correct |
5 ms |
7244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7244 KB |
Output is correct |
2 |
Correct |
6 ms |
7244 KB |
Output is correct |
3 |
Correct |
5 ms |
7244 KB |
Output is correct |
4 |
Correct |
5 ms |
7336 KB |
Output is correct |
5 |
Correct |
6 ms |
7244 KB |
Output is correct |
6 |
Correct |
5 ms |
7244 KB |
Output is correct |
7 |
Correct |
5 ms |
7244 KB |
Output is correct |
8 |
Correct |
5 ms |
7244 KB |
Output is correct |
9 |
Correct |
5 ms |
7244 KB |
Output is correct |
10 |
Correct |
5 ms |
7244 KB |
Output is correct |
11 |
Correct |
5 ms |
7244 KB |
Output is correct |
12 |
Correct |
5 ms |
7244 KB |
Output is correct |
13 |
Correct |
5 ms |
7244 KB |
Output is correct |
14 |
Correct |
5 ms |
7372 KB |
Output is correct |
15 |
Correct |
5 ms |
7244 KB |
Output is correct |
16 |
Correct |
5 ms |
7244 KB |
Output is correct |
17 |
Correct |
5 ms |
7244 KB |
Output is correct |
18 |
Correct |
5 ms |
7336 KB |
Output is correct |
19 |
Correct |
5 ms |
7244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7244 KB |
Output is correct |
2 |
Correct |
5 ms |
7336 KB |
Output is correct |
3 |
Correct |
5 ms |
7244 KB |
Output is correct |
4 |
Correct |
5 ms |
7244 KB |
Output is correct |
5 |
Correct |
5 ms |
7244 KB |
Output is correct |
6 |
Correct |
5 ms |
7244 KB |
Output is correct |
7 |
Correct |
5 ms |
7244 KB |
Output is correct |
8 |
Correct |
5 ms |
7244 KB |
Output is correct |
9 |
Correct |
5 ms |
7244 KB |
Output is correct |
10 |
Correct |
5 ms |
7244 KB |
Output is correct |
11 |
Correct |
5 ms |
7244 KB |
Output is correct |
12 |
Correct |
6 ms |
7244 KB |
Output is correct |
13 |
Correct |
5 ms |
7244 KB |
Output is correct |
14 |
Correct |
5 ms |
7336 KB |
Output is correct |
15 |
Correct |
6 ms |
7244 KB |
Output is correct |
16 |
Correct |
5 ms |
7244 KB |
Output is correct |
17 |
Correct |
5 ms |
7244 KB |
Output is correct |
18 |
Correct |
5 ms |
7244 KB |
Output is correct |
19 |
Correct |
5 ms |
7244 KB |
Output is correct |
20 |
Correct |
5 ms |
7244 KB |
Output is correct |
21 |
Correct |
5 ms |
7244 KB |
Output is correct |
22 |
Correct |
5 ms |
7244 KB |
Output is correct |
23 |
Correct |
5 ms |
7244 KB |
Output is correct |
24 |
Correct |
5 ms |
7372 KB |
Output is correct |
25 |
Correct |
5 ms |
7244 KB |
Output is correct |
26 |
Correct |
5 ms |
7244 KB |
Output is correct |
27 |
Correct |
5 ms |
7244 KB |
Output is correct |
28 |
Correct |
5 ms |
7336 KB |
Output is correct |
29 |
Correct |
5 ms |
7244 KB |
Output is correct |
30 |
Correct |
5 ms |
7372 KB |
Output is correct |
31 |
Correct |
5 ms |
7372 KB |
Output is correct |
32 |
Correct |
6 ms |
7380 KB |
Output is correct |
33 |
Correct |
7 ms |
7444 KB |
Output is correct |
34 |
Correct |
6 ms |
7500 KB |
Output is correct |
35 |
Correct |
7 ms |
7496 KB |
Output is correct |
36 |
Correct |
7 ms |
7552 KB |
Output is correct |
37 |
Correct |
9 ms |
7500 KB |
Output is correct |
38 |
Correct |
7 ms |
7628 KB |
Output is correct |
39 |
Correct |
7 ms |
7568 KB |
Output is correct |
40 |
Correct |
8 ms |
8888 KB |
Output is correct |
41 |
Correct |
8 ms |
8908 KB |
Output is correct |
42 |
Correct |
7 ms |
7480 KB |
Output is correct |
43 |
Correct |
8 ms |
8296 KB |
Output is correct |
44 |
Correct |
10 ms |
8076 KB |
Output is correct |
45 |
Correct |
8 ms |
8012 KB |
Output is correct |
46 |
Correct |
7 ms |
7628 KB |
Output is correct |
47 |
Correct |
7 ms |
7600 KB |
Output is correct |
48 |
Correct |
7 ms |
7484 KB |
Output is correct |
49 |
Correct |
7 ms |
7608 KB |
Output is correct |
50 |
Correct |
7 ms |
7500 KB |
Output is correct |
51 |
Correct |
7 ms |
7500 KB |
Output is correct |
52 |
Correct |
7 ms |
7552 KB |
Output is correct |
53 |
Correct |
7 ms |
7628 KB |
Output is correct |
54 |
Correct |
7 ms |
7628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7244 KB |
Output is correct |
2 |
Correct |
5 ms |
7336 KB |
Output is correct |
3 |
Correct |
5 ms |
7244 KB |
Output is correct |
4 |
Correct |
5 ms |
7244 KB |
Output is correct |
5 |
Correct |
5 ms |
7244 KB |
Output is correct |
6 |
Correct |
5 ms |
7244 KB |
Output is correct |
7 |
Correct |
5 ms |
7244 KB |
Output is correct |
8 |
Correct |
5 ms |
7244 KB |
Output is correct |
9 |
Correct |
5 ms |
7244 KB |
Output is correct |
10 |
Correct |
5 ms |
7244 KB |
Output is correct |
11 |
Correct |
5 ms |
7244 KB |
Output is correct |
12 |
Correct |
6 ms |
7244 KB |
Output is correct |
13 |
Correct |
5 ms |
7244 KB |
Output is correct |
14 |
Correct |
5 ms |
7336 KB |
Output is correct |
15 |
Correct |
6 ms |
7244 KB |
Output is correct |
16 |
Correct |
5 ms |
7244 KB |
Output is correct |
17 |
Correct |
5 ms |
7244 KB |
Output is correct |
18 |
Correct |
5 ms |
7244 KB |
Output is correct |
19 |
Correct |
5 ms |
7244 KB |
Output is correct |
20 |
Correct |
5 ms |
7244 KB |
Output is correct |
21 |
Correct |
5 ms |
7244 KB |
Output is correct |
22 |
Correct |
5 ms |
7244 KB |
Output is correct |
23 |
Correct |
5 ms |
7244 KB |
Output is correct |
24 |
Correct |
5 ms |
7372 KB |
Output is correct |
25 |
Correct |
5 ms |
7244 KB |
Output is correct |
26 |
Correct |
5 ms |
7244 KB |
Output is correct |
27 |
Correct |
5 ms |
7244 KB |
Output is correct |
28 |
Correct |
5 ms |
7336 KB |
Output is correct |
29 |
Correct |
5 ms |
7244 KB |
Output is correct |
30 |
Correct |
5 ms |
7372 KB |
Output is correct |
31 |
Correct |
5 ms |
7372 KB |
Output is correct |
32 |
Correct |
6 ms |
7380 KB |
Output is correct |
33 |
Correct |
7 ms |
7444 KB |
Output is correct |
34 |
Correct |
6 ms |
7500 KB |
Output is correct |
35 |
Correct |
7 ms |
7496 KB |
Output is correct |
36 |
Correct |
7 ms |
7552 KB |
Output is correct |
37 |
Correct |
9 ms |
7500 KB |
Output is correct |
38 |
Correct |
7 ms |
7628 KB |
Output is correct |
39 |
Correct |
7 ms |
7568 KB |
Output is correct |
40 |
Correct |
8 ms |
8888 KB |
Output is correct |
41 |
Correct |
8 ms |
8908 KB |
Output is correct |
42 |
Correct |
7 ms |
7480 KB |
Output is correct |
43 |
Correct |
8 ms |
8296 KB |
Output is correct |
44 |
Correct |
10 ms |
8076 KB |
Output is correct |
45 |
Correct |
8 ms |
8012 KB |
Output is correct |
46 |
Correct |
7 ms |
7628 KB |
Output is correct |
47 |
Correct |
7 ms |
7600 KB |
Output is correct |
48 |
Correct |
7 ms |
7484 KB |
Output is correct |
49 |
Correct |
7 ms |
7608 KB |
Output is correct |
50 |
Correct |
7 ms |
7500 KB |
Output is correct |
51 |
Correct |
7 ms |
7500 KB |
Output is correct |
52 |
Correct |
7 ms |
7552 KB |
Output is correct |
53 |
Correct |
7 ms |
7628 KB |
Output is correct |
54 |
Correct |
7 ms |
7628 KB |
Output is correct |
55 |
Correct |
12 ms |
8028 KB |
Output is correct |
56 |
Correct |
34 ms |
9892 KB |
Output is correct |
57 |
Correct |
60 ms |
11568 KB |
Output is correct |
58 |
Correct |
75 ms |
12860 KB |
Output is correct |
59 |
Correct |
99 ms |
14456 KB |
Output is correct |
60 |
Correct |
139 ms |
16008 KB |
Output is correct |
61 |
Correct |
150 ms |
17564 KB |
Output is correct |
62 |
Correct |
196 ms |
18520 KB |
Output is correct |
63 |
Correct |
279 ms |
20344 KB |
Output is correct |
64 |
Correct |
231 ms |
20864 KB |
Output is correct |
65 |
Correct |
178 ms |
106184 KB |
Output is correct |
66 |
Correct |
170 ms |
106060 KB |
Output is correct |
67 |
Correct |
134 ms |
21788 KB |
Output is correct |
68 |
Correct |
193 ms |
63508 KB |
Output is correct |
69 |
Correct |
244 ms |
57128 KB |
Output is correct |
70 |
Correct |
222 ms |
57116 KB |
Output is correct |
71 |
Correct |
195 ms |
23168 KB |
Output is correct |
72 |
Correct |
184 ms |
23048 KB |
Output is correct |
73 |
Correct |
161 ms |
21580 KB |
Output is correct |
74 |
Correct |
163 ms |
21812 KB |
Output is correct |
75 |
Correct |
161 ms |
20864 KB |
Output is correct |
76 |
Correct |
160 ms |
20904 KB |
Output is correct |
77 |
Correct |
172 ms |
21396 KB |
Output is correct |
78 |
Correct |
184 ms |
21640 KB |
Output is correct |
79 |
Correct |
147 ms |
25692 KB |
Output is correct |