#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 4e5 + 30, MOD = 1e9 + 7;
#define int ll
int n,m;
vector<pair<int,int>> g[N];
multiset<ll> st[N];
ll h[N],l[N],r[N];
int mx[N];
void merge(int v,int to){
mx[v]++;
for(int j:st[to]){
st[v].insert(j);
}
}
ll dist(int to,ll x){
if(x >= r[to]){
return h[to] + x - r[to];
}
if(x >= l[to]) return h[to];
st[to].erase(st[to].find(r[to]));
ll ret = h[to],f = h[to] + l[to] - x;
for(ll j:st[to]){
if(j >= x && j <= l[to]){
ll val = (j - x);
// cout << l[to] << ' ' << x << ' ' << j << '\n';
ret += val;
}
}
// cout << f << ' ' << ret << "A\n";
st[to].insert(r[to]);
return ret;
}
void dfs(int v,int W = 0){
if(g[v].empty()){
st[v].insert(W);
st[v].insert(W);
mx[v] = 1;
h[v] = 0;
l[v] = r[v] = W;
return;
}
for(auto [to,w]:g[v]){
dfs(to,w);
merge(v,to);
}
while(mx[v] > 1){
mx[v]--;
st[v].erase(st[v].find(*st[v].rbegin()));
}
ll x = (*st[v].rbegin());
st[v].erase(st[v].find(*st[v].rbegin()));
ll y = (*st[v].rbegin());
st[v].erase(st[v].find(*st[v].rbegin()));
st[v].insert(x + W);
st[v].insert(y + W);
r[v] = (*st[v].rbegin());
l[v] = (*(++(st[v].rbegin())));
for(auto [to,w]:g[v]){
h[v] += dist(to,r[v] - W);
}
}
void test(){
cin >> n >> m;
int P = -1;
for(int i = 2;i <= n + m;i++){
int a,b;
cin >> a >> b;
g[a].push_back({i,b});
if(a == 1){
P = i;
}
}
dfs(1);
cout << h[1] << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);cin.tie(0);
int t = 1;
// cin >> t;
while(t--){
test();
}
}
Compilation message
fireworks.cpp: In function 'll dist(ll, ll)':
fireworks.cpp:26:20: warning: unused variable 'f' [-Wunused-variable]
26 | ll ret = h[to],f = h[to] + l[to] - x;
| ^
fireworks.cpp: In function 'void test()':
fireworks.cpp:69:9: warning: variable 'P' set but not used [-Wunused-but-set-variable]
69 | int P = -1;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
34652 KB |
Output is correct |
2 |
Correct |
5 ms |
34652 KB |
Output is correct |
3 |
Correct |
5 ms |
34652 KB |
Output is correct |
4 |
Correct |
5 ms |
34652 KB |
Output is correct |
5 |
Correct |
5 ms |
34652 KB |
Output is correct |
6 |
Correct |
5 ms |
34652 KB |
Output is correct |
7 |
Correct |
6 ms |
34652 KB |
Output is correct |
8 |
Correct |
5 ms |
34652 KB |
Output is correct |
9 |
Correct |
5 ms |
34648 KB |
Output is correct |
10 |
Correct |
6 ms |
34848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
34652 KB |
Output is correct |
2 |
Correct |
5 ms |
34652 KB |
Output is correct |
3 |
Correct |
5 ms |
34652 KB |
Output is correct |
4 |
Correct |
6 ms |
34652 KB |
Output is correct |
5 |
Correct |
6 ms |
34652 KB |
Output is correct |
6 |
Correct |
6 ms |
34860 KB |
Output is correct |
7 |
Correct |
6 ms |
34652 KB |
Output is correct |
8 |
Correct |
6 ms |
34652 KB |
Output is correct |
9 |
Correct |
6 ms |
34652 KB |
Output is correct |
10 |
Correct |
5 ms |
34652 KB |
Output is correct |
11 |
Correct |
5 ms |
34736 KB |
Output is correct |
12 |
Correct |
5 ms |
34648 KB |
Output is correct |
13 |
Correct |
5 ms |
34648 KB |
Output is correct |
14 |
Correct |
5 ms |
34648 KB |
Output is correct |
15 |
Correct |
5 ms |
34776 KB |
Output is correct |
16 |
Correct |
5 ms |
34652 KB |
Output is correct |
17 |
Correct |
5 ms |
34784 KB |
Output is correct |
18 |
Correct |
6 ms |
34652 KB |
Output is correct |
19 |
Correct |
6 ms |
34648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
34652 KB |
Output is correct |
2 |
Correct |
5 ms |
34652 KB |
Output is correct |
3 |
Correct |
5 ms |
34652 KB |
Output is correct |
4 |
Correct |
5 ms |
34652 KB |
Output is correct |
5 |
Correct |
5 ms |
34652 KB |
Output is correct |
6 |
Correct |
5 ms |
34652 KB |
Output is correct |
7 |
Correct |
6 ms |
34652 KB |
Output is correct |
8 |
Correct |
5 ms |
34652 KB |
Output is correct |
9 |
Correct |
5 ms |
34648 KB |
Output is correct |
10 |
Correct |
6 ms |
34848 KB |
Output is correct |
11 |
Correct |
6 ms |
34652 KB |
Output is correct |
12 |
Correct |
5 ms |
34652 KB |
Output is correct |
13 |
Correct |
5 ms |
34652 KB |
Output is correct |
14 |
Correct |
6 ms |
34652 KB |
Output is correct |
15 |
Correct |
6 ms |
34652 KB |
Output is correct |
16 |
Correct |
6 ms |
34860 KB |
Output is correct |
17 |
Correct |
6 ms |
34652 KB |
Output is correct |
18 |
Correct |
6 ms |
34652 KB |
Output is correct |
19 |
Correct |
6 ms |
34652 KB |
Output is correct |
20 |
Correct |
5 ms |
34652 KB |
Output is correct |
21 |
Correct |
5 ms |
34736 KB |
Output is correct |
22 |
Correct |
5 ms |
34648 KB |
Output is correct |
23 |
Correct |
5 ms |
34648 KB |
Output is correct |
24 |
Correct |
5 ms |
34648 KB |
Output is correct |
25 |
Correct |
5 ms |
34776 KB |
Output is correct |
26 |
Correct |
5 ms |
34652 KB |
Output is correct |
27 |
Correct |
5 ms |
34784 KB |
Output is correct |
28 |
Correct |
6 ms |
34652 KB |
Output is correct |
29 |
Correct |
6 ms |
34648 KB |
Output is correct |
30 |
Correct |
7 ms |
34908 KB |
Output is correct |
31 |
Correct |
7 ms |
35720 KB |
Output is correct |
32 |
Correct |
9 ms |
35932 KB |
Output is correct |
33 |
Correct |
10 ms |
36700 KB |
Output is correct |
34 |
Correct |
12 ms |
37348 KB |
Output is correct |
35 |
Correct |
16 ms |
40280 KB |
Output is correct |
36 |
Correct |
16 ms |
39772 KB |
Output is correct |
37 |
Correct |
16 ms |
39260 KB |
Output is correct |
38 |
Correct |
29 ms |
46772 KB |
Output is correct |
39 |
Correct |
18 ms |
40792 KB |
Output is correct |
40 |
Correct |
7 ms |
35928 KB |
Output is correct |
41 |
Correct |
7 ms |
35932 KB |
Output is correct |
42 |
Correct |
7 ms |
35440 KB |
Output is correct |
43 |
Correct |
276 ms |
182352 KB |
Output is correct |
44 |
Correct |
264 ms |
144208 KB |
Output is correct |
45 |
Correct |
196 ms |
142160 KB |
Output is correct |
46 |
Correct |
8 ms |
35928 KB |
Output is correct |
47 |
Correct |
8 ms |
35932 KB |
Output is correct |
48 |
Correct |
8 ms |
35676 KB |
Output is correct |
49 |
Correct |
8 ms |
35856 KB |
Output is correct |
50 |
Correct |
8 ms |
35676 KB |
Output is correct |
51 |
Correct |
8 ms |
35540 KB |
Output is correct |
52 |
Correct |
14 ms |
38492 KB |
Output is correct |
53 |
Correct |
9 ms |
36364 KB |
Output is correct |
54 |
Correct |
8 ms |
35932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
34652 KB |
Output is correct |
2 |
Correct |
5 ms |
34652 KB |
Output is correct |
3 |
Correct |
5 ms |
34652 KB |
Output is correct |
4 |
Correct |
5 ms |
34652 KB |
Output is correct |
5 |
Correct |
5 ms |
34652 KB |
Output is correct |
6 |
Correct |
5 ms |
34652 KB |
Output is correct |
7 |
Correct |
6 ms |
34652 KB |
Output is correct |
8 |
Correct |
5 ms |
34652 KB |
Output is correct |
9 |
Correct |
5 ms |
34648 KB |
Output is correct |
10 |
Correct |
6 ms |
34848 KB |
Output is correct |
11 |
Correct |
6 ms |
34652 KB |
Output is correct |
12 |
Correct |
5 ms |
34652 KB |
Output is correct |
13 |
Correct |
5 ms |
34652 KB |
Output is correct |
14 |
Correct |
6 ms |
34652 KB |
Output is correct |
15 |
Correct |
6 ms |
34652 KB |
Output is correct |
16 |
Correct |
6 ms |
34860 KB |
Output is correct |
17 |
Correct |
6 ms |
34652 KB |
Output is correct |
18 |
Correct |
6 ms |
34652 KB |
Output is correct |
19 |
Correct |
6 ms |
34652 KB |
Output is correct |
20 |
Correct |
5 ms |
34652 KB |
Output is correct |
21 |
Correct |
5 ms |
34736 KB |
Output is correct |
22 |
Correct |
5 ms |
34648 KB |
Output is correct |
23 |
Correct |
5 ms |
34648 KB |
Output is correct |
24 |
Correct |
5 ms |
34648 KB |
Output is correct |
25 |
Correct |
5 ms |
34776 KB |
Output is correct |
26 |
Correct |
5 ms |
34652 KB |
Output is correct |
27 |
Correct |
5 ms |
34784 KB |
Output is correct |
28 |
Correct |
6 ms |
34652 KB |
Output is correct |
29 |
Correct |
6 ms |
34648 KB |
Output is correct |
30 |
Correct |
7 ms |
34908 KB |
Output is correct |
31 |
Correct |
7 ms |
35720 KB |
Output is correct |
32 |
Correct |
9 ms |
35932 KB |
Output is correct |
33 |
Correct |
10 ms |
36700 KB |
Output is correct |
34 |
Correct |
12 ms |
37348 KB |
Output is correct |
35 |
Correct |
16 ms |
40280 KB |
Output is correct |
36 |
Correct |
16 ms |
39772 KB |
Output is correct |
37 |
Correct |
16 ms |
39260 KB |
Output is correct |
38 |
Correct |
29 ms |
46772 KB |
Output is correct |
39 |
Correct |
18 ms |
40792 KB |
Output is correct |
40 |
Correct |
7 ms |
35928 KB |
Output is correct |
41 |
Correct |
7 ms |
35932 KB |
Output is correct |
42 |
Correct |
7 ms |
35440 KB |
Output is correct |
43 |
Correct |
276 ms |
182352 KB |
Output is correct |
44 |
Correct |
264 ms |
144208 KB |
Output is correct |
45 |
Correct |
196 ms |
142160 KB |
Output is correct |
46 |
Correct |
8 ms |
35928 KB |
Output is correct |
47 |
Correct |
8 ms |
35932 KB |
Output is correct |
48 |
Correct |
8 ms |
35676 KB |
Output is correct |
49 |
Correct |
8 ms |
35856 KB |
Output is correct |
50 |
Correct |
8 ms |
35676 KB |
Output is correct |
51 |
Correct |
8 ms |
35540 KB |
Output is correct |
52 |
Correct |
14 ms |
38492 KB |
Output is correct |
53 |
Correct |
9 ms |
36364 KB |
Output is correct |
54 |
Correct |
8 ms |
35932 KB |
Output is correct |
55 |
Correct |
69 ms |
66184 KB |
Output is correct |
56 |
Correct |
582 ms |
307280 KB |
Output is correct |
57 |
Runtime error |
960 ms |
524288 KB |
Execution killed with signal 9 |
58 |
Halted |
0 ms |
0 KB |
- |