Editorial
Let be the current sequence length and let be the sum of its elements. If , the answer is .
For , the minimum width required by all components and the mandatory single uncolored cells between them is
A valid coloring exists, so . Define the number of extra uncolored cells as
Let be the starting cell of component when every component is packed as far left as possible. When everything is packed as far right as possible, every component starts exactly cells later, so component starts at .
The cells covered by component in every valid coloring are exactly the overlap of these two intervals. Its size is
No cell outside these intervals is forced. Let be the number of left-packed components that end before such a cell. Place all extra uncolored cells in the gap between components and . The first components are then left-packed and the remaining components are right-packed. The cases and mean the outer gaps before the first component and after the last component. In this coloring, the cell is uncolored.
Therefore, the answer is
The insertion position does not affect this formula. We only need the number of current values, their sum, and their frequency distribution.
Collect every appearing in a type- query and coordinate-compress them. Maintain two Fenwick trees over the compressed values:
- the current frequency of each value;
- the current sum contributed by each value.
An insertion or deletion updates one position in both trees. An upper_bound and two suffix queries give the count and sum of all values greater than .
Each query takes time. The total time complexity is and the memory complexity is .