<p>Given an integer array <code>nums</code> and two integers <code>lower</code> and <code>upper</code>, return <em>the number of range sums that lie in</em> <code>[lower, upper]</code> <em>inclusive</em>.</p>

<p>Range sum <code>S(i, j)</code> is defined as the sum of the elements in <code>nums</code> between indices <code>i</code> and <code>j</code> inclusive, where <code>i &lt;= j</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

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

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [0], lower = 0, upper = 0
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li>
	<li><code>-10<sup>5</sup> &lt;= lower &lt;= upper &lt;= 10<sup>5</sup></code></li>
	<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>

class Solution {
public:
int countRangeSum(std::vector<int>& nums, int lower, int upper) {
        std::vector<long long> prefixSums(nums.size() + 1, 0);
        
        // Compute prefix sums
        for (int i = 0; i < nums.size(); ++i) {
            prefixSums[i + 1] = prefixSums[i] + nums[i];
        }
        
        // Call the merge sort based function
        return countRangeSumHelper(prefixSums, 0, prefixSums.size() - 1, lower, upper);
    }

private:
    int countRangeSumHelper(std::vector<long long>& prefixSums, int left, int right, int lower, int upper) {
        if (left >= right) return 0;

        int mid = (left + right) / 2;
        int count = 0;
        
        // Count range sums in each half
        count += countRangeSumHelper(prefixSums, left, mid, lower, upper);
        count += countRangeSumHelper(prefixSums, mid + 1, right, lower, upper);
        
        // Count range sums that cross the midpoint
        count += countCrossBoundary(prefixSums, left, mid, right, lower, upper);
        
        // Sort the prefix sums in the current range for merging
        merge(prefixSums, left, mid, right);
        
        return count;
    }
      void merge(std::vector<long long>& prefixSums, int left, int mid, int right) {
        // Create temporary arrays to hold the two halves
        std::vector<long long> leftHalf(prefixSums.begin() + left, prefixSums.begin() + mid + 1);
        std::vector<long long> rightHalf(prefixSums.begin() + mid + 1, prefixSums.begin() + right + 1);

        // Indices for the temporary arrays
        int i = 0; // Index for leftHalf
        int j = 0; // Index for rightHalf
        int k = left; // Index for prefixSums

        // Merge the two halves
        while (i < leftHalf.size() && j < rightHalf.size()) {
            if (leftHalf[i] <= rightHalf[j]) {
                prefixSums[k++] = leftHalf[i++];
            } else {
                prefixSums[k++] = rightHalf[j++];
            }
        }

        // Copy remaining elements from leftHalf, if any
        while (i < leftHalf.size()) {
            prefixSums[k++] = leftHalf[i++];
        }

        // Copy remaining elements from rightHalf, if any
        while (j < rightHalf.size()) {
            prefixSums[k++] = rightHalf[j++];
        }
    }
    
    int countCrossBoundary(std::vector<long long>& prefixSums, int left, int mid, int right, int lower, int upper) {
        int count = 0;
        std::vector<long long> merged(prefixSums.begin() + left, prefixSums.begin() + right + 1);
        
        int j = mid + 1; // Pointer for the right half of the array
        
        // Count the valid ranges crossing the midpoint
        for (int i = left; i <= mid; ++i) {
            while (j <= right && prefixSums[j] - prefixSums[i] < lower) ++j;
            int k = j;
            while (k <= right && prefixSums[k] - prefixSums[i] <= upper) ++k;
            count += (k - j);
        }
        
        return count;
    }
};
354849