Submission #40099

# Submission time Handle Problem Language Result Execution time Memory
40099 2018-01-27T04:34:16 Z user202729 Fireworks (APIO16_fireworks) C++14
100 / 100
797 ms 53580 KB
#ifndef _GLIBCXX_DEBUG
#define NDEBUG
#endif
#include <cassert>

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>

#ifdef Sublime
#include <iostream>
#include <sstream>
#define cin cin__
namespace std{std::stringstream cin(R"(
4 6
1 5
2 5
2 8
3 3
3 2
3 3
2 9
4 4
4 3
)");}
#endif // Sublime


template <typename T>
std::multiset<T> merge(std::multiset<T> a, std::multiset<T> b) {
	if (a.size() < b.size()) std::swap(a, b);
	a.insert(b.begin(), b.end()); // move iterator?
	return a;
}

struct function {
	int zero_slope; // negative
	int64_t zero_value;
	std::multiset<int64_t> switches;
}; // convex piecewise-linear function 
// with derivative delta of (multiple of) 1

function operator+(function a, function b) {
	return {
		a.zero_slope + b.zero_slope,
		a.zero_value + b.zero_value,
		merge(std::move(a.switches), std::move(b.switches))
	};
}

struct node {
	size_t parent; int64_t parent_len;

	// consider the function **after** connected to parent.
	function fn;
};

std::istream& operator >> (std::istream& str, node & a) {
	str >> a.parent >> a.parent_len;
	--a.parent;
	return str;
}

int main() {
	size_t nbranch, nleaf;
	std::cin >> nbranch >> nleaf;

	std::vector<node> nodes (nbranch + nleaf);

	// read input
	for (size_t i = 1; i < nodes.size(); ++i) {
		std::cin >> nodes[i];
	}

	for (size_t i = nodes.size(); i --> 1; ) {
		node& n = nodes[i];
		if (i >= nbranch) { // process leaves
			n.fn.switches.insert({
				n.parent_len, 
				n.parent_len}); // twice
			n.fn.zero_slope = 1;
			n.fn.zero_value = n.parent_len;
		} else { // branches
			// so now n.fn is the sum of the (fn)'s
			// of the node's children.
			for (int i = n.fn.switches.size() - 
				(n.fn.zero_slope + 1); i --> 0;)
				n.fn.switches.erase(--n.fn.switches.end());
			n.fn.zero_value += n.parent_len;

			auto a = --n.fn.switches.end(); auto _a = *a; n.fn.switches.erase(a);
			auto b = --n.fn.switches.end(); auto _b = *b; n.fn.switches.erase(b);
			n.fn.switches.insert({_a + n.parent_len, _b + n.parent_len});

			assert((int) n.fn.switches.size() == n.fn.zero_slope + 1);

		}
		// add to its parent
		nodes[n.parent].fn = std::move(nodes[n.parent].fn) + std::move(n.fn);
	}

	function f = std::move(nodes[0].fn);

	assert((int) f.switches.size() >= f.zero_slope);

	for (int i = f.switches.size() - f.zero_slope; i --> 0;)
		f.switches.erase(--f.switches.end());

	assert((int) f.switches.size() == f.zero_slope);

	int last_slope = f.zero_slope;
	int64_t last_x = 0, last_y = f.zero_value;
	for (int64_t x : f.switches) {
		last_y -= last_slope * (x - last_x);
		last_x = x;
		--last_slope;
	}

	assert(last_slope == 0);
	std::cout << last_y << '\n';

	return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 0 ms 2024 KB Output is correct
2 Correct 0 ms 2024 KB Output is correct
3 Correct 1 ms 2024 KB Output is correct
4 Correct 0 ms 2024 KB Output is correct
5 Correct 0 ms 2024 KB Output is correct
6 Correct 0 ms 2024 KB Output is correct
7 Correct 0 ms 2024 KB Output is correct
8 Correct 0 ms 2024 KB Output is correct
9 Correct 0 ms 2024 KB Output is correct
10 Correct 0 ms 2024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 2024 KB Output is correct
2 Correct 0 ms 2024 KB Output is correct
3 Correct 0 ms 2024 KB Output is correct
4 Correct 0 ms 2024 KB Output is correct
5 Correct 1 ms 2024 KB Output is correct
6 Correct 0 ms 2024 KB Output is correct
7 Correct 0 ms 2024 KB Output is correct
8 Correct 0 ms 2024 KB Output is correct
9 Correct 0 ms 2024 KB Output is correct
10 Correct 0 ms 2024 KB Output is correct
11 Correct 0 ms 2024 KB Output is correct
12 Correct 1 ms 2024 KB Output is correct
13 Correct 1 ms 2024 KB Output is correct
14 Correct 0 ms 2024 KB Output is correct
15 Correct 1 ms 2024 KB Output is correct
16 Correct 0 ms 2024 KB Output is correct
17 Correct 1 ms 2024 KB Output is correct
18 Correct 0 ms 2024 KB Output is correct
19 Correct 1 ms 2024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 2024 KB Output is correct
2 Correct 0 ms 2024 KB Output is correct
3 Correct 1 ms 2024 KB Output is correct
4 Correct 0 ms 2024 KB Output is correct
5 Correct 0 ms 2024 KB Output is correct
6 Correct 0 ms 2024 KB Output is correct
7 Correct 0 ms 2024 KB Output is correct
8 Correct 0 ms 2024 KB Output is correct
9 Correct 0 ms 2024 KB Output is correct
10 Correct 0 ms 2024 KB Output is correct
11 Correct 0 ms 2024 KB Output is correct
12 Correct 0 ms 2024 KB Output is correct
13 Correct 0 ms 2024 KB Output is correct
14 Correct 0 ms 2024 KB Output is correct
15 Correct 1 ms 2024 KB Output is correct
16 Correct 0 ms 2024 KB Output is correct
17 Correct 0 ms 2024 KB Output is correct
18 Correct 0 ms 2024 KB Output is correct
19 Correct 0 ms 2024 KB Output is correct
20 Correct 0 ms 2024 KB Output is correct
21 Correct 0 ms 2024 KB Output is correct
22 Correct 1 ms 2024 KB Output is correct
23 Correct 1 ms 2024 KB Output is correct
24 Correct 0 ms 2024 KB Output is correct
25 Correct 1 ms 2024 KB Output is correct
26 Correct 0 ms 2024 KB Output is correct
27 Correct 1 ms 2024 KB Output is correct
28 Correct 0 ms 2024 KB Output is correct
29 Correct 1 ms 2024 KB Output is correct
30 Correct 1 ms 2156 KB Output is correct
31 Correct 1 ms 2188 KB Output is correct
32 Correct 0 ms 2224 KB Output is correct
33 Correct 1 ms 2332 KB Output is correct
34 Correct 5 ms 2388 KB Output is correct
35 Correct 8 ms 2440 KB Output is correct
36 Correct 5 ms 2476 KB Output is correct
37 Correct 6 ms 2528 KB Output is correct
38 Correct 6 ms 2548 KB Output is correct
39 Correct 9 ms 2548 KB Output is correct
40 Correct 5 ms 2416 KB Output is correct
41 Correct 5 ms 2416 KB Output is correct
42 Correct 2 ms 2416 KB Output is correct
43 Correct 8 ms 2680 KB Output is correct
44 Correct 7 ms 2680 KB Output is correct
45 Correct 8 ms 2680 KB Output is correct
46 Correct 9 ms 2944 KB Output is correct
47 Correct 8 ms 2940 KB Output is correct
48 Correct 8 ms 2776 KB Output is correct
49 Correct 9 ms 2944 KB Output is correct
50 Correct 6 ms 2756 KB Output is correct
51 Correct 3 ms 2748 KB Output is correct
52 Correct 8 ms 2772 KB Output is correct
53 Correct 2 ms 2788 KB Output is correct
54 Correct 6 ms 2944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 2024 KB Output is correct
2 Correct 0 ms 2024 KB Output is correct
3 Correct 1 ms 2024 KB Output is correct
4 Correct 0 ms 2024 KB Output is correct
5 Correct 0 ms 2024 KB Output is correct
6 Correct 0 ms 2024 KB Output is correct
7 Correct 0 ms 2024 KB Output is correct
8 Correct 0 ms 2024 KB Output is correct
9 Correct 0 ms 2024 KB Output is correct
10 Correct 0 ms 2024 KB Output is correct
11 Correct 0 ms 2024 KB Output is correct
12 Correct 0 ms 2024 KB Output is correct
13 Correct 0 ms 2024 KB Output is correct
14 Correct 0 ms 2024 KB Output is correct
15 Correct 1 ms 2024 KB Output is correct
16 Correct 0 ms 2024 KB Output is correct
17 Correct 0 ms 2024 KB Output is correct
18 Correct 0 ms 2024 KB Output is correct
19 Correct 0 ms 2024 KB Output is correct
20 Correct 0 ms 2024 KB Output is correct
21 Correct 0 ms 2024 KB Output is correct
22 Correct 1 ms 2024 KB Output is correct
23 Correct 1 ms 2024 KB Output is correct
24 Correct 0 ms 2024 KB Output is correct
25 Correct 1 ms 2024 KB Output is correct
26 Correct 0 ms 2024 KB Output is correct
27 Correct 1 ms 2024 KB Output is correct
28 Correct 0 ms 2024 KB Output is correct
29 Correct 1 ms 2024 KB Output is correct
30 Correct 1 ms 2156 KB Output is correct
31 Correct 1 ms 2188 KB Output is correct
32 Correct 0 ms 2224 KB Output is correct
33 Correct 1 ms 2332 KB Output is correct
34 Correct 5 ms 2388 KB Output is correct
35 Correct 8 ms 2440 KB Output is correct
36 Correct 5 ms 2476 KB Output is correct
37 Correct 6 ms 2528 KB Output is correct
38 Correct 6 ms 2548 KB Output is correct
39 Correct 9 ms 2548 KB Output is correct
40 Correct 5 ms 2416 KB Output is correct
41 Correct 5 ms 2416 KB Output is correct
42 Correct 2 ms 2416 KB Output is correct
43 Correct 8 ms 2680 KB Output is correct
44 Correct 7 ms 2680 KB Output is correct
45 Correct 8 ms 2680 KB Output is correct
46 Correct 9 ms 2944 KB Output is correct
47 Correct 8 ms 2940 KB Output is correct
48 Correct 8 ms 2776 KB Output is correct
49 Correct 9 ms 2944 KB Output is correct
50 Correct 6 ms 2756 KB Output is correct
51 Correct 3 ms 2748 KB Output is correct
52 Correct 8 ms 2772 KB Output is correct
53 Correct 2 ms 2788 KB Output is correct
54 Correct 6 ms 2944 KB Output is correct
55 Correct 25 ms 3580 KB Output is correct
56 Correct 79 ms 7584 KB Output is correct
57 Correct 143 ms 11512 KB Output is correct
58 Correct 186 ms 14088 KB Output is correct
59 Correct 270 ms 18228 KB Output is correct
60 Correct 331 ms 22156 KB Output is correct
61 Correct 381 ms 24828 KB Output is correct
62 Correct 500 ms 27272 KB Output is correct
63 Correct 610 ms 32092 KB Output is correct
64 Correct 608 ms 33796 KB Output is correct
65 Correct 364 ms 25464 KB Output is correct
66 Correct 329 ms 25464 KB Output is correct
67 Correct 326 ms 25596 KB Output is correct
68 Correct 527 ms 39588 KB Output is correct
69 Correct 535 ms 39720 KB Output is correct
70 Correct 567 ms 39720 KB Output is correct
71 Correct 677 ms 53316 KB Output is correct
72 Correct 672 ms 53312 KB Output is correct
73 Correct 613 ms 53560 KB Output is correct
74 Correct 614 ms 53572 KB Output is correct
75 Correct 653 ms 53276 KB Output is correct
76 Correct 651 ms 53344 KB Output is correct
77 Correct 746 ms 52220 KB Output is correct
78 Correct 706 ms 52392 KB Output is correct
79 Correct 797 ms 53580 KB Output is correct