function solve(n, q, a)
pref[0] := 0
for i from 1 to n
pref[i] := pref[i-1] + a[i]
for each query (l, r)
print pref[r] - pref[l-1]
In CSES problems, indices are typically -based. Using as a sentinel handles this cleanly. Build the prefix array in one pass, then answer each query with one subtraction.
Time complexity: .
Space complexity: for the prefix array.