Submission #40845

# Submission time Handle Problem Language Result Execution time Memory
40845 2018-02-09T10:04:00 Z ssnsarang2023 Fireworks (APIO16_fireworks) C++14
100 / 100
245 ms 132484 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;

#define SZ(x) ((int)x.size())

const int N = (int)3e5+5;

struct data {
	ll a, b;
	priority_queue<ll> *pq;
} a[N];

data merge(const data &x, const data &y) {
	data re;
	re.a = x.a + y.a;
	re.b = x.b + y.b;
	if (x.pq->size() > y.pq->size()) {
		re.pq = x.pq;
		while (y.pq->size()) {
			re.pq->push(y.pq->top());
			y.pq->pop();
		}
	} else {
		re.pq = y.pq;
		while (x.pq->size()) {
			re.pq->push(x.pq->top());
			x.pq->pop();
		}
	}
	return re;
}

int n, m, p[N];
ll c[N];

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 2; i <= n+m; ++i) scanf("%d%lld", &p[i], &c[i]);
	for (int i = 1; i <= n+m; ++i) {
		a[i].pq = new priority_queue<ll>();
		a[i].a = a[i].b = 0;
	}
	for (int i = n+m; i >= n+1; --i) {
		a[i].a = 1;
		a[i].b = -c[i];
		a[i].pq->push(c[i]);
		a[i].pq->push(c[i]);
		a[p[i]] = merge(a[p[i]], a[i]);
	}
	for (int i = n; i >= 2; --i) {
		while (a[i].a > 1) {
			--a[i].a;
			a[i].b += a[i].pq->top();
			a[i].pq->pop();
		}
		ll x = a[i].pq->top();
		a[i].pq->pop();
		ll y = a[i].pq->top();
		a[i].pq->pop();
		a[i].pq->push(x + c[i]);
		a[i].pq->push(y + c[i]);
		a[i].b -= a[i].a*c[i];
		a[p[i]] = merge(a[p[i]], a[i]);
	}
	while (a[1].a > 0) {
		--a[1].a;
		a[1].b += a[1].pq->top();
		a[1].pq->pop();
	}
	printf("%lld", a[1].b);
	return 0;
}

Compilation message

fireworks.cpp: In function 'int main()':
fireworks.cpp:42:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &n, &m);
                       ^
fireworks.cpp:43:62: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for (int i = 2; i <= n+m; ++i) scanf("%d%lld", &p[i], &c[i]);
                                                              ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 488 KB Output is correct
3 Correct 2 ms 672 KB Output is correct
4 Correct 2 ms 672 KB Output is correct
5 Correct 2 ms 712 KB Output is correct
6 Correct 2 ms 712 KB Output is correct
7 Correct 2 ms 724 KB Output is correct
8 Correct 2 ms 728 KB Output is correct
9 Correct 1 ms 776 KB Output is correct
10 Correct 2 ms 780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1032 KB Output is correct
2 Correct 2 ms 1032 KB Output is correct
3 Correct 1 ms 1032 KB Output is correct
4 Correct 2 ms 1032 KB Output is correct
5 Correct 2 ms 1032 KB Output is correct
6 Correct 2 ms 1032 KB Output is correct
7 Correct 2 ms 1036 KB Output is correct
8 Correct 2 ms 1040 KB Output is correct
9 Correct 2 ms 1044 KB Output is correct
10 Correct 2 ms 1052 KB Output is correct
11 Correct 2 ms 1056 KB Output is correct
12 Correct 2 ms 1056 KB Output is correct
13 Correct 2 ms 1064 KB Output is correct
14 Correct 2 ms 1068 KB Output is correct
15 Correct 2 ms 1072 KB Output is correct
16 Correct 2 ms 1140 KB Output is correct
17 Correct 2 ms 1144 KB Output is correct
18 Correct 2 ms 1144 KB Output is correct
19 Correct 2 ms 1144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 488 KB Output is correct
3 Correct 2 ms 672 KB Output is correct
4 Correct 2 ms 672 KB Output is correct
5 Correct 2 ms 712 KB Output is correct
6 Correct 2 ms 712 KB Output is correct
7 Correct 2 ms 724 KB Output is correct
8 Correct 2 ms 728 KB Output is correct
9 Correct 1 ms 776 KB Output is correct
10 Correct 2 ms 780 KB Output is correct
11 Correct 2 ms 1032 KB Output is correct
12 Correct 2 ms 1032 KB Output is correct
13 Correct 1 ms 1032 KB Output is correct
14 Correct 2 ms 1032 KB Output is correct
15 Correct 2 ms 1032 KB Output is correct
16 Correct 2 ms 1032 KB Output is correct
17 Correct 2 ms 1036 KB Output is correct
18 Correct 2 ms 1040 KB Output is correct
19 Correct 2 ms 1044 KB Output is correct
20 Correct 2 ms 1052 KB Output is correct
21 Correct 2 ms 1056 KB Output is correct
22 Correct 2 ms 1056 KB Output is correct
23 Correct 2 ms 1064 KB Output is correct
24 Correct 2 ms 1068 KB Output is correct
25 Correct 2 ms 1072 KB Output is correct
26 Correct 2 ms 1140 KB Output is correct
27 Correct 2 ms 1144 KB Output is correct
28 Correct 2 ms 1144 KB Output is correct
29 Correct 2 ms 1144 KB Output is correct
30 Correct 2 ms 1144 KB Output is correct
31 Correct 2 ms 1228 KB Output is correct
32 Correct 2 ms 1260 KB Output is correct
33 Correct 3 ms 1412 KB Output is correct
34 Correct 3 ms 1588 KB Output is correct
35 Correct 3 ms 1632 KB Output is correct
36 Correct 4 ms 1812 KB Output is correct
37 Correct 4 ms 1888 KB Output is correct
38 Correct 6 ms 2096 KB Output is correct
39 Correct 5 ms 2284 KB Output is correct
40 Correct 4 ms 2284 KB Output is correct
41 Correct 3 ms 2284 KB Output is correct
42 Correct 3 ms 2284 KB Output is correct
43 Correct 4 ms 2452 KB Output is correct
44 Correct 5 ms 2544 KB Output is correct
45 Correct 5 ms 2600 KB Output is correct
46 Correct 5 ms 2812 KB Output is correct
47 Correct 5 ms 2876 KB Output is correct
48 Correct 4 ms 2876 KB Output is correct
49 Correct 5 ms 2996 KB Output is correct
50 Correct 5 ms 2996 KB Output is correct
51 Correct 5 ms 2996 KB Output is correct
52 Correct 5 ms 3048 KB Output is correct
53 Correct 4 ms 3288 KB Output is correct
54 Correct 5 ms 3288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 488 KB Output is correct
3 Correct 2 ms 672 KB Output is correct
4 Correct 2 ms 672 KB Output is correct
5 Correct 2 ms 712 KB Output is correct
6 Correct 2 ms 712 KB Output is correct
7 Correct 2 ms 724 KB Output is correct
8 Correct 2 ms 728 KB Output is correct
9 Correct 1 ms 776 KB Output is correct
10 Correct 2 ms 780 KB Output is correct
11 Correct 2 ms 1032 KB Output is correct
12 Correct 2 ms 1032 KB Output is correct
13 Correct 1 ms 1032 KB Output is correct
14 Correct 2 ms 1032 KB Output is correct
15 Correct 2 ms 1032 KB Output is correct
16 Correct 2 ms 1032 KB Output is correct
17 Correct 2 ms 1036 KB Output is correct
18 Correct 2 ms 1040 KB Output is correct
19 Correct 2 ms 1044 KB Output is correct
20 Correct 2 ms 1052 KB Output is correct
21 Correct 2 ms 1056 KB Output is correct
22 Correct 2 ms 1056 KB Output is correct
23 Correct 2 ms 1064 KB Output is correct
24 Correct 2 ms 1068 KB Output is correct
25 Correct 2 ms 1072 KB Output is correct
26 Correct 2 ms 1140 KB Output is correct
27 Correct 2 ms 1144 KB Output is correct
28 Correct 2 ms 1144 KB Output is correct
29 Correct 2 ms 1144 KB Output is correct
30 Correct 2 ms 1144 KB Output is correct
31 Correct 2 ms 1228 KB Output is correct
32 Correct 2 ms 1260 KB Output is correct
33 Correct 3 ms 1412 KB Output is correct
34 Correct 3 ms 1588 KB Output is correct
35 Correct 3 ms 1632 KB Output is correct
36 Correct 4 ms 1812 KB Output is correct
37 Correct 4 ms 1888 KB Output is correct
38 Correct 6 ms 2096 KB Output is correct
39 Correct 5 ms 2284 KB Output is correct
40 Correct 4 ms 2284 KB Output is correct
41 Correct 3 ms 2284 KB Output is correct
42 Correct 3 ms 2284 KB Output is correct
43 Correct 4 ms 2452 KB Output is correct
44 Correct 5 ms 2544 KB Output is correct
45 Correct 5 ms 2600 KB Output is correct
46 Correct 5 ms 2812 KB Output is correct
47 Correct 5 ms 2876 KB Output is correct
48 Correct 4 ms 2876 KB Output is correct
49 Correct 5 ms 2996 KB Output is correct
50 Correct 5 ms 2996 KB Output is correct
51 Correct 5 ms 2996 KB Output is correct
52 Correct 5 ms 3048 KB Output is correct
53 Correct 4 ms 3288 KB Output is correct
54 Correct 5 ms 3288 KB Output is correct
55 Correct 10 ms 4244 KB Output is correct
56 Correct 33 ms 9388 KB Output is correct
57 Correct 62 ms 14904 KB Output is correct
58 Correct 78 ms 19260 KB Output is correct
59 Correct 103 ms 25620 KB Output is correct
60 Correct 133 ms 32520 KB Output is correct
61 Correct 155 ms 38824 KB Output is correct
62 Correct 177 ms 44800 KB Output is correct
63 Correct 223 ms 54016 KB Output is correct
64 Correct 213 ms 59800 KB Output is correct
65 Correct 119 ms 59800 KB Output is correct
66 Correct 109 ms 61088 KB Output is correct
67 Correct 146 ms 66072 KB Output is correct
68 Correct 184 ms 77736 KB Output is correct
69 Correct 231 ms 83164 KB Output is correct
70 Correct 210 ms 87852 KB Output is correct
71 Correct 244 ms 107232 KB Output is correct
72 Correct 245 ms 111800 KB Output is correct
73 Correct 202 ms 112232 KB Output is correct
74 Correct 217 ms 116924 KB Output is correct
75 Correct 215 ms 119420 KB Output is correct
76 Correct 222 ms 123092 KB Output is correct
77 Correct 231 ms 126136 KB Output is correct
78 Correct 233 ms 129704 KB Output is correct
79 Correct 225 ms 132484 KB Output is correct