## Editorial for DMOPC '19 Contest 6 P5 - University Math

Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.

Submitting an official solution before solving the problem yourself is a bannable offence.

Author: george_chen

In the first subtask, we observe that the answer is just the total boundary length of the one or two bars we are asked to draw.

Time complexity: We observe that the histogram can be modelled as a graph. Let every lattice point be a node and every boundary be a bidirectional edge that connects its endpoints. Each node in our graph has degree two or three. Observe that any valid way of drawing corresponds to some path in this graph.

Suppose the optimal path is . From the task statement, clearly traverses every edge of our graph at least once. If it traverses some edge more than once, we simply add duplicate edges until traverses every edge exactly once. Thus, we observe that is essentially an Euler path and our problem is reduced to finding the set of edges with smallest sum of lengths to add that ensures our graph contains an Euler path. From the definition of an Euler path, our graph can have at most two vertices with odd degree.

For the second subtask, our graph has at most vertices. We can simply keep a bitmask of which vertices have odd degree and transition by adding edges to our graph. Since is very small, it's better to preprocess all possible queries beforehand.

Time complexity: There is no intended solution for the third subtask.

Time complexity: For the fourth subtask, we will further simplify our graph. We claim that any chain can be shortened into a single edge.

Suppose we have a chain (a sequence of nodes of degree ). Observe that it is never more optimal to traverse part of this chain from one endpoint, turn back, traverse the remaining edges from the other endpoint, and turn back than it is to just traverse the entire chain at once and then turn back. This is because the distance is using either method. Thus, we can replace all chains with a single edge which is the sum of the lengths of the edges of the chain. Thus, our graph is comprised only of vertices with degree .

Observe that the graph looks like this: Thus, we can perform a bitmask dp with representing the minimum sum of edge lengths with being the current bar, being a bitmask of the parity of the degree of the previous two nodes, and being the number of nodes before that have odd degree. Our final answer is the min of any state at the right end of our query interval with .

Time complexity: There is no intended solution for the fifth subtask.

Time complexity: For the final subtask, we store the dp transitions on a segment tree. In a node covering the range on the segtree, we store the minimum distance where is the bitmask of the nodes of the -th bar, is the bitmask of the nodes of the -th bar, and is how many additional nodes with odd outdegree are created in this interval. Queries can be answered by following the state transitions inside the nodes of the query interval. Initializing and querying the segment tree have a fairly high constant but solutions with decent implementation will pass.

Time complexity: Another approach would be to perform divide and conquer on the queries. Split the array at its midpoint and compute the dp from the midpoint to every other element of the array. Then, all queries crossing the midpoint can be answered by merging the answer to two bitmask dp's. The remaining queries can be answered by recursively solving the left and right halves of the array.

Time complexity: 