Official

E - Segment-Tree Optimization Editorial by evima


Assume that the “\(i\)-th partition” exists between the integers \(i\) and \(i+1\) (the \(0\)-th partition is at the beginning, and the \(N\)-th is at the end). From now on, the \(i\)-th partition is simply called partition \(i\). When creating the binary tree in the problem, if we assign to each non-leaf node the partition that divides the interval it holds into the two intervals held by its children, the following properties hold:

  • Partitions \(1\) to \(N-1\) appear once each.
  • For each node’s partition \(i\), its children hold at most one partition smaller than \(i\) and at most one partition larger than \(i\).

Considering each interval query, the partitions at both ends are \(l-1\) and \(r\). Among the nodes investigated in this query, the deepest one will be the children of the deeper of the partitions \(l-1\) and \(r\). Therefore, in order for the maximum depth to be at most \(d\), it is sufficient to assign all partitions that appear in the queries (excluding \(0\) and \(N\)) to nodes with depth at most \(d-1\) in a complete binary tree. There are \(2^d-1\) such nodes, so this gives us \(d\).

Next, we minimize \(c\). A node of depth \(d\) is investigated when either partition \(l-1\) or \(r\) is at depth \(d-1\). Also, for each query, it is seen that twice the number of partitions at depth \(d-1\) are investigated at depth \(d\). In the end, \(c\) is twice the sum of the appearance counts (which we will call weights from now on) of the partitions assigned to nodes of depth \(d-1\). Therefore, considering a complete binary tree up to depth \(d-1\), we should minimize the total weight assigned to depth \(d-1\) by assigning the partitions that appear in the queries in an appropriate order.

In a complete binary tree, partitions at depth \(d-1\) are located at odd-numbered positions (\(1\)-indexed) in the sequence of all \(2^d-1\) partitions at depth \(d-1\) or less. Also, the number of partitions that need to be placed at depth \(d-1\) or less is generally less than \(2^d-1\), so it is also possible to place nodes without assigned partitions (in this case, in the restored binary tree, the node at that position becomes a leaf, or it is assigned a partition with weight \(0\) that does not appear in the queries).

Here, consider the following problem:

Assign a sequence of \(s\) terms to \(2^d-1\) squares while preserving the order so that the sum of numbers written in odd-numbered squares is minimized (here, \(2^{d-1} \leq s \leq 2^d-1\)).

For the placements allowed in this problem, it is not generally possible to restore the binary tree in the original problem. However, in an optimal solution, blanks will only appear in odd-numbered squares, so the binary tree in the original problem can always be restored for such a placement (even if there is a blank square between partitions with adjacent values, if it is at an odd-numbered position, which is the deepest in the considered range, it can simply be a leaf without a partition).

Therefore, it is sufficient to solve the above problem, and considering that it is unnecessary to make consecutive blanks, it can be solved by DP with indices indicating how many terms of the sequence have been placed and how many blanks have been placed.

Note that \(d=0\) can be a corner case.


[Bonus (Solution for \(N\leq 2\times 10^5\))]

For the last problem, considering that even-numbered squares cannot be assigned blanks, it is seen that \(s-(2^{d-1}-1)\) numbers will be chosen for odd-numbered squares, and they are not adjacent in the original sequence. Therefore, the problem can be rephrased as follows:

Choose \(t\) non-adjacent terms from an \(s\)-term sequence so that their sum is minimized.

It can be solved similarly to the following problem:

https://atcoder.jp/contests/abc218/tasks/abc218_h

(Modified the first draft written by GPT-4.)

posted:
last update: