Submission #39869

# Submission time Handle Problem Language Result Execution time Memory
39869 2018-01-22T14:42:42 Z cheater2k Fireworks (APIO16_fireworks) C++14
100 / 100
282 ms 52460 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 300005;

struct slope {
	priority_queue <long long> pq;
	long long starting_value;
	long long starting_slope;
} S[N];

int n, m, p[N], a[N];
long long ans;

int main() {
	scanf("%d %d", &n, &m);
	for (int i = 2; i <= n + m; ++i) {
		scanf("%d %d", &p[i], &a[i]);
		if (i >= n + 1) { // leaf
			S[i].pq.push(a[i]); S[i].pq.push(a[i]); // -x + a and x - a
			S[i].starting_slope = -1;
			S[i].starting_value = a[i]; // -0 + a
		}
	}

	for (int i = n + m; i >= 2; --i) {
		if (i <= n) { // not leaf
			// fix slopes of S[i]
			long long last_slope = S[i].starting_slope + S[i].pq.size();
			while(last_slope > 1) {
				S[i].pq.pop(); --last_slope;
			}

			long long x1 = S[i].pq.top(); S[i].pq.pop(); // x-coordinate at slope 1
			long long x0 = S[i].pq.top(); S[i].pq.pop(); // x-coordinate at slope 0

			S[i].starting_value += a[i];
			S[i].pq.push(x0 + a[i]);
			S[i].pq.push(x1 + a[i]);
		}

		// update to p[i]
		int par = p[i];
		if (S[par].pq.size() < S[i].pq.size()) {
			swap(S[par], S[i]);
		}
		S[par].starting_value += S[i].starting_value;
		S[par].starting_slope += S[i].starting_slope;
		while(!S[i].pq.empty()) {
			long long x = S[i].pq.top(); S[i].pq.pop();
			S[par].pq.push(x);
		}
	}

	// root: 1
	long long starting_value = S[1].starting_value;
	long long starting_slope = S[1].starting_slope;

	vector<long long> pts;

	while(!S[1].pq.empty()) {
		pts.push_back(S[1].pq.top()); S[1].pq.pop();
	}
	pts.push_back(0);
	reverse(pts.begin(), pts.end());

	ans = starting_value;
	for (int i = 1; i < pts.size(); ++i) {
		starting_value += starting_slope * (pts[i] - pts[i-1]);
		++starting_slope;
		ans = min(ans, starting_value);
	}

	printf("%lld\n", ans);
}

Compilation message

fireworks.cpp: In function 'int main()':
fireworks.cpp:68:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 1; i < pts.size(); ++i) {
                    ^
fireworks.cpp:16:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &m);
                        ^
fireworks.cpp:18:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &p[i], &a[i]);
                               ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 18424 KB Output is correct
2 Correct 0 ms 18424 KB Output is correct
3 Correct 5 ms 18424 KB Output is correct
4 Correct 0 ms 18424 KB Output is correct
5 Correct 0 ms 18424 KB Output is correct
6 Correct 3 ms 18424 KB Output is correct
7 Correct 3 ms 18424 KB Output is correct
8 Correct 0 ms 18424 KB Output is correct
9 Correct 0 ms 18424 KB Output is correct
10 Correct 0 ms 18424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 18424 KB Output is correct
2 Correct 3 ms 18424 KB Output is correct
3 Correct 5 ms 18424 KB Output is correct
4 Correct 2 ms 18424 KB Output is correct
5 Correct 5 ms 18424 KB Output is correct
6 Correct 3 ms 18424 KB Output is correct
7 Correct 0 ms 18424 KB Output is correct
8 Correct 0 ms 18424 KB Output is correct
9 Correct 2 ms 18424 KB Output is correct
10 Correct 6 ms 18424 KB Output is correct
11 Correct 4 ms 18424 KB Output is correct
12 Correct 3 ms 18424 KB Output is correct
13 Correct 0 ms 18424 KB Output is correct
14 Correct 5 ms 18424 KB Output is correct
15 Correct 2 ms 18424 KB Output is correct
16 Correct 3 ms 18424 KB Output is correct
17 Correct 7 ms 18424 KB Output is correct
18 Correct 6 ms 18424 KB Output is correct
19 Correct 2 ms 18424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 18424 KB Output is correct
2 Correct 0 ms 18424 KB Output is correct
3 Correct 5 ms 18424 KB Output is correct
4 Correct 0 ms 18424 KB Output is correct
5 Correct 0 ms 18424 KB Output is correct
6 Correct 3 ms 18424 KB Output is correct
7 Correct 3 ms 18424 KB Output is correct
8 Correct 0 ms 18424 KB Output is correct
9 Correct 0 ms 18424 KB Output is correct
10 Correct 0 ms 18424 KB Output is correct
11 Correct 3 ms 18424 KB Output is correct
12 Correct 3 ms 18424 KB Output is correct
13 Correct 5 ms 18424 KB Output is correct
14 Correct 2 ms 18424 KB Output is correct
15 Correct 5 ms 18424 KB Output is correct
16 Correct 3 ms 18424 KB Output is correct
17 Correct 0 ms 18424 KB Output is correct
18 Correct 0 ms 18424 KB Output is correct
19 Correct 2 ms 18424 KB Output is correct
20 Correct 6 ms 18424 KB Output is correct
21 Correct 4 ms 18424 KB Output is correct
22 Correct 3 ms 18424 KB Output is correct
23 Correct 0 ms 18424 KB Output is correct
24 Correct 5 ms 18424 KB Output is correct
25 Correct 2 ms 18424 KB Output is correct
26 Correct 3 ms 18424 KB Output is correct
27 Correct 7 ms 18424 KB Output is correct
28 Correct 6 ms 18424 KB Output is correct
29 Correct 2 ms 18424 KB Output is correct
30 Correct 0 ms 18424 KB Output is correct
31 Correct 2 ms 18424 KB Output is correct
32 Correct 2 ms 18556 KB Output is correct
33 Correct 4 ms 18556 KB Output is correct
34 Correct 3 ms 18556 KB Output is correct
35 Correct 0 ms 18556 KB Output is correct
36 Correct 3 ms 18556 KB Output is correct
37 Correct 6 ms 18556 KB Output is correct
38 Correct 3 ms 18696 KB Output is correct
39 Correct 2 ms 18696 KB Output is correct
40 Correct 3 ms 18424 KB Output is correct
41 Correct 2 ms 18424 KB Output is correct
42 Correct 7 ms 18424 KB Output is correct
43 Correct 3 ms 18556 KB Output is correct
44 Correct 8 ms 18716 KB Output is correct
45 Correct 6 ms 18716 KB Output is correct
46 Correct 2 ms 18992 KB Output is correct
47 Correct 10 ms 18980 KB Output is correct
48 Correct 5 ms 18832 KB Output is correct
49 Correct 5 ms 18864 KB Output is correct
50 Correct 5 ms 18856 KB Output is correct
51 Correct 4 ms 18856 KB Output is correct
52 Correct 7 ms 18844 KB Output is correct
53 Correct 5 ms 18848 KB Output is correct
54 Correct 4 ms 18920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 18424 KB Output is correct
2 Correct 0 ms 18424 KB Output is correct
3 Correct 5 ms 18424 KB Output is correct
4 Correct 0 ms 18424 KB Output is correct
5 Correct 0 ms 18424 KB Output is correct
6 Correct 3 ms 18424 KB Output is correct
7 Correct 3 ms 18424 KB Output is correct
8 Correct 0 ms 18424 KB Output is correct
9 Correct 0 ms 18424 KB Output is correct
10 Correct 0 ms 18424 KB Output is correct
11 Correct 3 ms 18424 KB Output is correct
12 Correct 3 ms 18424 KB Output is correct
13 Correct 5 ms 18424 KB Output is correct
14 Correct 2 ms 18424 KB Output is correct
15 Correct 5 ms 18424 KB Output is correct
16 Correct 3 ms 18424 KB Output is correct
17 Correct 0 ms 18424 KB Output is correct
18 Correct 0 ms 18424 KB Output is correct
19 Correct 2 ms 18424 KB Output is correct
20 Correct 6 ms 18424 KB Output is correct
21 Correct 4 ms 18424 KB Output is correct
22 Correct 3 ms 18424 KB Output is correct
23 Correct 0 ms 18424 KB Output is correct
24 Correct 5 ms 18424 KB Output is correct
25 Correct 2 ms 18424 KB Output is correct
26 Correct 3 ms 18424 KB Output is correct
27 Correct 7 ms 18424 KB Output is correct
28 Correct 6 ms 18424 KB Output is correct
29 Correct 2 ms 18424 KB Output is correct
30 Correct 0 ms 18424 KB Output is correct
31 Correct 2 ms 18424 KB Output is correct
32 Correct 2 ms 18556 KB Output is correct
33 Correct 4 ms 18556 KB Output is correct
34 Correct 3 ms 18556 KB Output is correct
35 Correct 0 ms 18556 KB Output is correct
36 Correct 3 ms 18556 KB Output is correct
37 Correct 6 ms 18556 KB Output is correct
38 Correct 3 ms 18696 KB Output is correct
39 Correct 2 ms 18696 KB Output is correct
40 Correct 3 ms 18424 KB Output is correct
41 Correct 2 ms 18424 KB Output is correct
42 Correct 7 ms 18424 KB Output is correct
43 Correct 3 ms 18556 KB Output is correct
44 Correct 8 ms 18716 KB Output is correct
45 Correct 6 ms 18716 KB Output is correct
46 Correct 2 ms 18992 KB Output is correct
47 Correct 10 ms 18980 KB Output is correct
48 Correct 5 ms 18832 KB Output is correct
49 Correct 5 ms 18864 KB Output is correct
50 Correct 5 ms 18856 KB Output is correct
51 Correct 4 ms 18856 KB Output is correct
52 Correct 7 ms 18844 KB Output is correct
53 Correct 5 ms 18848 KB Output is correct
54 Correct 4 ms 18920 KB Output is correct
55 Correct 6 ms 18956 KB Output is correct
56 Correct 43 ms 20888 KB Output is correct
57 Correct 62 ms 22052 KB Output is correct
58 Correct 63 ms 23620 KB Output is correct
59 Correct 93 ms 24844 KB Output is correct
60 Correct 128 ms 26112 KB Output is correct
61 Correct 135 ms 28368 KB Output is correct
62 Correct 171 ms 29224 KB Output is correct
63 Correct 229 ms 30852 KB Output is correct
64 Correct 212 ms 30916 KB Output is correct
65 Correct 82 ms 18424 KB Output is correct
66 Correct 89 ms 18424 KB Output is correct
67 Correct 98 ms 18556 KB Output is correct
68 Correct 179 ms 29340 KB Output is correct
69 Correct 210 ms 30500 KB Output is correct
70 Correct 197 ms 30492 KB Output is correct
71 Correct 282 ms 50472 KB Output is correct
72 Correct 247 ms 50308 KB Output is correct
73 Correct 220 ms 46492 KB Output is correct
74 Correct 250 ms 47176 KB Output is correct
75 Correct 236 ms 46996 KB Output is correct
76 Correct 242 ms 47072 KB Output is correct
77 Correct 258 ms 46888 KB Output is correct
78 Correct 259 ms 45844 KB Output is correct
79 Correct 251 ms 52460 KB Output is correct