Array Interval Sums
Problem statement
You are given an integer array nums and an array of interval queries queries. Each query is a pair [left, right] of zero-based indices, with both endpoints included.
For each query, calculate the sum of nums[left] through nums[right]. Return one sum for each query, in the same order as the queries.
The array does not change between queries. Queries may overlap or repeat. If queries is empty, return an empty array. Use signed 64-bit sums; do not take a modulus.
Function
sumArrayIntervals(nums: int[], queries: int[][]) → long[]Examples
Example 1
nums = [3,-2,5,1]queries = [[0,2],[1,3],[2,2]]return = [6,4,5]The requested sums are 3 - 2 + 5 = 6, -2 + 5 + 1 = 4, and 5. Both endpoints belong to each interval.
Example 2
nums = [-5,4,-2]queries = [[0,0],[0,2],[2,2],[0,2]]return = [-5,-3,-2,-3]A one-element interval returns that element. The full-array sum is -5 + 4 - 2 = -3. Repeating the full-array query repeats its result.
Example 3
nums = [1000000000,1000000000,1000000000]queries = [[0,2],[1,2]]return = [3000000000,2000000000]The full interval totals 3000000000, which exceeds the signed 32-bit range. The second interval contains two values and totals 2000000000.
Constraints
1 <= nums.length <= 100000.0 <= queries.length <= 100000.-10^9 <= nums[i] <= 10^9.- Every query contains exactly two indices with
0 <= left <= right < nums.length. - Every sum fits in a signed 64-bit integer.