# Question

Formatted question description: https://leetcode.ca/all/327.html

 327. Count of Range Sum

Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.
Range sum S(i, j) is defined as the sum of the elements in nums between indices i and j (i ≤ j), inclusive.

Note:
A naive algorithm of O(n^2) is trivial. You MUST do better than that.

Example:

Input: nums = [-2,5,-1], lower = -2, upper = 2,
Output: 3
Explanation: The three ranges are : [0,0], [2,2], [0,2] and their respective sums are: -2, -1, 2.

@tag-interval



# Algorithm

The most intuitive idea of ​​this question is to enumerate all the sub-arrays and then check whether they are within the required value range. The time complexity of this method is O(n^2), and it will obviously time out.

What is the easiest thing to think of when seeing this kind of topic? Two Pointers! Yes, but only using Two Pointers is definitely not enough for this problem. Based on the ideas of Two Pointers, we can find a better solution by merging and sorting.

Here our sorting object is the prefix sum array. In the merge stage of merge sort, we have left and right arrays, and both the left and right arrays are sorted, so we can traverse the left array with i, j, k The two pointers are searched in the right array, so that:

sums[j]-sums[i] <upper
sums[k]-sums[i] >= lower


So at this time, we have found j-k sub-arrays that meet the requirements.

Since the left and right arrays are all sorted, when i is incremented, the positions of j and k need not be scanned from the beginning.

One last thing to note is that in order to prevent overflow, our vector contains long long elements.

Java