Range Sum Query - Immutable
Given an integer array nums that is never modified, answer a batch of range-sum queries. Each query is a pair [i, j] (0-based, with i <= j) asking for the sum of the elements from index i to index j **inclusive** of both ends.
Return an array whose k-th entry is the answer to the k-th query. Because the array is immutable, the expectation is that you precompute once and then answer every query in constant time.
Example cases
- overlapping rangesin nums = [-2,0,3,-5,2,-1]queries =022505out [1,-1,-3]nums[0..2] = -2+0+3 = 1; nums[2..5] = 3-5+2-1 = -1; nums[0..5] = -3.
- interior rangein nums = [1,2,3,4]queries =13out [9]nums[1..3] = 2+3+4 = 9.
- single elementin nums = [5]queries =00out [5]
- no queriesin nums = [7,8,9], queries = []out []
Constraints
- 0 <= nums.length <= 10^4
- -10^5 <= nums[i] <= 10^5
- 0 <= queries.length <= 10^4
- 0 <= i <= j < nums.length for every query [i, j]
nums =
[-2,0,3,-5,2,-1]
queries =
[[0,2],[2,5],[0,5]]