Build a prefix sum array where prefix[i] = sum of elements from index 1 to i. This takes time once. Now any range sum [a,b] = prefix[b] - prefix[a-1].
Why? prefix[b] includes everything up to b. Subtracting prefix[a-1] removes everything before a. You've converted q queries of each into q queries of each. Total: instead of . Understanding this concept will help you solve more complex problems.