Editorial for QCC P6 - Freedom!


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

Subtask 1

For each query, sum the cost from each house to hi via simulation.

Time Complexity: O(QN2)

Subtask 2

For each query, use a pointer on the left and right side of hi to simulate the cost.

Time Complexity: O(NQ)

Subtask 3

For the final subtask, there are a few different solutions. One solution involves building a prefix sum array and suffix sum array where:

psai={0if i=1psai1+abs(aiai1)×(i1)if 1<iN

ssai={0if i=Nssai+1+abs(aiai+1)×(Ni)if 1i<N

The answer for each query is psahi+ssahi. Note that the above solution uses 1-indexed arrays.

Time Complexity: O(N+Q)


Comments

There are no comments at the moment.