01Prefix sums: precompute once, answer instantly
Summing a[l..r] with a loop costs O(n) per question. With a million questions that is too slow.
Instead, build prefix sums once: p[0] = 0 and p[i+1] = p[i] + a[i]. So p[i] is the sum of the first i elements.
Now any range sum is one subtraction:
sum(a[l..r]) = p[r+1] − p[l]
p[r+1] covers everything up to r, and p[l] removes the part before l. In the picture: a[2..5] = p[6] − p[2] = 23 − 4 = 19.
Building is O(n), every query is O(1). Watch the off-by-one: p has n+1 entries, and p[i] means "before index i".