#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
#define sz(x) (int)(x.size())
#define fi(a,b) for(int i=a;i<b;++i)
#define fj(a,b) for(int j=a;j<b;++j)
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
/////////////////////////
int const N = 3e5 + 41;
int n, m;
vector<pii> e[N];
ll ans;
struct Fun{
multiset<ll> l, r;
ll addl, addr;
ll ans;
Fun(){
addl = addr = ans = 0;
};
Fun(int c){
addl = addr = ans = 0;
l.insert(c);
r.insert(c);
}
int getq(){
return sz(l) + sz(r);
}
ll getleftmostmin(){
ll x = (*l.rbegin());
return x + addl;
}
ll getrightmostmin(){
ll x = (*r.begin());
return x + addr;
}
ll extractleftmostmin(){
ll x = (*l.rbegin());
l.erase(l.find(x));
return x + addl;
}
ll extractrightmostmin(){
ll x = (*r.begin());
r.erase(r.begin());
return x + addr;
}
void pushrightpoint(ll x){
if(x >= getleftmostmin()){
r.insert(x - addr);
}else{
ll lx = extractleftmostmin();
ans += lx - x;
l.insert(x - addl);
r.insert(lx - addr);
}
}
void pushright(multiset<ll> &rp, ll addright){
for(ll x : rp){
pushrightpoint(x + addright);
}
}
void pushleftpoint(ll x){
if(x <= getrightmostmin()){
l.insert(x - addl);
}else{
ll rx = extractrightmostmin();
ans += x - rx;
l.insert(rx - addl);
r.insert(x - addr);
}
}
void pushleft(multiset<ll> &lp, ll addleft){
for(ll x : lp){
pushleftpoint(x + addleft);
}
}
void shift(ll s){
addr += s;
addl += s;
ll lx = extractleftmostmin();
addl -= s;
l.insert(lx - addl);
}
void relax(){
while(sz(r) > 1){
ll x = (*r.rbegin());
r.erase(r.find(x));
}
}
};
Fun *f[N];
void dfs(int x, int p = -1, int c = 0){
if(x >= n){
f[x] = new Fun(c);
return;
}
f[x] = new Fun();
int id = x;
for(pii e : ::e[x]){
int y = e.first;
if(y == p) continue;
int c = e.second;
dfs(y, x, c);
if(f[x]->getq() < f[y]->getq()){
f[x] = f[y];
id = y;
}
}
for(pii e : ::e[x]){
int y = e.first;
int c = e.second;
if(y == id) continue;
if(y == p) continue;
f[x]->ans += f[y]->ans;
f[x]->pushleft(f[y]->l, f[y]->addl);
f[x]->pushright(f[y]->r, f[y]->addr);
}
if(p != -1){
f[x]->shift(c);
f[x]->relax();
}
//cerr << x << " " << f[x]->ans << endl;
}
void solve(){
fi(0, n) f[i] = new Fun();
dfs(0);
ans = f[0]->ans;
}
int main(){
#ifdef _DEBUG
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
scanf("%d %d",&n,&m);
fi(1, n+m){
int p, c;
scanf("%d %d",&p,&c);
--p;
e[p].pb(mp(i, c));
e[i].pb(mp(p, c));
}
solve();
printf("%lld\n",ans);
return 0;
}
Compilation message
fireworks.cpp: In function 'void dfs(int, int, int)':
fireworks.cpp:135:7: warning: unused variable 'c' [-Wunused-variable]
int c = e.second;
^
fireworks.cpp: In function 'int main()':
fireworks.cpp:162:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&n,&m);
^
fireworks.cpp:165:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&p,&c);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
11396 KB |
Output is correct |
2 |
Correct |
3 ms |
11396 KB |
Output is correct |
3 |
Correct |
3 ms |
11396 KB |
Output is correct |
4 |
Correct |
3 ms |
11396 KB |
Output is correct |
5 |
Correct |
0 ms |
11396 KB |
Output is correct |
6 |
Correct |
3 ms |
11396 KB |
Output is correct |
7 |
Correct |
3 ms |
11396 KB |
Output is correct |
8 |
Correct |
0 ms |
11396 KB |
Output is correct |
9 |
Correct |
0 ms |
11396 KB |
Output is correct |
10 |
Correct |
3 ms |
11396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
11396 KB |
Output is correct |
2 |
Correct |
3 ms |
11396 KB |
Output is correct |
3 |
Correct |
6 ms |
11396 KB |
Output is correct |
4 |
Correct |
0 ms |
11396 KB |
Output is correct |
5 |
Correct |
6 ms |
11396 KB |
Output is correct |
6 |
Correct |
3 ms |
11396 KB |
Output is correct |
7 |
Correct |
6 ms |
11396 KB |
Output is correct |
8 |
Correct |
0 ms |
11528 KB |
Output is correct |
9 |
Correct |
3 ms |
11528 KB |
Output is correct |
10 |
Correct |
0 ms |
11528 KB |
Output is correct |
11 |
Correct |
3 ms |
11528 KB |
Output is correct |
12 |
Correct |
6 ms |
11528 KB |
Output is correct |
13 |
Correct |
3 ms |
11528 KB |
Output is correct |
14 |
Correct |
0 ms |
11528 KB |
Output is correct |
15 |
Correct |
0 ms |
11528 KB |
Output is correct |
16 |
Correct |
0 ms |
11528 KB |
Output is correct |
17 |
Correct |
6 ms |
11528 KB |
Output is correct |
18 |
Correct |
0 ms |
11528 KB |
Output is correct |
19 |
Correct |
0 ms |
11528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
11396 KB |
Output is correct |
2 |
Correct |
3 ms |
11396 KB |
Output is correct |
3 |
Correct |
3 ms |
11396 KB |
Output is correct |
4 |
Correct |
3 ms |
11396 KB |
Output is correct |
5 |
Correct |
0 ms |
11396 KB |
Output is correct |
6 |
Correct |
3 ms |
11396 KB |
Output is correct |
7 |
Correct |
3 ms |
11396 KB |
Output is correct |
8 |
Correct |
0 ms |
11396 KB |
Output is correct |
9 |
Correct |
0 ms |
11396 KB |
Output is correct |
10 |
Correct |
3 ms |
11396 KB |
Output is correct |
11 |
Correct |
0 ms |
11396 KB |
Output is correct |
12 |
Correct |
3 ms |
11396 KB |
Output is correct |
13 |
Correct |
6 ms |
11396 KB |
Output is correct |
14 |
Correct |
0 ms |
11396 KB |
Output is correct |
15 |
Correct |
6 ms |
11396 KB |
Output is correct |
16 |
Correct |
3 ms |
11396 KB |
Output is correct |
17 |
Correct |
6 ms |
11396 KB |
Output is correct |
18 |
Correct |
0 ms |
11528 KB |
Output is correct |
19 |
Correct |
3 ms |
11528 KB |
Output is correct |
20 |
Correct |
0 ms |
11528 KB |
Output is correct |
21 |
Correct |
3 ms |
11528 KB |
Output is correct |
22 |
Correct |
6 ms |
11528 KB |
Output is correct |
23 |
Correct |
3 ms |
11528 KB |
Output is correct |
24 |
Correct |
0 ms |
11528 KB |
Output is correct |
25 |
Correct |
0 ms |
11528 KB |
Output is correct |
26 |
Correct |
0 ms |
11528 KB |
Output is correct |
27 |
Correct |
6 ms |
11528 KB |
Output is correct |
28 |
Correct |
0 ms |
11528 KB |
Output is correct |
29 |
Correct |
0 ms |
11528 KB |
Output is correct |
30 |
Correct |
3 ms |
11528 KB |
Output is correct |
31 |
Correct |
3 ms |
11792 KB |
Output is correct |
32 |
Correct |
0 ms |
11924 KB |
Output is correct |
33 |
Correct |
6 ms |
12188 KB |
Output is correct |
34 |
Correct |
3 ms |
12320 KB |
Output is correct |
35 |
Correct |
6 ms |
12584 KB |
Output is correct |
36 |
Correct |
9 ms |
12716 KB |
Output is correct |
37 |
Correct |
6 ms |
12980 KB |
Output is correct |
38 |
Correct |
3 ms |
12980 KB |
Output is correct |
39 |
Correct |
6 ms |
12980 KB |
Output is correct |
40 |
Correct |
0 ms |
13428 KB |
Output is correct |
41 |
Correct |
3 ms |
13428 KB |
Output is correct |
42 |
Correct |
6 ms |
12848 KB |
Output is correct |
43 |
Correct |
3 ms |
13076 KB |
Output is correct |
44 |
Correct |
6 ms |
12968 KB |
Output is correct |
45 |
Correct |
6 ms |
12968 KB |
Output is correct |
46 |
Correct |
6 ms |
13244 KB |
Output is correct |
47 |
Correct |
6 ms |
13112 KB |
Output is correct |
48 |
Correct |
6 ms |
12980 KB |
Output is correct |
49 |
Correct |
6 ms |
13112 KB |
Output is correct |
50 |
Correct |
3 ms |
12852 KB |
Output is correct |
51 |
Correct |
13 ms |
12852 KB |
Output is correct |
52 |
Correct |
3 ms |
12852 KB |
Output is correct |
53 |
Correct |
6 ms |
12988 KB |
Output is correct |
54 |
Correct |
9 ms |
13148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
11396 KB |
Output is correct |
2 |
Correct |
3 ms |
11396 KB |
Output is correct |
3 |
Correct |
3 ms |
11396 KB |
Output is correct |
4 |
Correct |
3 ms |
11396 KB |
Output is correct |
5 |
Correct |
0 ms |
11396 KB |
Output is correct |
6 |
Correct |
3 ms |
11396 KB |
Output is correct |
7 |
Correct |
3 ms |
11396 KB |
Output is correct |
8 |
Correct |
0 ms |
11396 KB |
Output is correct |
9 |
Correct |
0 ms |
11396 KB |
Output is correct |
10 |
Correct |
3 ms |
11396 KB |
Output is correct |
11 |
Correct |
0 ms |
11396 KB |
Output is correct |
12 |
Correct |
3 ms |
11396 KB |
Output is correct |
13 |
Correct |
6 ms |
11396 KB |
Output is correct |
14 |
Correct |
0 ms |
11396 KB |
Output is correct |
15 |
Correct |
6 ms |
11396 KB |
Output is correct |
16 |
Correct |
3 ms |
11396 KB |
Output is correct |
17 |
Correct |
6 ms |
11396 KB |
Output is correct |
18 |
Correct |
0 ms |
11528 KB |
Output is correct |
19 |
Correct |
3 ms |
11528 KB |
Output is correct |
20 |
Correct |
0 ms |
11528 KB |
Output is correct |
21 |
Correct |
3 ms |
11528 KB |
Output is correct |
22 |
Correct |
6 ms |
11528 KB |
Output is correct |
23 |
Correct |
3 ms |
11528 KB |
Output is correct |
24 |
Correct |
0 ms |
11528 KB |
Output is correct |
25 |
Correct |
0 ms |
11528 KB |
Output is correct |
26 |
Correct |
0 ms |
11528 KB |
Output is correct |
27 |
Correct |
6 ms |
11528 KB |
Output is correct |
28 |
Correct |
0 ms |
11528 KB |
Output is correct |
29 |
Correct |
0 ms |
11528 KB |
Output is correct |
30 |
Correct |
3 ms |
11528 KB |
Output is correct |
31 |
Correct |
3 ms |
11792 KB |
Output is correct |
32 |
Correct |
0 ms |
11924 KB |
Output is correct |
33 |
Correct |
6 ms |
12188 KB |
Output is correct |
34 |
Correct |
3 ms |
12320 KB |
Output is correct |
35 |
Correct |
6 ms |
12584 KB |
Output is correct |
36 |
Correct |
9 ms |
12716 KB |
Output is correct |
37 |
Correct |
6 ms |
12980 KB |
Output is correct |
38 |
Correct |
3 ms |
12980 KB |
Output is correct |
39 |
Correct |
6 ms |
12980 KB |
Output is correct |
40 |
Correct |
0 ms |
13428 KB |
Output is correct |
41 |
Correct |
3 ms |
13428 KB |
Output is correct |
42 |
Correct |
6 ms |
12848 KB |
Output is correct |
43 |
Correct |
3 ms |
13076 KB |
Output is correct |
44 |
Correct |
6 ms |
12968 KB |
Output is correct |
45 |
Correct |
6 ms |
12968 KB |
Output is correct |
46 |
Correct |
6 ms |
13244 KB |
Output is correct |
47 |
Correct |
6 ms |
13112 KB |
Output is correct |
48 |
Correct |
6 ms |
12980 KB |
Output is correct |
49 |
Correct |
6 ms |
13112 KB |
Output is correct |
50 |
Correct |
3 ms |
12852 KB |
Output is correct |
51 |
Correct |
13 ms |
12852 KB |
Output is correct |
52 |
Correct |
3 ms |
12852 KB |
Output is correct |
53 |
Correct |
6 ms |
12988 KB |
Output is correct |
54 |
Correct |
9 ms |
13148 KB |
Output is correct |
55 |
Correct |
19 ms |
15752 KB |
Output is correct |
56 |
Correct |
89 ms |
27896 KB |
Output is correct |
57 |
Correct |
119 ms |
39908 KB |
Output is correct |
58 |
Correct |
166 ms |
47960 KB |
Output is correct |
59 |
Correct |
189 ms |
60128 KB |
Output is correct |
60 |
Correct |
306 ms |
72380 KB |
Output is correct |
61 |
Correct |
336 ms |
80728 KB |
Output is correct |
62 |
Correct |
356 ms |
88372 KB |
Output is correct |
63 |
Correct |
459 ms |
104192 KB |
Output is correct |
64 |
Correct |
456 ms |
107812 KB |
Output is correct |
65 |
Correct |
223 ms |
137812 KB |
Output is correct |
66 |
Correct |
186 ms |
137808 KB |
Output is correct |
67 |
Correct |
246 ms |
95876 KB |
Output is correct |
68 |
Correct |
366 ms |
121460 KB |
Output is correct |
69 |
Correct |
466 ms |
117820 KB |
Output is correct |
70 |
Correct |
516 ms |
117816 KB |
Output is correct |
71 |
Correct |
563 ms |
130328 KB |
Output is correct |
72 |
Correct |
579 ms |
130328 KB |
Output is correct |
73 |
Correct |
566 ms |
117692 KB |
Output is correct |
74 |
Correct |
583 ms |
117680 KB |
Output is correct |
75 |
Correct |
589 ms |
117344 KB |
Output is correct |
76 |
Correct |
466 ms |
117720 KB |
Output is correct |
77 |
Correct |
553 ms |
115048 KB |
Output is correct |
78 |
Correct |
616 ms |
112512 KB |
Output is correct |
79 |
Correct |
759 ms |
118624 KB |
Output is correct |