#include <bits/stdc++.h>
#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif
using namespace std;
vector<pair<int,int>> adj[300000];
int parent[300000];
vector<pair<long long,long long>> D[300000];
void dfs(int c)
{
vector<long long> x(1);
vector<pair<long long,long long>> C;
long long A=0, B=0, A0, B0, A1, B1;
if(adj[c].empty()) {
D[c].emplace_back(0,0);
return;
}
for(auto[n,w]: adj[c]) {
dfs(n);
if(D[n].size()>1 && D[n][D[n].size()-2].second==D[n][D[n].size()-1].second) {
D[n].push_back(D[n].back());
D[n][D[n].size()-2]=D[n][D[n].size()-3];
x.push_back(D[n][D[n].size()-1].first+=w);
x.push_back(D[n][D[n].size()-2].first+=w);
for(int i=0;i<D[n].size()-2;i++) {
D[n][i].second+=w;
x.push_back(D[n][i].first);
}
}
else {
D[n].push_back(D[n].back());
x.push_back(D[n].back().first+=w);
for(int i=0;i<D[n].size()-1;i++) {
D[n][i].second+=w;
x.push_back(D[n][i].first);
}
}
}
sort(x.begin(),x.end());
x.resize(unique(x.begin(),x.end())-x.begin());
C.resize(x.size());
for(auto[n,w]: adj[c]) {
if(D[n].size()>1) {
A+=(D[n][1].second-D[n][0].second)/D[n][1].first;
B+=D[n][0].second;
}
else {
A++;
B+=D[n][0].second;
}
for(int i=1;i<D[n].size();i++) {
int p=lower_bound(x.begin(),x.end(),D[n][i].first)-x.begin();
A0=(D[n][i].second-D[n][i-1].second)/(D[n][i].first-D[n][i-1].first);
B0=-A0*D[n][i].first+D[n][i].second;
if(i+1==D[n].size()) A1=1;
else A1=(D[n][i].second-D[n][i+1].second)/(D[n][i].first-D[n][i+1].first);
B1=-A1*D[n][i].first+D[n][i].second;
C[p].first+=A1-A0;
C[p].second+=B1-B0;
}
}
D[c].emplace_back(0,B);
for(int i=1;i<x.size();i++) {
A+=C[i].first; B+=C[i].second;
while(D[c].size()>1 && (D[c][D[c].size()-2].second-D[c].back().second)/(D[c][D[c].size()-2].first-D[c].back().first)==(A*x[i]+B-D[c].back().second)/(x[i]-D[c].back().first)) D[c].pop_back();
if(A*x[i]+B<=D[c].back().second) D[c].emplace_back(x[i],A*x[i]+B);
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
TEST(freopen("input.txt","r",stdin));
TEST(freopen("output.txt","w",stdout));
TEST(freopen("debug.txt","w",stderr));
int N, M;
cin>>N>>M; N+=M;
for(int i=1;i<N;i++) {
int c;
cin>>parent[i]>>c;
adj[--parent[i]].emplace_back(i,c);
}
dfs(0);
cout<<D[0].back().second<<'\n';
return 0;
}
Compilation message
fireworks.cpp: In function 'void dfs(int)':
fireworks.cpp:33:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<D[n].size()-2;i++) {
~^~~~~~~~~~~~~~
fireworks.cpp:41:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<D[n].size()-1;i++) {
~^~~~~~~~~~~~~~
fireworks.cpp:59:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=1;i<D[n].size();i++) {
~^~~~~~~~~~~~
fireworks.cpp:63:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(i+1==D[n].size()) A1=1;
~~~^~~~~~~~~~~~~
fireworks.cpp:50:14: warning: unused variable 'w' [-Wunused-variable]
for(auto[n,w]: adj[c]) {
^
fireworks.cpp:71:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=1;i<x.size();i++) {
~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Correct |
13 ms |
14464 KB |
Output is correct |
3 |
Correct |
13 ms |
14464 KB |
Output is correct |
4 |
Correct |
13 ms |
14464 KB |
Output is correct |
5 |
Correct |
13 ms |
14464 KB |
Output is correct |
6 |
Correct |
13 ms |
14464 KB |
Output is correct |
7 |
Correct |
13 ms |
14464 KB |
Output is correct |
8 |
Correct |
13 ms |
14464 KB |
Output is correct |
9 |
Correct |
13 ms |
14464 KB |
Output is correct |
10 |
Correct |
13 ms |
14336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
14464 KB |
Output is correct |
2 |
Correct |
13 ms |
14464 KB |
Output is correct |
3 |
Correct |
13 ms |
14464 KB |
Output is correct |
4 |
Correct |
13 ms |
14464 KB |
Output is correct |
5 |
Correct |
13 ms |
14464 KB |
Output is correct |
6 |
Correct |
13 ms |
14464 KB |
Output is correct |
7 |
Correct |
14 ms |
14464 KB |
Output is correct |
8 |
Correct |
13 ms |
14464 KB |
Output is correct |
9 |
Correct |
15 ms |
14464 KB |
Output is correct |
10 |
Correct |
13 ms |
14464 KB |
Output is correct |
11 |
Correct |
13 ms |
14464 KB |
Output is correct |
12 |
Correct |
14 ms |
14464 KB |
Output is correct |
13 |
Correct |
13 ms |
14464 KB |
Output is correct |
14 |
Correct |
14 ms |
14592 KB |
Output is correct |
15 |
Correct |
13 ms |
14464 KB |
Output is correct |
16 |
Correct |
15 ms |
14464 KB |
Output is correct |
17 |
Correct |
2 ms |
14464 KB |
Output is correct |
18 |
Correct |
13 ms |
14464 KB |
Output is correct |
19 |
Correct |
16 ms |
14464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Correct |
13 ms |
14464 KB |
Output is correct |
3 |
Correct |
13 ms |
14464 KB |
Output is correct |
4 |
Correct |
13 ms |
14464 KB |
Output is correct |
5 |
Correct |
13 ms |
14464 KB |
Output is correct |
6 |
Correct |
13 ms |
14464 KB |
Output is correct |
7 |
Correct |
13 ms |
14464 KB |
Output is correct |
8 |
Correct |
13 ms |
14464 KB |
Output is correct |
9 |
Correct |
13 ms |
14464 KB |
Output is correct |
10 |
Correct |
13 ms |
14336 KB |
Output is correct |
11 |
Correct |
12 ms |
14464 KB |
Output is correct |
12 |
Correct |
13 ms |
14464 KB |
Output is correct |
13 |
Correct |
13 ms |
14464 KB |
Output is correct |
14 |
Correct |
13 ms |
14464 KB |
Output is correct |
15 |
Correct |
13 ms |
14464 KB |
Output is correct |
16 |
Correct |
13 ms |
14464 KB |
Output is correct |
17 |
Correct |
14 ms |
14464 KB |
Output is correct |
18 |
Correct |
13 ms |
14464 KB |
Output is correct |
19 |
Correct |
15 ms |
14464 KB |
Output is correct |
20 |
Correct |
13 ms |
14464 KB |
Output is correct |
21 |
Correct |
13 ms |
14464 KB |
Output is correct |
22 |
Correct |
14 ms |
14464 KB |
Output is correct |
23 |
Correct |
13 ms |
14464 KB |
Output is correct |
24 |
Correct |
14 ms |
14592 KB |
Output is correct |
25 |
Correct |
13 ms |
14464 KB |
Output is correct |
26 |
Correct |
15 ms |
14464 KB |
Output is correct |
27 |
Correct |
2 ms |
14464 KB |
Output is correct |
28 |
Correct |
13 ms |
14464 KB |
Output is correct |
29 |
Correct |
16 ms |
14464 KB |
Output is correct |
30 |
Correct |
14 ms |
14592 KB |
Output is correct |
31 |
Correct |
15 ms |
14848 KB |
Output is correct |
32 |
Correct |
15 ms |
14976 KB |
Output is correct |
33 |
Correct |
17 ms |
15232 KB |
Output is correct |
34 |
Correct |
19 ms |
15488 KB |
Output is correct |
35 |
Correct |
27 ms |
16384 KB |
Output is correct |
36 |
Correct |
23 ms |
16256 KB |
Output is correct |
37 |
Correct |
25 ms |
16248 KB |
Output is correct |
38 |
Correct |
34 ms |
18304 KB |
Output is correct |
39 |
Correct |
25 ms |
16768 KB |
Output is correct |
40 |
Correct |
18 ms |
16000 KB |
Output is correct |
41 |
Correct |
18 ms |
16000 KB |
Output is correct |
42 |
Correct |
16 ms |
15104 KB |
Output is correct |
43 |
Correct |
156 ms |
51964 KB |
Output is correct |
44 |
Correct |
127 ms |
43640 KB |
Output is correct |
45 |
Correct |
118 ms |
43512 KB |
Output is correct |
46 |
Correct |
16 ms |
15104 KB |
Output is correct |
47 |
Correct |
16 ms |
15104 KB |
Output is correct |
48 |
Correct |
16 ms |
14976 KB |
Output is correct |
49 |
Correct |
16 ms |
15104 KB |
Output is correct |
50 |
Correct |
16 ms |
15104 KB |
Output is correct |
51 |
Correct |
16 ms |
14976 KB |
Output is correct |
52 |
Correct |
19 ms |
15488 KB |
Output is correct |
53 |
Correct |
17 ms |
15104 KB |
Output is correct |
54 |
Correct |
16 ms |
15104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Correct |
13 ms |
14464 KB |
Output is correct |
3 |
Correct |
13 ms |
14464 KB |
Output is correct |
4 |
Correct |
13 ms |
14464 KB |
Output is correct |
5 |
Correct |
13 ms |
14464 KB |
Output is correct |
6 |
Correct |
13 ms |
14464 KB |
Output is correct |
7 |
Correct |
13 ms |
14464 KB |
Output is correct |
8 |
Correct |
13 ms |
14464 KB |
Output is correct |
9 |
Correct |
13 ms |
14464 KB |
Output is correct |
10 |
Correct |
13 ms |
14336 KB |
Output is correct |
11 |
Correct |
12 ms |
14464 KB |
Output is correct |
12 |
Correct |
13 ms |
14464 KB |
Output is correct |
13 |
Correct |
13 ms |
14464 KB |
Output is correct |
14 |
Correct |
13 ms |
14464 KB |
Output is correct |
15 |
Correct |
13 ms |
14464 KB |
Output is correct |
16 |
Correct |
13 ms |
14464 KB |
Output is correct |
17 |
Correct |
14 ms |
14464 KB |
Output is correct |
18 |
Correct |
13 ms |
14464 KB |
Output is correct |
19 |
Correct |
15 ms |
14464 KB |
Output is correct |
20 |
Correct |
13 ms |
14464 KB |
Output is correct |
21 |
Correct |
13 ms |
14464 KB |
Output is correct |
22 |
Correct |
14 ms |
14464 KB |
Output is correct |
23 |
Correct |
13 ms |
14464 KB |
Output is correct |
24 |
Correct |
14 ms |
14592 KB |
Output is correct |
25 |
Correct |
13 ms |
14464 KB |
Output is correct |
26 |
Correct |
15 ms |
14464 KB |
Output is correct |
27 |
Correct |
2 ms |
14464 KB |
Output is correct |
28 |
Correct |
13 ms |
14464 KB |
Output is correct |
29 |
Correct |
16 ms |
14464 KB |
Output is correct |
30 |
Correct |
14 ms |
14592 KB |
Output is correct |
31 |
Correct |
15 ms |
14848 KB |
Output is correct |
32 |
Correct |
15 ms |
14976 KB |
Output is correct |
33 |
Correct |
17 ms |
15232 KB |
Output is correct |
34 |
Correct |
19 ms |
15488 KB |
Output is correct |
35 |
Correct |
27 ms |
16384 KB |
Output is correct |
36 |
Correct |
23 ms |
16256 KB |
Output is correct |
37 |
Correct |
25 ms |
16248 KB |
Output is correct |
38 |
Correct |
34 ms |
18304 KB |
Output is correct |
39 |
Correct |
25 ms |
16768 KB |
Output is correct |
40 |
Correct |
18 ms |
16000 KB |
Output is correct |
41 |
Correct |
18 ms |
16000 KB |
Output is correct |
42 |
Correct |
16 ms |
15104 KB |
Output is correct |
43 |
Correct |
156 ms |
51964 KB |
Output is correct |
44 |
Correct |
127 ms |
43640 KB |
Output is correct |
45 |
Correct |
118 ms |
43512 KB |
Output is correct |
46 |
Correct |
16 ms |
15104 KB |
Output is correct |
47 |
Correct |
16 ms |
15104 KB |
Output is correct |
48 |
Correct |
16 ms |
14976 KB |
Output is correct |
49 |
Correct |
16 ms |
15104 KB |
Output is correct |
50 |
Correct |
16 ms |
15104 KB |
Output is correct |
51 |
Correct |
16 ms |
14976 KB |
Output is correct |
52 |
Correct |
19 ms |
15488 KB |
Output is correct |
53 |
Correct |
17 ms |
15104 KB |
Output is correct |
54 |
Correct |
16 ms |
15104 KB |
Output is correct |
55 |
Correct |
68 ms |
23672 KB |
Output is correct |
56 |
Correct |
520 ms |
83792 KB |
Output is correct |
57 |
Correct |
1076 ms |
153508 KB |
Output is correct |
58 |
Correct |
1235 ms |
177232 KB |
Output is correct |
59 |
Execution timed out |
2101 ms |
281596 KB |
Time limit exceeded |
60 |
Halted |
0 ms |
0 KB |
- |