Here's the complete pattern:
Read the array
Build prefix array: , then
For each query : output
Watch the indexing. If your array is 0-indexed, adjust the formula to . Off-by-one errors are the most common bug here. Trace through a small example by hand before submitting. Trace through a small example to verify your understanding before moving on.
Time complexity: .
Space complexity: in time and in space.