Editorial for COCI '20 Contest 6 #5 Index
Submitting an official solution before solving the problem yourself is a bannable offence.
First subtask can be solved by brute force.
The idea for the rest of the solution is to use binary search to determine the -index. We need to be able to answer questions of the form "How many numbers on positions through are greater than or equal to ?" efficiently. The difference between the second and third subtask will be in the data structure we use to answer such queries.
One possible approach is to build a segment tree, where the node corresponding to the interval holds the numbers sorted in increasing order. When we query, we use binary search in the appropriate nodes to find how many numbers are greater than in the corresponding interval of the node. Therefore we can answer each question from the problem in complexity, which is fast enough for the second subtask.
For all points, there are multiple approaches. One is to use parallel binary search (most solutions on HONI, local version of COCI, were of this type). We will describe a different solution that uses a persistent segment tree. Times will be the prefixes of the array, that is, we will have a "separate" segment tree for each prefix. A node in the tree corresponding to the interval will hold the value , where is the number of elements in the prefix that are equal to .
Now we can answer the question "How many numbers on positions through are greater than or equal to ?" by querying the sum of values in the interval in moments and , and subtracting them. The complexity per question from the problem is .
For those who want to know more: there is a solution with complexity per question, that uses the same data structure as described for the second subtask. It can be sped up using a technique known as fractional cascading. You can read more about it here.
Comments
This problem is solvable using wavelet Tree in O(log(maxA[I]) * log(n) )
This problem is solvable using Parallel Binary Search