Building the prefix array takes one pass through the original array: for from to Each element adds to the running total. Time: . Space: for the prefix array. You do this once, before any queries arrive.
After that, every query becomes fast. The preprocessing cost is fixed; the per-query cost drops dramatically. Understanding this concept will help you solve more complex problems.