Submission #1022839

# Submission time Handle Problem Language Result Execution time Memory
1022839 2024-07-14T06:05:23 Z Sputnik123 Fireworks (APIO16_fireworks) C++17
100 / 100
128 ms 46620 KB
/*
 * Time Complexity: O((N+M) (log (N+M))^2 )
 */

#include<stdio.h>
#include<queue>
#define MAXN 300100
int n,m;
int p[MAXN];
int c[MAXN];
struct ndata{//contains data for subtree (y=f(x), where y is minimum cost when distance to all leaf node is x
	long long int a,b;//y=ax+b at large x
	std::priority_queue<long long int> *pq;//saves slope changing points, slope change by 1 at each element
	ndata operator+(ndata r){//merge two data by adding them
		ndata s;//result(merged data)
		s.a=a+r.a;
		s.b=b+r.b;
		if(pq->size()>r.pq->size()){//merge smaller priority queue to larger priority queue
			s.pq=pq;
			while(r.pq->size()!=0){
				s.pq->push(r.pq->top());
				r.pq->pop();
			}
		}
		else{
			s.pq=r.pq;
			while(pq->size()!=0){
				s.pq->push(pq->top());
				pq->pop();
			}
		}
		return s;
	}
};
ndata d[MAXN];
int main(){
	int i;
	scanf("%d%d",&n,&m);
	for(i=2;i<=n+m;i++){
		scanf("%d%d",&p[i],&c[i]);
	}
	for(i=n+m;i>0;i--){//initiallize
		d[i].a=0;
		d[i].b=0;
		d[i].pq=new std::priority_queue<long long>;
	}
	for(i=n+m;i>n;i--){//leaf nodes
		d[i].a=1;
		d[i].b=-c[i];
		d[i].pq->push(c[i]);//slope is -1 if x<c[i], 1 if x>c[i]
		d[i].pq->push(c[i]);//slope changes by 2
		d[p[i]]=d[p[i]]+d[i];//add the data to parent node
	}
	for(i=n;i>1;i--){
		//add edge to parent node
		while(d[i].a>1){//slope over 1 is useless because we can increase only one edge(edge toward parent node)
			d[i].a--;//slope decrease by 1
			d[i].b+=d[i].pq->top();//y=ax+b=(a-1)x+(b+x) at slope changing point
			d[i].pq->pop();
		}
		long long int ta=d[i].pq->top();//increase length of slope -1 part by c[i]
		d[i].pq->pop();
		long long int tb=d[i].pq->top();
		d[i].pq->pop();
		d[i].pq->push(tb+c[i]);//move location of slope 0, 1 part by c[i]
		d[i].pq->push(ta+c[i]);
		d[i].b-=c[i];//y is decreased by c[i] at sufficiently large x (slope 1 part)
		d[p[i]]=d[p[i]]+d[i];//add the data to parent node
	}
	while(d[1].a>0){//root node, y at slope 0 is the answer because it is minimum y
		d[1].a--;
		d[1].b+=d[1].pq->top();
		d[1].pq->pop();
	}
	printf("%lld\n",d[1].b);
	return 0;
}

Compilation message

fireworks.cpp: In function 'int main()':
fireworks.cpp:38:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |  scanf("%d%d",&n,&m);
      |  ~~~~~^~~~~~~~~~~~~~
fireworks.cpp:40:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |   scanf("%d%d",&p[i],&c[i]);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 2396 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 2 ms 604 KB Output is correct
36 Correct 1 ms 860 KB Output is correct
37 Correct 3 ms 860 KB Output is correct
38 Correct 3 ms 860 KB Output is correct
39 Correct 4 ms 860 KB Output is correct
40 Correct 1 ms 708 KB Output is correct
41 Correct 1 ms 604 KB Output is correct
42 Correct 1 ms 604 KB Output is correct
43 Correct 3 ms 860 KB Output is correct
44 Correct 4 ms 2908 KB Output is correct
45 Correct 2 ms 860 KB Output is correct
46 Correct 2 ms 1112 KB Output is correct
47 Correct 3 ms 1116 KB Output is correct
48 Correct 3 ms 860 KB Output is correct
49 Correct 3 ms 1112 KB Output is correct
50 Correct 2 ms 2908 KB Output is correct
51 Correct 2 ms 860 KB Output is correct
52 Correct 2 ms 860 KB Output is correct
53 Correct 2 ms 860 KB Output is correct
54 Correct 2 ms 1116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 2396 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 2 ms 604 KB Output is correct
36 Correct 1 ms 860 KB Output is correct
37 Correct 3 ms 860 KB Output is correct
38 Correct 3 ms 860 KB Output is correct
39 Correct 4 ms 860 KB Output is correct
40 Correct 1 ms 708 KB Output is correct
41 Correct 1 ms 604 KB Output is correct
42 Correct 1 ms 604 KB Output is correct
43 Correct 3 ms 860 KB Output is correct
44 Correct 4 ms 2908 KB Output is correct
45 Correct 2 ms 860 KB Output is correct
46 Correct 2 ms 1112 KB Output is correct
47 Correct 3 ms 1116 KB Output is correct
48 Correct 3 ms 860 KB Output is correct
49 Correct 3 ms 1112 KB Output is correct
50 Correct 2 ms 2908 KB Output is correct
51 Correct 2 ms 860 KB Output is correct
52 Correct 2 ms 860 KB Output is correct
53 Correct 2 ms 860 KB Output is correct
54 Correct 2 ms 1116 KB Output is correct
55 Correct 4 ms 1884 KB Output is correct
56 Correct 24 ms 6088 KB Output is correct
57 Correct 38 ms 10072 KB Output is correct
58 Correct 64 ms 14028 KB Output is correct
59 Correct 61 ms 17740 KB Output is correct
60 Correct 66 ms 21420 KB Output is correct
61 Correct 78 ms 24520 KB Output is correct
62 Correct 79 ms 26568 KB Output is correct
63 Correct 102 ms 31296 KB Output is correct
64 Correct 105 ms 32676 KB Output is correct
65 Correct 58 ms 23892 KB Output is correct
66 Correct 55 ms 23864 KB Output is correct
67 Correct 72 ms 23892 KB Output is correct
68 Correct 93 ms 30656 KB Output is correct
69 Correct 101 ms 31172 KB Output is correct
70 Correct 99 ms 31180 KB Output is correct
71 Correct 124 ms 46556 KB Output is correct
72 Correct 127 ms 46620 KB Output is correct
73 Correct 113 ms 43236 KB Output is correct
74 Correct 119 ms 43896 KB Output is correct
75 Correct 122 ms 42312 KB Output is correct
76 Correct 120 ms 42436 KB Output is correct
77 Correct 120 ms 41276 KB Output is correct
78 Correct 125 ms 41120 KB Output is correct
79 Correct 128 ms 40128 KB Output is correct