Static Range Sum Query

Given a static array nums, return the sum of values for every queried interval. This is a basic range-query problem that can be solved with a prefix array, but it also serves as a clean introductory case for range-query data structures such as segment trees.

Input Format

nums = static integer array, queries = range sum requests

Output Format

sum for each query, in order

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= queries.length <= 10^5
  • Each query is [left, right] with 0 <= left <= right < nums.length.

Examples

Example 1:

Input:

nums = [1,3,5,7]
queries = [[0,1],[1,3]]

Output:

[4,15]

Explanation:

The first interval sums to 4 and the second interval sums to 15.

Example 2:

Input:

nums = [2,4,6]
queries = [[0,2]]

Output:

[12]

Explanation:

The sum of the full array is 12.

Loading...
Static Range Sum Query - Advanced Trees