Here's why prefix sums work. The sum from index to equals: Think about it: contains everything from to .
Subtracting removes everything from to . What's left is exactly the sum from to . Two array lookups and one subtraction. That's per query, no matter how big the range.