<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 mergeSortV1(vector<int>& nums, int st, int end, int lower, int upper) {
        // cout<<st<<" "<<end<<endl;
        if(st == end && st >= 0 && end < nums.size()) return (nums[st] >= lower && nums[st] <= upper);
        if(st > end) {
            return 0;
        }

        int mid = st + (end - st) / 2;

        long long ans = mergeSortV1(nums, st, mid, lower, upper) + mergeSortV1(nums, mid+1, end, lower, upper);

        // Calculate suffixSum for the left half (st to mid)
        vector<long long> suffixSum(mid - st + 1, 0);
        suffixSum[mid - st] = nums[mid];
        for (int i = mid - 1; i >= st; i--) {
            suffixSum[i - st] = suffixSum[i - st + 1] + nums[i];
        }

        // Calculate prefixSum for the right half (mid+1 to end)
        vector<long long> prefixSum(end - mid, 0);
        prefixSum[0] = nums[mid + 1];
        for (int i = mid + 2; i <= end; i++) {
            prefixSum[i - mid - 1] = prefixSum[i - mid - 2] + nums[i];
        }

        // nlogn
        sort(prefixSum.begin(), prefixSum.end());
        
        for(auto e : suffixSum) {
            int a = (upper_bound(prefixSum.begin(), prefixSum.end(), upper-e) - prefixSum.begin());
            int b = (lower_bound(prefixSum.begin(), prefixSum.end(), lower-e) - prefixSum.begin());
            // cout<<"St-"<<st<<" "<<end<<" "<<a<<" "<<b<<endl;
            ans += a - b;
        }
        // cout<<st<<" "<<end<<" "<<ans<<endl;
        return ans;
    }
    int countRangeSum(vector<int>& nums, int lower, int upper) {
        int n = nums.size();
        vector<long long>preSum(n, 0);
        preSum[0] = nums[0];
        for(int i=1; i<n; i++) {
            preSum[i] = preSum[i-1] + nums[i];
        }

        return mergeSortV1(nums, 0, n-1, lower, upper);
    }

    int mergeSort(vector<long long>&preSum, int st, int end, int lower, int upper) {
        if(st == end && st >=0 && end < preSum.size()) {
            return (preSum[st] >= lower && preSum[st]<=upper);
        }
        if(st > end) {
            return 0;
        }
        int mid = st + (end - st) / 2;
        int ans = mergeSort(preSum, st, mid, lower, upper) + mergeSort(preSum, mid+1, end, lower, upper);

        int i=st, j=mid+1, k= mid+1;
        int p=0, q = mid+1;
        
        vector<long long> sortedPreSum;
        while(i <= mid) {
            // for each element in the sorted prefix sum array
            while(j<=end && preSum[j] - preSum[i] < lower) j++;
            while(k<=end && preSum[k] - preSum[i] <= upper) k++;
            ans += (k-j);
            while(q<=end && preSum[q] < preSum[i]) {
                sortedPreSum.push_back(preSum[q]);
                q++;
            }
            sortedPreSum.push_back(preSum[i++]);
        }
        while(q <= end) {
            sortedPreSum.push_back(preSum[q++]);
        }
        copy(sortedPreSum.begin(), sortedPreSum.end(), preSum.begin() + st);
        return  ans;
    }
};
262675