/*
* Author: Gyeonggeun Kim(kriii)
* Time Complexity: O(N lg N)
*/
#include <stdio.h>
const int Z = 1<<21;
int N,M,V,par[Z/2],dist[Z/2],child[Z/2],size[Z/2],st[Z/2],ed[Z/2];
long long my[Z/2];
struct node{
node(){
x = i = 0;
}
node(long long x_, int i_){
x = x_; i = i_;
}
long long x; int i;
bool operator <(const node &t)const{
return x < t.x;
};
node operator +(const node &t)const{
if (x < t.x) return t;
return (*this);
}
}I[Z*2];
void in(long long x, int i)
{
I[i+Z] = node(x,i);
i = (i + Z) >> 1;
while (i){
I[i] = I[i*2] + I[i*2+1];
i >>= 1;
}
}
node out(int i, int j)
{
node res;
i += Z; j += Z;
while (i < j){
if (i & 1){
if (res < I[i]) res = I[i];
i++;
}
if (~j & 1){
if (res < I[j]) res = I[j];
j--;
}
i >>= 1; j >>= 1;
}
if (i == j){
if (res < I[i]) res = I[i];
}
return res;
}
long long remove(int s, int e)
{
node t = out(s,e);
in(0,t.i);
return t.x;
}
int main()
{
scanf ("%d %d",&N,&M); V = N + M;
for (int i=2;i<=V;i++){
scanf ("%d %d",&par[i],&dist[i]);
child[par[i]]++;
}
for (int i=V;i>=1;i--){
size[i]++;
size[par[i]] += size[i];
}
ed[1] = 1;
for (int i=2;i<=V;i++){
st[i] = ed[par[i]] + 1;
ed[i] = st[i] + 1;
ed[par[i]] = st[i] + size[i] * 2 - 1;
}
for (int i=V;i>=2;i--){
int s = st[i], e = ed[i];
for (int k=1;k<child[i];k++) my[i] += remove(s,e);
long long p = remove(s,e);
long long q = remove(s,e);
in(dist[i]+p,s);
in(dist[i]+q,s+1);
my[i] -= dist[i];
my[par[i]] += my[i];
}
for (int k=0;k<child[1];k++) my[1] += remove(st[1],ed[1]);
printf ("%lld\n",my[1]);
return 0;
}
Compilation message
fireworks.cpp: In function 'int main()':
fireworks.cpp:71:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%d %d",&N,&M); V = N + M;
^
fireworks.cpp:74:35: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%d %d",&par[i],&dist[i]);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
99420 KB |
Output is correct |
2 |
Correct |
3 ms |
99420 KB |
Output is correct |
3 |
Correct |
3 ms |
99420 KB |
Output is correct |
4 |
Correct |
13 ms |
99420 KB |
Output is correct |
5 |
Correct |
9 ms |
99420 KB |
Output is correct |
6 |
Correct |
16 ms |
99420 KB |
Output is correct |
7 |
Correct |
13 ms |
99420 KB |
Output is correct |
8 |
Correct |
9 ms |
99420 KB |
Output is correct |
9 |
Correct |
6 ms |
99420 KB |
Output is correct |
10 |
Correct |
3 ms |
99420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
99420 KB |
Output is correct |
2 |
Correct |
0 ms |
99420 KB |
Output is correct |
3 |
Correct |
13 ms |
99420 KB |
Output is correct |
4 |
Correct |
9 ms |
99420 KB |
Output is correct |
5 |
Correct |
6 ms |
99420 KB |
Output is correct |
6 |
Correct |
13 ms |
99420 KB |
Output is correct |
7 |
Correct |
0 ms |
99420 KB |
Output is correct |
8 |
Correct |
9 ms |
99420 KB |
Output is correct |
9 |
Correct |
6 ms |
99420 KB |
Output is correct |
10 |
Correct |
0 ms |
99420 KB |
Output is correct |
11 |
Correct |
9 ms |
99420 KB |
Output is correct |
12 |
Correct |
6 ms |
99420 KB |
Output is correct |
13 |
Correct |
6 ms |
99420 KB |
Output is correct |
14 |
Correct |
3 ms |
99420 KB |
Output is correct |
15 |
Correct |
9 ms |
99420 KB |
Output is correct |
16 |
Correct |
3 ms |
99420 KB |
Output is correct |
17 |
Correct |
3 ms |
99420 KB |
Output is correct |
18 |
Correct |
6 ms |
99420 KB |
Output is correct |
19 |
Correct |
6 ms |
99420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
99420 KB |
Output is correct |
2 |
Correct |
3 ms |
99420 KB |
Output is correct |
3 |
Correct |
3 ms |
99420 KB |
Output is correct |
4 |
Correct |
13 ms |
99420 KB |
Output is correct |
5 |
Correct |
9 ms |
99420 KB |
Output is correct |
6 |
Correct |
16 ms |
99420 KB |
Output is correct |
7 |
Correct |
13 ms |
99420 KB |
Output is correct |
8 |
Correct |
9 ms |
99420 KB |
Output is correct |
9 |
Correct |
6 ms |
99420 KB |
Output is correct |
10 |
Correct |
3 ms |
99420 KB |
Output is correct |
11 |
Correct |
13 ms |
99420 KB |
Output is correct |
12 |
Correct |
0 ms |
99420 KB |
Output is correct |
13 |
Correct |
13 ms |
99420 KB |
Output is correct |
14 |
Correct |
9 ms |
99420 KB |
Output is correct |
15 |
Correct |
6 ms |
99420 KB |
Output is correct |
16 |
Correct |
13 ms |
99420 KB |
Output is correct |
17 |
Correct |
0 ms |
99420 KB |
Output is correct |
18 |
Correct |
9 ms |
99420 KB |
Output is correct |
19 |
Correct |
6 ms |
99420 KB |
Output is correct |
20 |
Correct |
0 ms |
99420 KB |
Output is correct |
21 |
Correct |
9 ms |
99420 KB |
Output is correct |
22 |
Correct |
6 ms |
99420 KB |
Output is correct |
23 |
Correct |
6 ms |
99420 KB |
Output is correct |
24 |
Correct |
3 ms |
99420 KB |
Output is correct |
25 |
Correct |
9 ms |
99420 KB |
Output is correct |
26 |
Correct |
3 ms |
99420 KB |
Output is correct |
27 |
Correct |
3 ms |
99420 KB |
Output is correct |
28 |
Correct |
6 ms |
99420 KB |
Output is correct |
29 |
Correct |
6 ms |
99420 KB |
Output is correct |
30 |
Correct |
9 ms |
99420 KB |
Output is correct |
31 |
Correct |
3 ms |
99420 KB |
Output is correct |
32 |
Correct |
9 ms |
99420 KB |
Output is correct |
33 |
Correct |
3 ms |
99420 KB |
Output is correct |
34 |
Correct |
13 ms |
99420 KB |
Output is correct |
35 |
Correct |
9 ms |
99420 KB |
Output is correct |
36 |
Correct |
9 ms |
99420 KB |
Output is correct |
37 |
Correct |
3 ms |
99420 KB |
Output is correct |
38 |
Correct |
16 ms |
99420 KB |
Output is correct |
39 |
Correct |
13 ms |
99420 KB |
Output is correct |
40 |
Correct |
9 ms |
99420 KB |
Output is correct |
41 |
Correct |
9 ms |
99420 KB |
Output is correct |
42 |
Correct |
9 ms |
99420 KB |
Output is correct |
43 |
Correct |
6 ms |
99420 KB |
Output is correct |
44 |
Correct |
9 ms |
99420 KB |
Output is correct |
45 |
Correct |
9 ms |
99420 KB |
Output is correct |
46 |
Correct |
13 ms |
99420 KB |
Output is correct |
47 |
Correct |
13 ms |
99420 KB |
Output is correct |
48 |
Correct |
16 ms |
99420 KB |
Output is correct |
49 |
Correct |
19 ms |
99420 KB |
Output is correct |
50 |
Correct |
9 ms |
99420 KB |
Output is correct |
51 |
Correct |
9 ms |
99420 KB |
Output is correct |
52 |
Correct |
9 ms |
99420 KB |
Output is correct |
53 |
Correct |
13 ms |
99420 KB |
Output is correct |
54 |
Correct |
23 ms |
99420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
99420 KB |
Output is correct |
2 |
Correct |
3 ms |
99420 KB |
Output is correct |
3 |
Correct |
3 ms |
99420 KB |
Output is correct |
4 |
Correct |
13 ms |
99420 KB |
Output is correct |
5 |
Correct |
9 ms |
99420 KB |
Output is correct |
6 |
Correct |
16 ms |
99420 KB |
Output is correct |
7 |
Correct |
13 ms |
99420 KB |
Output is correct |
8 |
Correct |
9 ms |
99420 KB |
Output is correct |
9 |
Correct |
6 ms |
99420 KB |
Output is correct |
10 |
Correct |
3 ms |
99420 KB |
Output is correct |
11 |
Correct |
13 ms |
99420 KB |
Output is correct |
12 |
Correct |
0 ms |
99420 KB |
Output is correct |
13 |
Correct |
13 ms |
99420 KB |
Output is correct |
14 |
Correct |
9 ms |
99420 KB |
Output is correct |
15 |
Correct |
6 ms |
99420 KB |
Output is correct |
16 |
Correct |
13 ms |
99420 KB |
Output is correct |
17 |
Correct |
0 ms |
99420 KB |
Output is correct |
18 |
Correct |
9 ms |
99420 KB |
Output is correct |
19 |
Correct |
6 ms |
99420 KB |
Output is correct |
20 |
Correct |
0 ms |
99420 KB |
Output is correct |
21 |
Correct |
9 ms |
99420 KB |
Output is correct |
22 |
Correct |
6 ms |
99420 KB |
Output is correct |
23 |
Correct |
6 ms |
99420 KB |
Output is correct |
24 |
Correct |
3 ms |
99420 KB |
Output is correct |
25 |
Correct |
9 ms |
99420 KB |
Output is correct |
26 |
Correct |
3 ms |
99420 KB |
Output is correct |
27 |
Correct |
3 ms |
99420 KB |
Output is correct |
28 |
Correct |
6 ms |
99420 KB |
Output is correct |
29 |
Correct |
6 ms |
99420 KB |
Output is correct |
30 |
Correct |
9 ms |
99420 KB |
Output is correct |
31 |
Correct |
3 ms |
99420 KB |
Output is correct |
32 |
Correct |
9 ms |
99420 KB |
Output is correct |
33 |
Correct |
3 ms |
99420 KB |
Output is correct |
34 |
Correct |
13 ms |
99420 KB |
Output is correct |
35 |
Correct |
9 ms |
99420 KB |
Output is correct |
36 |
Correct |
9 ms |
99420 KB |
Output is correct |
37 |
Correct |
3 ms |
99420 KB |
Output is correct |
38 |
Correct |
16 ms |
99420 KB |
Output is correct |
39 |
Correct |
13 ms |
99420 KB |
Output is correct |
40 |
Correct |
9 ms |
99420 KB |
Output is correct |
41 |
Correct |
9 ms |
99420 KB |
Output is correct |
42 |
Correct |
9 ms |
99420 KB |
Output is correct |
43 |
Correct |
6 ms |
99420 KB |
Output is correct |
44 |
Correct |
9 ms |
99420 KB |
Output is correct |
45 |
Correct |
9 ms |
99420 KB |
Output is correct |
46 |
Correct |
13 ms |
99420 KB |
Output is correct |
47 |
Correct |
13 ms |
99420 KB |
Output is correct |
48 |
Correct |
16 ms |
99420 KB |
Output is correct |
49 |
Correct |
19 ms |
99420 KB |
Output is correct |
50 |
Correct |
9 ms |
99420 KB |
Output is correct |
51 |
Correct |
9 ms |
99420 KB |
Output is correct |
52 |
Correct |
9 ms |
99420 KB |
Output is correct |
53 |
Correct |
13 ms |
99420 KB |
Output is correct |
54 |
Correct |
23 ms |
99420 KB |
Output is correct |
55 |
Correct |
19 ms |
99420 KB |
Output is correct |
56 |
Correct |
63 ms |
99420 KB |
Output is correct |
57 |
Correct |
103 ms |
99420 KB |
Output is correct |
58 |
Correct |
126 ms |
99420 KB |
Output is correct |
59 |
Correct |
176 ms |
99420 KB |
Output is correct |
60 |
Correct |
213 ms |
99420 KB |
Output is correct |
61 |
Correct |
246 ms |
99420 KB |
Output is correct |
62 |
Correct |
276 ms |
99420 KB |
Output is correct |
63 |
Correct |
336 ms |
99420 KB |
Output is correct |
64 |
Correct |
339 ms |
99420 KB |
Output is correct |
65 |
Correct |
249 ms |
99420 KB |
Output is correct |
66 |
Correct |
259 ms |
99420 KB |
Output is correct |
67 |
Correct |
299 ms |
99420 KB |
Output is correct |
68 |
Correct |
263 ms |
99420 KB |
Output is correct |
69 |
Correct |
313 ms |
99420 KB |
Output is correct |
70 |
Correct |
303 ms |
99420 KB |
Output is correct |
71 |
Correct |
236 ms |
99420 KB |
Output is correct |
72 |
Correct |
243 ms |
99420 KB |
Output is correct |
73 |
Correct |
226 ms |
99420 KB |
Output is correct |
74 |
Correct |
233 ms |
99420 KB |
Output is correct |
75 |
Correct |
239 ms |
99420 KB |
Output is correct |
76 |
Correct |
236 ms |
99420 KB |
Output is correct |
77 |
Correct |
243 ms |
99420 KB |
Output is correct |
78 |
Correct |
243 ms |
99420 KB |
Output is correct |
79 |
Correct |
259 ms |
99420 KB |
Output is correct |