Editorial for DMOPC '18 Contest 4 P4 - Dr. Henri and Lab Data


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: Kirito

For 10% of points, we can loop over the numbers Al,Al+1,,Ar1,Ar for each query, and find the appropriate difference in O(N).

Time Complexity: O(NQ)

For 60% of points, we have two possible solutions. The first solution involves maintaining a range tree of sorted vectors, and using binary search to answer each query in O(log2N), which can be optimized to O(logN). The optimization is left as an exercise to the reader.

Time Complexity: O((N+Q)logN)

Another possible solution is to solve the queries using offline processing. We observe that the answer to a query q=(l,r,k) is the sum Al+Al+1++Ar1+Ar minus twice the sum of all numbers less than k in the range A[l,r].

We can compute the first part in O(1), with O(N) preprocessing, using a prefix sum array. To handle the second sum, we can sort all queries by their k values. We can loop through the values i=0,1,2,,max(A), and update a range tree to contain all elements with a value less than or equal to i, and then execute all queries where i=k1. This results in a time complexity of O(logN) per update and query.

Time Complexity: O((N+Q)logN)

For the final 30% of points, we observe that we don't have to loop through i=0,1,2,,max(A). Rather, we see that the values of A and k are irrelevant when processing offline, and only their relative order matters.

Thus we can remap A[1],A[2],,A[N] and the k values of the queries to the numbers 1,2,,N+Q, and solve the problem in the same way as the last subtask.

Time Complexity: O((N+Q)logN)


Comments


  • 0
    Mystical  commented on June 14, 2024, 10:29 p.m. edited

    or just throw a merge sort tree at it

    edit: didn't see "range tree of sorted vectors" (same thing)