C - Self-Stopping Shower Editorial by en_translator
Original proposer: vwxyz
Suppose there is an interval between time \(0\) and time \(T\) where hot water does not come out without Takahashi pressing the button.
If the interval has a length \(d\), Takahashi has to press the button at least \(\lfloor\frac{d+K-1}{K}\rfloor\) times for hot water to come out continuously during the interval.
The desired value is the sum of this value for all interval lengths \(d\).
To enumerate \(d\), one possible approach is to sort \(A\) and inspect the difference of \(A_i\) and \(A_{i+1}\) for each \(i\).
While this does yield a correct answer, this requires additional care, for example when \(A_i\) is around \(T\), which is error-prone.
We introduce a simpler way.
Enumerate all possible values of \(A_i\) and \(A_i+K\), together with \(0\) and \(T\), and apply coordinate compression.
The moment when hot water starts or stops coming out is any of these moments, so tracking the prefix sums allows to find the intervals for which hot water does not come out.
While this still requires handling \(T\), the timestamps exceeding \(T\) can be simply ignored from the coordinate compression.
Although this problem may require extra implementation, if you are used to coordinate compression and prefix sums, it may be processed automatically than cautiously implementing conditional branches.
Keep in mind that typical tricks are not only useful for solving problems but also simplifying implementation and reducing mistakes.