Submission #593985

# Submission time Handle Problem Language Result Execution time Memory
593985 2022-07-11T19:53:07 Z penguinhacker Fireworks (APIO16_fireworks) C++17
100 / 100
241 ms 73272 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ar array

const int mxN=3e5;
int n, m;
vector<ar<int, 2>> adj[mxN];
ll yi[mxN], ys[mxN]; // y intercept and y intercept slope
priority_queue<ll> pq[mxN];

void dfs(int u=0) {
	for (auto [v, w] : adj[u]) {
		if (adj[v].empty()) {
			yi[v]=w, ys[v]=-1;
			pq[v].push(w);
			pq[v].push(w);
		} else {
			dfs(v);
			// shift container by w
			ll last=-1;
			while(ys[v]+(int)pq[v].size()>0) {
				last=pq[v].top();
				pq[v].pop();
			}
			assert(last!=-1&&pq[v].size());
			ll first=pq[v].top();
			pq[v].pop();
			yi[v]+=w;
			pq[v].push(first+w);
			pq[v].push(last+w);
		}
		yi[u]+=yi[v];
		ys[u]+=ys[v];
		if (pq[v].size()>pq[u].size())
			swap(pq[u], pq[v]);
		while(pq[v].size()) {
			pq[u].push(pq[v].top());
			pq[v].pop();
		}
	}
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> m;
	for (int i=1; i<n+m; ++i) {
		int p, c;
		cin >> p >> c, --p;
		adj[p].push_back({i, c});
	}
	dfs();
	ll ans=yi[0], slope=ys[0];
	vector<ll> changes;
	while(pq[0].size()) {
		changes.push_back(pq[0].top());
		pq[0].pop();
	}
	reverse(changes.begin(), changes.end());
	ll last=0;
	for (int i=0; slope<0; ++i, ++slope) {
		assert(i<changes.size());
		ans+=(changes[i]-last)*slope;
		last=changes[i];
	}
	cout << ans;
	return 0;
}

Compilation message

In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from fireworks.cpp:1:
fireworks.cpp: In function 'int main()':
fireworks.cpp:64:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int, std::allocator<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |   assert(i<changes.size());
      |          ~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 16724 KB Output is correct
2 Correct 9 ms 16756 KB Output is correct
3 Correct 8 ms 16724 KB Output is correct
4 Correct 10 ms 16724 KB Output is correct
5 Correct 9 ms 16852 KB Output is correct
6 Correct 8 ms 16724 KB Output is correct
7 Correct 9 ms 16696 KB Output is correct
8 Correct 10 ms 16668 KB Output is correct
9 Correct 9 ms 16676 KB Output is correct
10 Correct 8 ms 16788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 16980 KB Output is correct
2 Correct 8 ms 16724 KB Output is correct
3 Correct 8 ms 16724 KB Output is correct
4 Correct 10 ms 16668 KB Output is correct
5 Correct 11 ms 16732 KB Output is correct
6 Correct 9 ms 16724 KB Output is correct
7 Correct 9 ms 16724 KB Output is correct
8 Correct 11 ms 16788 KB Output is correct
9 Correct 9 ms 16724 KB Output is correct
10 Correct 9 ms 16724 KB Output is correct
11 Correct 10 ms 16792 KB Output is correct
12 Correct 9 ms 16724 KB Output is correct
13 Correct 9 ms 16788 KB Output is correct
14 Correct 9 ms 16764 KB Output is correct
15 Correct 11 ms 16684 KB Output is correct
16 Correct 11 ms 16724 KB Output is correct
17 Correct 10 ms 16724 KB Output is correct
18 Correct 9 ms 16716 KB Output is correct
19 Correct 9 ms 16744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 16724 KB Output is correct
2 Correct 9 ms 16756 KB Output is correct
3 Correct 8 ms 16724 KB Output is correct
4 Correct 10 ms 16724 KB Output is correct
5 Correct 9 ms 16852 KB Output is correct
6 Correct 8 ms 16724 KB Output is correct
7 Correct 9 ms 16696 KB Output is correct
8 Correct 10 ms 16668 KB Output is correct
9 Correct 9 ms 16676 KB Output is correct
10 Correct 8 ms 16788 KB Output is correct
11 Correct 10 ms 16980 KB Output is correct
12 Correct 8 ms 16724 KB Output is correct
13 Correct 8 ms 16724 KB Output is correct
14 Correct 10 ms 16668 KB Output is correct
15 Correct 11 ms 16732 KB Output is correct
16 Correct 9 ms 16724 KB Output is correct
17 Correct 9 ms 16724 KB Output is correct
18 Correct 11 ms 16788 KB Output is correct
19 Correct 9 ms 16724 KB Output is correct
20 Correct 9 ms 16724 KB Output is correct
21 Correct 10 ms 16792 KB Output is correct
22 Correct 9 ms 16724 KB Output is correct
23 Correct 9 ms 16788 KB Output is correct
24 Correct 9 ms 16764 KB Output is correct
25 Correct 11 ms 16684 KB Output is correct
26 Correct 11 ms 16724 KB Output is correct
27 Correct 10 ms 16724 KB Output is correct
28 Correct 9 ms 16716 KB Output is correct
29 Correct 9 ms 16744 KB Output is correct
30 Correct 9 ms 16808 KB Output is correct
31 Correct 10 ms 16852 KB Output is correct
32 Correct 12 ms 16852 KB Output is correct
33 Correct 12 ms 16936 KB Output is correct
34 Correct 12 ms 16980 KB Output is correct
35 Correct 13 ms 16980 KB Output is correct
36 Correct 11 ms 17108 KB Output is correct
37 Correct 11 ms 17108 KB Output is correct
38 Correct 11 ms 17108 KB Output is correct
39 Correct 12 ms 17164 KB Output is correct
40 Correct 11 ms 17620 KB Output is correct
41 Correct 11 ms 17620 KB Output is correct
42 Correct 12 ms 16980 KB Output is correct
43 Correct 12 ms 17428 KB Output is correct
44 Correct 12 ms 17364 KB Output is correct
45 Correct 12 ms 17284 KB Output is correct
46 Correct 13 ms 17348 KB Output is correct
47 Correct 12 ms 17296 KB Output is correct
48 Correct 12 ms 17236 KB Output is correct
49 Correct 12 ms 17236 KB Output is correct
50 Correct 11 ms 17188 KB Output is correct
51 Correct 12 ms 17236 KB Output is correct
52 Correct 12 ms 17236 KB Output is correct
53 Correct 12 ms 17236 KB Output is correct
54 Correct 11 ms 17364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 16724 KB Output is correct
2 Correct 9 ms 16756 KB Output is correct
3 Correct 8 ms 16724 KB Output is correct
4 Correct 10 ms 16724 KB Output is correct
5 Correct 9 ms 16852 KB Output is correct
6 Correct 8 ms 16724 KB Output is correct
7 Correct 9 ms 16696 KB Output is correct
8 Correct 10 ms 16668 KB Output is correct
9 Correct 9 ms 16676 KB Output is correct
10 Correct 8 ms 16788 KB Output is correct
11 Correct 10 ms 16980 KB Output is correct
12 Correct 8 ms 16724 KB Output is correct
13 Correct 8 ms 16724 KB Output is correct
14 Correct 10 ms 16668 KB Output is correct
15 Correct 11 ms 16732 KB Output is correct
16 Correct 9 ms 16724 KB Output is correct
17 Correct 9 ms 16724 KB Output is correct
18 Correct 11 ms 16788 KB Output is correct
19 Correct 9 ms 16724 KB Output is correct
20 Correct 9 ms 16724 KB Output is correct
21 Correct 10 ms 16792 KB Output is correct
22 Correct 9 ms 16724 KB Output is correct
23 Correct 9 ms 16788 KB Output is correct
24 Correct 9 ms 16764 KB Output is correct
25 Correct 11 ms 16684 KB Output is correct
26 Correct 11 ms 16724 KB Output is correct
27 Correct 10 ms 16724 KB Output is correct
28 Correct 9 ms 16716 KB Output is correct
29 Correct 9 ms 16744 KB Output is correct
30 Correct 9 ms 16808 KB Output is correct
31 Correct 10 ms 16852 KB Output is correct
32 Correct 12 ms 16852 KB Output is correct
33 Correct 12 ms 16936 KB Output is correct
34 Correct 12 ms 16980 KB Output is correct
35 Correct 13 ms 16980 KB Output is correct
36 Correct 11 ms 17108 KB Output is correct
37 Correct 11 ms 17108 KB Output is correct
38 Correct 11 ms 17108 KB Output is correct
39 Correct 12 ms 17164 KB Output is correct
40 Correct 11 ms 17620 KB Output is correct
41 Correct 11 ms 17620 KB Output is correct
42 Correct 12 ms 16980 KB Output is correct
43 Correct 12 ms 17428 KB Output is correct
44 Correct 12 ms 17364 KB Output is correct
45 Correct 12 ms 17284 KB Output is correct
46 Correct 13 ms 17348 KB Output is correct
47 Correct 12 ms 17296 KB Output is correct
48 Correct 12 ms 17236 KB Output is correct
49 Correct 12 ms 17236 KB Output is correct
50 Correct 11 ms 17188 KB Output is correct
51 Correct 12 ms 17236 KB Output is correct
52 Correct 12 ms 17236 KB Output is correct
53 Correct 12 ms 17236 KB Output is correct
54 Correct 11 ms 17364 KB Output is correct
55 Correct 15 ms 17876 KB Output is correct
56 Correct 35 ms 20936 KB Output is correct
57 Correct 66 ms 23792 KB Output is correct
58 Correct 96 ms 25964 KB Output is correct
59 Correct 116 ms 28844 KB Output is correct
60 Correct 145 ms 31648 KB Output is correct
61 Correct 163 ms 34488 KB Output is correct
62 Correct 204 ms 36136 KB Output is correct
63 Correct 216 ms 39632 KB Output is correct
64 Correct 232 ms 40700 KB Output is correct
65 Correct 124 ms 73272 KB Output is correct
66 Correct 112 ms 73224 KB Output is correct
67 Correct 116 ms 35820 KB Output is correct
68 Correct 192 ms 58568 KB Output is correct
69 Correct 204 ms 55880 KB Output is correct
70 Correct 193 ms 55852 KB Output is correct
71 Correct 241 ms 55884 KB Output is correct
72 Correct 217 ms 55864 KB Output is correct
73 Correct 188 ms 52352 KB Output is correct
74 Correct 194 ms 52588 KB Output is correct
75 Correct 194 ms 50988 KB Output is correct
76 Correct 189 ms 50996 KB Output is correct
77 Correct 189 ms 49880 KB Output is correct
78 Correct 206 ms 48692 KB Output is correct
79 Correct 185 ms 53696 KB Output is correct