#include<bits/stdc++.h>
#define rall(x) x.rbegin(), x.rend()
#define fi first
#define se second
#define ve vector
typedef long long ll;
using namespace std;
const int N = 350000;
priority_queue<ll> ql[N];
priority_queue<ll, vector<ll>, greater<ll>> qr[N];
unsigned long long ans=0;
vector<pair<int, ll>> g[N];
void add(ll val, int& i, int x){
if(ql[i].top()<val && val < qr[i].top()){
if(x<0){
ql[i].push(val);
}
else{
qr[i].push(val);
}
}
else if(ql[i].top()>=val){
if(x>0){
ans += ql[i].top()-val;
qr[i].push(ql[i].top());
ql[i].pop();
}
ql[i].push(val);
}
else if(qr[i].top()<=val){
if(x<0){
ans += val-qr[i].top();
ql[i].push(qr[i].top());
qr[i].pop();
}
qr[i].push(val);
}
}
void unite(int v, int u){
bool sw = 0;
if(ql[u].size()+qr[u].size() > ql[v].size()+qr[v].size()){swap(v, u); sw=1;}
while(!ql[u].empty()){
add(ql[u].top(), v, -1);
ql[u].pop();
}
while(!qr[u].empty()){
add(qr[u].top(), v, 1);
qr[u].pop();
}
if(sw){
ql[v].swap(ql[u]);
qr[v].swap(qr[u]);
}
}
void dfs(int v){
for(int i = 0;i<g[v].size();i++){
int son = g[v][i].first;
ll w = g[v][i].second;
dfs(son);
if(!g[son].size()){
ql[son].push(w);
qr[son].push(w);
}
else{
ll l = ql[son].top(), r = qr[son].top();
ql[son].pop();
ql[son].push(l+w);
while(!qr[son].empty())qr[son].pop();
qr[son].push(r+w);
}
unite(v, son);
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int n, m;
cin>>n>>m;
for(int i = 1; i<n+m;i++){
int an;ll c;
cin>>an>>c;an--;
g[an].push_back({i, c});
}
dfs(0);
cout<<ans<<'\n';
return 0;
}
Compilation message
fireworks.cpp: In function 'void dfs(int)':
fireworks.cpp:61:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for(int i = 0;i<g[v].size();i++){
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
30444 KB |
Output is correct |
2 |
Correct |
13 ms |
30420 KB |
Output is correct |
3 |
Correct |
13 ms |
30416 KB |
Output is correct |
4 |
Correct |
14 ms |
30552 KB |
Output is correct |
5 |
Correct |
13 ms |
30420 KB |
Output is correct |
6 |
Correct |
13 ms |
30420 KB |
Output is correct |
7 |
Correct |
12 ms |
30416 KB |
Output is correct |
8 |
Correct |
14 ms |
30420 KB |
Output is correct |
9 |
Correct |
14 ms |
30464 KB |
Output is correct |
10 |
Correct |
13 ms |
30420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
30384 KB |
Output is correct |
2 |
Correct |
13 ms |
30460 KB |
Output is correct |
3 |
Correct |
13 ms |
30420 KB |
Output is correct |
4 |
Correct |
13 ms |
30376 KB |
Output is correct |
5 |
Correct |
16 ms |
30464 KB |
Output is correct |
6 |
Correct |
14 ms |
30468 KB |
Output is correct |
7 |
Correct |
14 ms |
30408 KB |
Output is correct |
8 |
Correct |
14 ms |
30384 KB |
Output is correct |
9 |
Correct |
13 ms |
30448 KB |
Output is correct |
10 |
Correct |
14 ms |
30420 KB |
Output is correct |
11 |
Correct |
13 ms |
30400 KB |
Output is correct |
12 |
Correct |
13 ms |
30396 KB |
Output is correct |
13 |
Correct |
14 ms |
30424 KB |
Output is correct |
14 |
Correct |
13 ms |
30512 KB |
Output is correct |
15 |
Correct |
13 ms |
30484 KB |
Output is correct |
16 |
Correct |
13 ms |
30412 KB |
Output is correct |
17 |
Correct |
13 ms |
30420 KB |
Output is correct |
18 |
Correct |
13 ms |
30420 KB |
Output is correct |
19 |
Correct |
14 ms |
30420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
30444 KB |
Output is correct |
2 |
Correct |
13 ms |
30420 KB |
Output is correct |
3 |
Correct |
13 ms |
30416 KB |
Output is correct |
4 |
Correct |
14 ms |
30552 KB |
Output is correct |
5 |
Correct |
13 ms |
30420 KB |
Output is correct |
6 |
Correct |
13 ms |
30420 KB |
Output is correct |
7 |
Correct |
12 ms |
30416 KB |
Output is correct |
8 |
Correct |
14 ms |
30420 KB |
Output is correct |
9 |
Correct |
14 ms |
30464 KB |
Output is correct |
10 |
Correct |
13 ms |
30420 KB |
Output is correct |
11 |
Correct |
13 ms |
30384 KB |
Output is correct |
12 |
Correct |
13 ms |
30460 KB |
Output is correct |
13 |
Correct |
13 ms |
30420 KB |
Output is correct |
14 |
Correct |
13 ms |
30376 KB |
Output is correct |
15 |
Correct |
16 ms |
30464 KB |
Output is correct |
16 |
Correct |
14 ms |
30468 KB |
Output is correct |
17 |
Correct |
14 ms |
30408 KB |
Output is correct |
18 |
Correct |
14 ms |
30384 KB |
Output is correct |
19 |
Correct |
13 ms |
30448 KB |
Output is correct |
20 |
Correct |
14 ms |
30420 KB |
Output is correct |
21 |
Correct |
13 ms |
30400 KB |
Output is correct |
22 |
Correct |
13 ms |
30396 KB |
Output is correct |
23 |
Correct |
14 ms |
30424 KB |
Output is correct |
24 |
Correct |
13 ms |
30512 KB |
Output is correct |
25 |
Correct |
13 ms |
30484 KB |
Output is correct |
26 |
Correct |
13 ms |
30412 KB |
Output is correct |
27 |
Correct |
13 ms |
30420 KB |
Output is correct |
28 |
Correct |
13 ms |
30420 KB |
Output is correct |
29 |
Correct |
14 ms |
30420 KB |
Output is correct |
30 |
Correct |
13 ms |
30452 KB |
Output is correct |
31 |
Correct |
13 ms |
30548 KB |
Output is correct |
32 |
Correct |
14 ms |
30548 KB |
Output is correct |
33 |
Correct |
14 ms |
30600 KB |
Output is correct |
34 |
Correct |
15 ms |
30652 KB |
Output is correct |
35 |
Correct |
15 ms |
30676 KB |
Output is correct |
36 |
Correct |
17 ms |
30812 KB |
Output is correct |
37 |
Correct |
16 ms |
30788 KB |
Output is correct |
38 |
Correct |
16 ms |
30856 KB |
Output is correct |
39 |
Correct |
15 ms |
30808 KB |
Output is correct |
40 |
Correct |
15 ms |
31500 KB |
Output is correct |
41 |
Correct |
15 ms |
31508 KB |
Output is correct |
42 |
Correct |
14 ms |
30692 KB |
Output is correct |
43 |
Correct |
16 ms |
31188 KB |
Output is correct |
44 |
Correct |
15 ms |
31044 KB |
Output is correct |
45 |
Correct |
16 ms |
31136 KB |
Output is correct |
46 |
Correct |
16 ms |
31056 KB |
Output is correct |
47 |
Correct |
15 ms |
31060 KB |
Output is correct |
48 |
Correct |
15 ms |
30984 KB |
Output is correct |
49 |
Correct |
16 ms |
30932 KB |
Output is correct |
50 |
Correct |
18 ms |
30880 KB |
Output is correct |
51 |
Correct |
17 ms |
30900 KB |
Output is correct |
52 |
Correct |
19 ms |
30916 KB |
Output is correct |
53 |
Correct |
16 ms |
30984 KB |
Output is correct |
54 |
Correct |
15 ms |
30980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
30444 KB |
Output is correct |
2 |
Correct |
13 ms |
30420 KB |
Output is correct |
3 |
Correct |
13 ms |
30416 KB |
Output is correct |
4 |
Correct |
14 ms |
30552 KB |
Output is correct |
5 |
Correct |
13 ms |
30420 KB |
Output is correct |
6 |
Correct |
13 ms |
30420 KB |
Output is correct |
7 |
Correct |
12 ms |
30416 KB |
Output is correct |
8 |
Correct |
14 ms |
30420 KB |
Output is correct |
9 |
Correct |
14 ms |
30464 KB |
Output is correct |
10 |
Correct |
13 ms |
30420 KB |
Output is correct |
11 |
Correct |
13 ms |
30384 KB |
Output is correct |
12 |
Correct |
13 ms |
30460 KB |
Output is correct |
13 |
Correct |
13 ms |
30420 KB |
Output is correct |
14 |
Correct |
13 ms |
30376 KB |
Output is correct |
15 |
Correct |
16 ms |
30464 KB |
Output is correct |
16 |
Correct |
14 ms |
30468 KB |
Output is correct |
17 |
Correct |
14 ms |
30408 KB |
Output is correct |
18 |
Correct |
14 ms |
30384 KB |
Output is correct |
19 |
Correct |
13 ms |
30448 KB |
Output is correct |
20 |
Correct |
14 ms |
30420 KB |
Output is correct |
21 |
Correct |
13 ms |
30400 KB |
Output is correct |
22 |
Correct |
13 ms |
30396 KB |
Output is correct |
23 |
Correct |
14 ms |
30424 KB |
Output is correct |
24 |
Correct |
13 ms |
30512 KB |
Output is correct |
25 |
Correct |
13 ms |
30484 KB |
Output is correct |
26 |
Correct |
13 ms |
30412 KB |
Output is correct |
27 |
Correct |
13 ms |
30420 KB |
Output is correct |
28 |
Correct |
13 ms |
30420 KB |
Output is correct |
29 |
Correct |
14 ms |
30420 KB |
Output is correct |
30 |
Correct |
13 ms |
30452 KB |
Output is correct |
31 |
Correct |
13 ms |
30548 KB |
Output is correct |
32 |
Correct |
14 ms |
30548 KB |
Output is correct |
33 |
Correct |
14 ms |
30600 KB |
Output is correct |
34 |
Correct |
15 ms |
30652 KB |
Output is correct |
35 |
Correct |
15 ms |
30676 KB |
Output is correct |
36 |
Correct |
17 ms |
30812 KB |
Output is correct |
37 |
Correct |
16 ms |
30788 KB |
Output is correct |
38 |
Correct |
16 ms |
30856 KB |
Output is correct |
39 |
Correct |
15 ms |
30808 KB |
Output is correct |
40 |
Correct |
15 ms |
31500 KB |
Output is correct |
41 |
Correct |
15 ms |
31508 KB |
Output is correct |
42 |
Correct |
14 ms |
30692 KB |
Output is correct |
43 |
Correct |
16 ms |
31188 KB |
Output is correct |
44 |
Correct |
15 ms |
31044 KB |
Output is correct |
45 |
Correct |
16 ms |
31136 KB |
Output is correct |
46 |
Correct |
16 ms |
31056 KB |
Output is correct |
47 |
Correct |
15 ms |
31060 KB |
Output is correct |
48 |
Correct |
15 ms |
30984 KB |
Output is correct |
49 |
Correct |
16 ms |
30932 KB |
Output is correct |
50 |
Correct |
18 ms |
30880 KB |
Output is correct |
51 |
Correct |
17 ms |
30900 KB |
Output is correct |
52 |
Correct |
19 ms |
30916 KB |
Output is correct |
53 |
Correct |
16 ms |
30984 KB |
Output is correct |
54 |
Correct |
15 ms |
30980 KB |
Output is correct |
55 |
Correct |
18 ms |
31504 KB |
Output is correct |
56 |
Correct |
38 ms |
34380 KB |
Output is correct |
57 |
Correct |
57 ms |
37112 KB |
Output is correct |
58 |
Correct |
73 ms |
38972 KB |
Output is correct |
59 |
Correct |
101 ms |
41832 KB |
Output is correct |
60 |
Correct |
124 ms |
44620 KB |
Output is correct |
61 |
Correct |
183 ms |
46932 KB |
Output is correct |
62 |
Correct |
178 ms |
48344 KB |
Output is correct |
63 |
Correct |
207 ms |
51916 KB |
Output is correct |
64 |
Correct |
217 ms |
52928 KB |
Output is correct |
65 |
Correct |
98 ms |
96300 KB |
Output is correct |
66 |
Correct |
95 ms |
96368 KB |
Output is correct |
67 |
Correct |
96 ms |
44856 KB |
Output is correct |
68 |
Correct |
136 ms |
78540 KB |
Output is correct |
69 |
Correct |
158 ms |
74880 KB |
Output is correct |
70 |
Correct |
160 ms |
74844 KB |
Output is correct |
71 |
Correct |
177 ms |
71496 KB |
Output is correct |
72 |
Correct |
177 ms |
71440 KB |
Output is correct |
73 |
Correct |
156 ms |
67700 KB |
Output is correct |
74 |
Correct |
159 ms |
68060 KB |
Output is correct |
75 |
Correct |
170 ms |
66312 KB |
Output is correct |
76 |
Correct |
160 ms |
66284 KB |
Output is correct |
77 |
Correct |
166 ms |
64496 KB |
Output is correct |
78 |
Correct |
164 ms |
63452 KB |
Output is correct |
79 |
Correct |
124 ms |
63808 KB |
Output is correct |