Submission #605942

# Submission time Handle Problem Language Result Execution time Memory
605942 2022-07-26T04:29:08 Z GusterGoose27 Fireworks (APIO16_fireworks) C++11
100 / 100
337 ms 80328 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pii;

class hull {
public:
	ll init_val = 0;
	ll init_slope = 0;
	multiset<ll> changes;
	hull() {}
	void prune(ll length) {
		init_val += length;
		if (changes.empty()) {
			changes.insert(length);
			changes.insert(length);
			init_slope = -1;
			return;
		}
		while (changes.size() > -init_slope+1) changes.erase(changes.find(*changes.rbegin()));
		auto it = changes.rbegin();
		ll b2 = *it;
		changes.erase(changes.find(*it));
		it = changes.rbegin();
		ll b1 = *it;
		changes.erase(changes.find(*it));
		changes.insert(b1+length);
		changes.insert(b2+length);
	}
	void merge(hull *other) {
		init_val += other->init_val;
		init_slope += other->init_slope;
		for (ll v: other->changes) changes.insert(v);
		delete other;
	}
	ll size() {
		return changes.size();
	}
};

const ll MAXN = 3e5;
ll n, m;
vector<pii> edges[MAXN];
hull *hulls[MAXN];

void dfs(ll cur = 0) {
	hulls[cur] = new hull();
	for (pii e: edges[cur]) {
		ll next = e.first;
		dfs(next);
		hulls[next]->prune(e.second);
		if (hulls[cur]->size() > hulls[next]->size()) hulls[cur]->merge(hulls[next]);
		else {
			hulls[next]->merge(hulls[cur]);
			hulls[cur] = hulls[next];
		}
	}
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	cin >> n >> m;
	for (ll i = 1; i < n+m; i++) {
		ll p, w;
		cin >> p >> w; p--;
		edges[p].push_back(pii(i, w));
	}
	dfs();
	hull *h = hulls[0];
	ll v = h->init_val;
	ll slope = h->init_slope;
	ll prev = 0;
	for (ll change: h->changes) {
		v += slope*(change-prev);
		slope++;
		if (slope == 0) break;
		prev = change;
	}
	cout << v << "\n";
}

Compilation message

fireworks.cpp: In member function 'void hull::prune(ll)':
fireworks.cpp:22:25: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
   22 |   while (changes.size() > -init_slope+1) changes.erase(changes.find(*changes.rbegin()));
      |          ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7252 KB Output is correct
2 Correct 4 ms 7252 KB Output is correct
3 Correct 5 ms 7252 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 4 ms 7380 KB Output is correct
6 Correct 4 ms 7252 KB Output is correct
7 Correct 6 ms 7252 KB Output is correct
8 Correct 5 ms 7380 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 4 ms 7380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7252 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
3 Correct 4 ms 7252 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 4 ms 7252 KB Output is correct
6 Correct 4 ms 7252 KB Output is correct
7 Correct 4 ms 7252 KB Output is correct
8 Correct 5 ms 7252 KB Output is correct
9 Correct 4 ms 7252 KB Output is correct
10 Correct 4 ms 7380 KB Output is correct
11 Correct 5 ms 7380 KB Output is correct
12 Correct 5 ms 7380 KB Output is correct
13 Correct 5 ms 7380 KB Output is correct
14 Correct 6 ms 7380 KB Output is correct
15 Correct 4 ms 7380 KB Output is correct
16 Correct 4 ms 7380 KB Output is correct
17 Correct 4 ms 7380 KB Output is correct
18 Correct 4 ms 7380 KB Output is correct
19 Correct 6 ms 7380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7252 KB Output is correct
2 Correct 4 ms 7252 KB Output is correct
3 Correct 5 ms 7252 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 4 ms 7380 KB Output is correct
6 Correct 4 ms 7252 KB Output is correct
7 Correct 6 ms 7252 KB Output is correct
8 Correct 5 ms 7380 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 4 ms 7380 KB Output is correct
11 Correct 5 ms 7252 KB Output is correct
12 Correct 4 ms 7372 KB Output is correct
13 Correct 4 ms 7252 KB Output is correct
14 Correct 4 ms 7380 KB Output is correct
15 Correct 4 ms 7252 KB Output is correct
16 Correct 4 ms 7252 KB Output is correct
17 Correct 4 ms 7252 KB Output is correct
18 Correct 5 ms 7252 KB Output is correct
19 Correct 4 ms 7252 KB Output is correct
20 Correct 4 ms 7380 KB Output is correct
21 Correct 5 ms 7380 KB Output is correct
22 Correct 5 ms 7380 KB Output is correct
23 Correct 5 ms 7380 KB Output is correct
24 Correct 6 ms 7380 KB Output is correct
25 Correct 4 ms 7380 KB Output is correct
26 Correct 4 ms 7380 KB Output is correct
27 Correct 4 ms 7380 KB Output is correct
28 Correct 4 ms 7380 KB Output is correct
29 Correct 6 ms 7380 KB Output is correct
30 Correct 4 ms 7380 KB Output is correct
31 Correct 5 ms 7380 KB Output is correct
32 Correct 5 ms 7384 KB Output is correct
33 Correct 5 ms 7512 KB Output is correct
34 Correct 5 ms 7512 KB Output is correct
35 Correct 6 ms 7636 KB Output is correct
36 Correct 7 ms 7616 KB Output is correct
37 Correct 7 ms 7672 KB Output is correct
38 Correct 7 ms 7636 KB Output is correct
39 Correct 8 ms 7636 KB Output is correct
40 Correct 7 ms 8528 KB Output is correct
41 Correct 8 ms 8532 KB Output is correct
42 Correct 6 ms 7636 KB Output is correct
43 Correct 7 ms 8020 KB Output is correct
44 Correct 7 ms 8020 KB Output is correct
45 Correct 7 ms 8020 KB Output is correct
46 Correct 7 ms 7796 KB Output is correct
47 Correct 9 ms 7780 KB Output is correct
48 Correct 7 ms 7764 KB Output is correct
49 Correct 9 ms 7772 KB Output is correct
50 Correct 6 ms 7764 KB Output is correct
51 Correct 7 ms 7768 KB Output is correct
52 Correct 6 ms 7764 KB Output is correct
53 Correct 7 ms 7764 KB Output is correct
54 Correct 7 ms 7976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7252 KB Output is correct
2 Correct 4 ms 7252 KB Output is correct
3 Correct 5 ms 7252 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 4 ms 7380 KB Output is correct
6 Correct 4 ms 7252 KB Output is correct
7 Correct 6 ms 7252 KB Output is correct
8 Correct 5 ms 7380 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 4 ms 7380 KB Output is correct
11 Correct 5 ms 7252 KB Output is correct
12 Correct 4 ms 7372 KB Output is correct
13 Correct 4 ms 7252 KB Output is correct
14 Correct 4 ms 7380 KB Output is correct
15 Correct 4 ms 7252 KB Output is correct
16 Correct 4 ms 7252 KB Output is correct
17 Correct 4 ms 7252 KB Output is correct
18 Correct 5 ms 7252 KB Output is correct
19 Correct 4 ms 7252 KB Output is correct
20 Correct 4 ms 7380 KB Output is correct
21 Correct 5 ms 7380 KB Output is correct
22 Correct 5 ms 7380 KB Output is correct
23 Correct 5 ms 7380 KB Output is correct
24 Correct 6 ms 7380 KB Output is correct
25 Correct 4 ms 7380 KB Output is correct
26 Correct 4 ms 7380 KB Output is correct
27 Correct 4 ms 7380 KB Output is correct
28 Correct 4 ms 7380 KB Output is correct
29 Correct 6 ms 7380 KB Output is correct
30 Correct 4 ms 7380 KB Output is correct
31 Correct 5 ms 7380 KB Output is correct
32 Correct 5 ms 7384 KB Output is correct
33 Correct 5 ms 7512 KB Output is correct
34 Correct 5 ms 7512 KB Output is correct
35 Correct 6 ms 7636 KB Output is correct
36 Correct 7 ms 7616 KB Output is correct
37 Correct 7 ms 7672 KB Output is correct
38 Correct 7 ms 7636 KB Output is correct
39 Correct 8 ms 7636 KB Output is correct
40 Correct 7 ms 8528 KB Output is correct
41 Correct 8 ms 8532 KB Output is correct
42 Correct 6 ms 7636 KB Output is correct
43 Correct 7 ms 8020 KB Output is correct
44 Correct 7 ms 8020 KB Output is correct
45 Correct 7 ms 8020 KB Output is correct
46 Correct 7 ms 7796 KB Output is correct
47 Correct 9 ms 7780 KB Output is correct
48 Correct 7 ms 7764 KB Output is correct
49 Correct 9 ms 7772 KB Output is correct
50 Correct 6 ms 7764 KB Output is correct
51 Correct 7 ms 7768 KB Output is correct
52 Correct 6 ms 7764 KB Output is correct
53 Correct 7 ms 7764 KB Output is correct
54 Correct 7 ms 7976 KB Output is correct
55 Correct 12 ms 8264 KB Output is correct
56 Correct 38 ms 10700 KB Output is correct
57 Correct 67 ms 13180 KB Output is correct
58 Correct 80 ms 14944 KB Output is correct
59 Correct 114 ms 17708 KB Output is correct
60 Correct 173 ms 19760 KB Output is correct
61 Correct 181 ms 21792 KB Output is correct
62 Correct 178 ms 23952 KB Output is correct
63 Correct 225 ms 26736 KB Output is correct
64 Correct 265 ms 27104 KB Output is correct
65 Correct 146 ms 80232 KB Output is correct
66 Correct 135 ms 80328 KB Output is correct
67 Correct 123 ms 24056 KB Output is correct
68 Correct 257 ms 49700 KB Output is correct
69 Correct 253 ms 45688 KB Output is correct
70 Correct 235 ms 45612 KB Output is correct
71 Correct 285 ms 34932 KB Output is correct
72 Correct 285 ms 35116 KB Output is correct
73 Correct 281 ms 33708 KB Output is correct
74 Correct 259 ms 33848 KB Output is correct
75 Correct 276 ms 33016 KB Output is correct
76 Correct 278 ms 33016 KB Output is correct
77 Correct 312 ms 32792 KB Output is correct
78 Correct 297 ms 33308 KB Output is correct
79 Correct 337 ms 46156 KB Output is correct