Workspace Prefix Sum
0d
Problem-solving patterns

Prefix Sums

Store the total before each boundary to answer range sums.

Recognition signal

Many queries reuse the same static sequence.

Method

Set P[0] = 0. The half-open sum [l, r) is P[r] − P[l].

Worked example

For [3, −1, 6], P = [0, 3, 2, 8]. The last two values sum to 8 − 3 = 5.

Common mistake

Inclusive and exclusive endpoints use different prefix indices.