Submission #23502

# Submission time Handle Problem Language Result Execution time Memory
23502 2017-05-11T13:14:01 Z natsukagami Fireworks (APIO16_fireworks) C++14
100 / 100
309 ms 58860 KB
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;

typedef long long ll;
typedef pair<ll, int> ii;
const int MAXN = 3e5 + 5;

struct node {
	int total;
	ll off;
	priority_queue<ii> q;
	node() { total = off = 0; }
} *V[MAXN];

void merge(node *&a, node *&b) {
	// merge a <- b
	if (a->q.size() < b->q.size()) swap(a, b); // small to large
	a->total += b->total;
	a->off += b->off;
	while (b->q.size()) { a->q.push(b->q.top()); b->q.pop(); }
}

void opt(node *a, int k) {
	// optimize with k
	a->off += k;
	ll mx, mn;
	while (a->total > 0) {
		mx = mn = a->q.top().x;
		a->total -= a->q.top().y;
		a->q.pop(); 
	}
	while (a->total == 0) {
		a->total -= a->q.top().y;
		mn = a->q.top().x; a->q.pop(); 
	}
	// cout << "ck " << mn << ' ' << mx << endl;
	while (a->q.size() && a->q.top().x >= mn) {
		a->total -= a->q.top().y;
		a->q.pop(); 
	}
	// raise total to -1
	a->q.push(ii(mn, -1 - a->total)); 
	// push 0 in
	a->q.push(ii(mn + k, 1)); 
	// push 1 in
	a->q.push(ii(mx + k, 1));
	// fix total
	a->total = 1;
}

int N, M;
int par[MAXN], edge[MAXN], deg[MAXN];

// random_device rd;

int main() {
	ios_base::sync_with_stdio(false);
	// N = M = 1e5;
	cin >> N >> M;
	for (int i = 2; i <= N + M; ++i) {
		// par[i] = rd()%(i-1) + 1;
		// edge[i] = rd()%int(1e9) + 1;
		cin >> par[i] >> edge[i]; ++deg[par[i]];
	}
	for (int i = 1; i <= N + M; ++i) V[i] = new node();
	for (int i = N + M; i >= 1; --i) {
		// cout << i << endl;
		if (!deg[i]) {
			// i is leaf
			V[i]->total = 1; V[i]->off = edge[i];
			V[i]->q.push(ii(edge[i], 2));
		} else {
			opt(V[i], edge[i]);
		}
		// cout << "v = " << i << ":" << endl;
		// cout << V[i]->off << ' ' << V[i]->total << endl << "qq:" << endl;
		// auto qq = V[i]->q;
		// while (qq.size()) {
		// 	cout << qq.top().x << ' ' << qq.top().y << endl;
		// 	qq.pop();
		// }
		if (i > 1) merge(V[par[i]], V[i]);
	}
	vector<ii> P; 
	while (V[1]->q.size()) { 
		P.push_back(V[1]->q.top()); 
		V[1]->total -= V[1]->q.top().y; 
		V[1]->q.pop(); 
	}
	reverse(P.begin(), P.end());
	ll ans = V[1]->off, last = 0, cur = V[1]->off;
	for (auto i: P) {
		// cout << i.x << ' ' << i.y << ' ' << V[1]->total << ' ' << cur << endl;
		cur += (i.x - last) * V[1]->total;
		ans = min(ans, cur);
		V[1]->total += i.y;
		last = i.x;
	}
	cout << ans << endl;
}

Compilation message

fireworks.cpp: In function 'void opt(node*, int)':
fireworks.cpp:48:18: warning: 'mx' may be used uninitialized in this function [-Wmaybe-uninitialized]
  a->q.push(ii(mx + k, 1));
                  ^
fireworks.cpp:39:21: warning: 'mn' may be used uninitialized in this function [-Wmaybe-uninitialized]
  while (a->q.size() && a->q.top().x >= mn) {
                     ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 8040 KB Output is correct
2 Correct 0 ms 8040 KB Output is correct
3 Correct 0 ms 8040 KB Output is correct
4 Correct 0 ms 8040 KB Output is correct
5 Correct 0 ms 8040 KB Output is correct
6 Correct 0 ms 8040 KB Output is correct
7 Correct 0 ms 8040 KB Output is correct
8 Correct 0 ms 8040 KB Output is correct
9 Correct 0 ms 8040 KB Output is correct
10 Correct 0 ms 8040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 8040 KB Output is correct
2 Correct 0 ms 8040 KB Output is correct
3 Correct 0 ms 8040 KB Output is correct
4 Correct 0 ms 8040 KB Output is correct
5 Correct 0 ms 8040 KB Output is correct
6 Correct 0 ms 8040 KB Output is correct
7 Correct 0 ms 8040 KB Output is correct
8 Correct 0 ms 8040 KB Output is correct
9 Correct 0 ms 8040 KB Output is correct
10 Correct 0 ms 8040 KB Output is correct
11 Correct 0 ms 8040 KB Output is correct
12 Correct 0 ms 8040 KB Output is correct
13 Correct 0 ms 8040 KB Output is correct
14 Correct 0 ms 8040 KB Output is correct
15 Correct 0 ms 8040 KB Output is correct
16 Correct 0 ms 8040 KB Output is correct
17 Correct 0 ms 8040 KB Output is correct
18 Correct 0 ms 8040 KB Output is correct
19 Correct 0 ms 8040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 8040 KB Output is correct
2 Correct 0 ms 8040 KB Output is correct
3 Correct 0 ms 8040 KB Output is correct
4 Correct 0 ms 8040 KB Output is correct
5 Correct 0 ms 8040 KB Output is correct
6 Correct 0 ms 8040 KB Output is correct
7 Correct 0 ms 8040 KB Output is correct
8 Correct 0 ms 8040 KB Output is correct
9 Correct 0 ms 8040 KB Output is correct
10 Correct 0 ms 8040 KB Output is correct
11 Correct 0 ms 8040 KB Output is correct
12 Correct 0 ms 8040 KB Output is correct
13 Correct 0 ms 8040 KB Output is correct
14 Correct 0 ms 8040 KB Output is correct
15 Correct 0 ms 8040 KB Output is correct
16 Correct 0 ms 8040 KB Output is correct
17 Correct 0 ms 8040 KB Output is correct
18 Correct 0 ms 8040 KB Output is correct
19 Correct 0 ms 8040 KB Output is correct
20 Correct 0 ms 8040 KB Output is correct
21 Correct 0 ms 8040 KB Output is correct
22 Correct 0 ms 8040 KB Output is correct
23 Correct 0 ms 8040 KB Output is correct
24 Correct 0 ms 8040 KB Output is correct
25 Correct 0 ms 8040 KB Output is correct
26 Correct 0 ms 8040 KB Output is correct
27 Correct 0 ms 8040 KB Output is correct
28 Correct 0 ms 8040 KB Output is correct
29 Correct 0 ms 8040 KB Output is correct
30 Correct 0 ms 8040 KB Output is correct
31 Correct 0 ms 8172 KB Output is correct
32 Correct 0 ms 8304 KB Output is correct
33 Correct 0 ms 8440 KB Output is correct
34 Correct 0 ms 8436 KB Output is correct
35 Correct 0 ms 8568 KB Output is correct
36 Correct 3 ms 8572 KB Output is correct
37 Correct 0 ms 8860 KB Output is correct
38 Correct 3 ms 8868 KB Output is correct
39 Correct 6 ms 8892 KB Output is correct
40 Correct 3 ms 8648 KB Output is correct
41 Correct 3 ms 8580 KB Output is correct
42 Correct 3 ms 8776 KB Output is correct
43 Correct 3 ms 8436 KB Output is correct
44 Correct 3 ms 8592 KB Output is correct
45 Correct 3 ms 8592 KB Output is correct
46 Correct 3 ms 8872 KB Output is correct
47 Correct 3 ms 8860 KB Output is correct
48 Correct 3 ms 8736 KB Output is correct
49 Correct 3 ms 8740 KB Output is correct
50 Correct 3 ms 8568 KB Output is correct
51 Correct 3 ms 8624 KB Output is correct
52 Correct 3 ms 8768 KB Output is correct
53 Correct 0 ms 8772 KB Output is correct
54 Correct 0 ms 8740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 8040 KB Output is correct
2 Correct 0 ms 8040 KB Output is correct
3 Correct 0 ms 8040 KB Output is correct
4 Correct 0 ms 8040 KB Output is correct
5 Correct 0 ms 8040 KB Output is correct
6 Correct 0 ms 8040 KB Output is correct
7 Correct 0 ms 8040 KB Output is correct
8 Correct 0 ms 8040 KB Output is correct
9 Correct 0 ms 8040 KB Output is correct
10 Correct 0 ms 8040 KB Output is correct
11 Correct 0 ms 8040 KB Output is correct
12 Correct 0 ms 8040 KB Output is correct
13 Correct 0 ms 8040 KB Output is correct
14 Correct 0 ms 8040 KB Output is correct
15 Correct 0 ms 8040 KB Output is correct
16 Correct 0 ms 8040 KB Output is correct
17 Correct 0 ms 8040 KB Output is correct
18 Correct 0 ms 8040 KB Output is correct
19 Correct 0 ms 8040 KB Output is correct
20 Correct 0 ms 8040 KB Output is correct
21 Correct 0 ms 8040 KB Output is correct
22 Correct 0 ms 8040 KB Output is correct
23 Correct 0 ms 8040 KB Output is correct
24 Correct 0 ms 8040 KB Output is correct
25 Correct 0 ms 8040 KB Output is correct
26 Correct 0 ms 8040 KB Output is correct
27 Correct 0 ms 8040 KB Output is correct
28 Correct 0 ms 8040 KB Output is correct
29 Correct 0 ms 8040 KB Output is correct
30 Correct 0 ms 8040 KB Output is correct
31 Correct 0 ms 8172 KB Output is correct
32 Correct 0 ms 8304 KB Output is correct
33 Correct 0 ms 8440 KB Output is correct
34 Correct 0 ms 8436 KB Output is correct
35 Correct 0 ms 8568 KB Output is correct
36 Correct 3 ms 8572 KB Output is correct
37 Correct 0 ms 8860 KB Output is correct
38 Correct 3 ms 8868 KB Output is correct
39 Correct 6 ms 8892 KB Output is correct
40 Correct 3 ms 8648 KB Output is correct
41 Correct 3 ms 8580 KB Output is correct
42 Correct 3 ms 8776 KB Output is correct
43 Correct 3 ms 8436 KB Output is correct
44 Correct 3 ms 8592 KB Output is correct
45 Correct 3 ms 8592 KB Output is correct
46 Correct 3 ms 8872 KB Output is correct
47 Correct 3 ms 8860 KB Output is correct
48 Correct 3 ms 8736 KB Output is correct
49 Correct 3 ms 8740 KB Output is correct
50 Correct 3 ms 8568 KB Output is correct
51 Correct 3 ms 8624 KB Output is correct
52 Correct 3 ms 8768 KB Output is correct
53 Correct 0 ms 8772 KB Output is correct
54 Correct 0 ms 8740 KB Output is correct
55 Correct 6 ms 10016 KB Output is correct
56 Correct 29 ms 15964 KB Output is correct
57 Correct 89 ms 22312 KB Output is correct
58 Correct 103 ms 25684 KB Output is correct
59 Correct 133 ms 33144 KB Output is correct
60 Correct 176 ms 38436 KB Output is correct
61 Correct 216 ms 41568 KB Output is correct
62 Correct 179 ms 44748 KB Output is correct
63 Correct 309 ms 51816 KB Output is correct
64 Correct 219 ms 52228 KB Output is correct
65 Correct 236 ms 51408 KB Output is correct
66 Correct 223 ms 39120 KB Output is correct
67 Correct 253 ms 50232 KB Output is correct
68 Correct 143 ms 37660 KB Output is correct
69 Correct 186 ms 39756 KB Output is correct
70 Correct 193 ms 39760 KB Output is correct
71 Correct 196 ms 58860 KB Output is correct
72 Correct 189 ms 58716 KB Output is correct
73 Correct 203 ms 54696 KB Output is correct
74 Correct 156 ms 55396 KB Output is correct
75 Correct 163 ms 55184 KB Output is correct
76 Correct 183 ms 55304 KB Output is correct
77 Correct 179 ms 54744 KB Output is correct
78 Correct 189 ms 53712 KB Output is correct
79 Correct 183 ms 52584 KB Output is correct