Editorial for JOI '18 Open P1 - Bubble Sort 2
Submitting an official solution before solving the problem yourself is a bannable offence.
Adding to the -term of the sequence, we may assume all the values of the sequence are different from each other.
For each element in the sequence, consider the set elements which are prior to it and larger than it. If this set is non-empty, after a pass, the element will move one step forward. Therefore, the maximum value of elements which are prior to it and larger than it will be the number of passes by bubble sort. They can take the maximum value only at vertices in the right lower convex hull of the sequence. For these vertices, the equality
holds. For other vertices, the inequality
holds. Therefore, the number of passes by bubble sort for the sequence is calculated as
It is enough to keep the value of
for each , and to update and calculate the maximum value efficiently. Since the term does not change, we may calculate it simply for each query. In order to update the term
for each query, we may add or to every element in the segment containing .
Using Segment Tree, the above operations can be done in time per query. It takes time to compress the coordinates which appear in the sequence. Therefore, in total, this problem can be solved in time.
Comments