Editorial for Back To School '18: The Golden Porcupine


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

Subtasks 1 and 2 are intended to reward brute force solutions.

Time Complexity: \mathcal{O}(NT)

For Subtask 3, because l=1 and r=T, we can add up a_i, b_i, and c_i for all 1 \leq i \leq N to create one function and evaluate it over [1, T]

Time Complexity: \mathcal{O}(N + T)


The intended solution for Subtasks 4 and 5 was to use a triple Prefix Difference Array. For example, we store it in P[N][4]. After accumulating quadratic, linear, and constant differences, compute P[N][k] = P[N - 1][k] + P[N][k + 1]. The answer is in P[i][0] for all 1 \leq i \leq N.

There are many ways to solve this problem. One common solution during the contest was to keep distinct Prefix Difference Arrays for the quadratic, linear, and constant differences. Another approach that avoids the Prefix Difference Array altogether is a line sweep.

Time complexity: \mathcal{O}(N + T) or up to \mathcal{O}((N + T)\times(\log N + \log T))


Comments


  • 4
    madhur4127  commented on Jan. 21, 2020, 10:06 a.m.

    Every fast solution I see uses prefix sums technique. Can you explain more about the line sweep?