<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>

#define ll long long
// Method 2: using Segment tree + Coordinate Commpression.
// const int N = 2e5+5;

class Solution {
public:
    vector<ll> segTree;
    ll query(int i, int l, int r, int lq, int rq){
        if(rq < l || lq > r) return 0; 
        if(lq <= l and rq >= r) return segTree[i];
        ll mid = (l + r) / 2;
        return query(2 * i, l, mid, lq, rq) +  query(2 * i + 1, mid + 1, r, lq, rq);
    }

    void update(int i, int l, int r, int x, int v){
        if(x < l || x > r) return;
        if(l == r) {
            segTree[i] += v; 
            return; 
        }
        int mid = (l + r) / 2;
        update(2 * i, l, mid, x, v);
        update(2 * i + 1, mid + 1, r, x, v);
        segTree[i] = segTree[2*i] + segTree[2*i + 1];
    }

    int countRangeSum(vector<int>& nums, int lower, int upper) {
        map<ll, ll> compressed_mp;
        ll sm = 0;
        compressed_mp[sm];
        compressed_mp[sm - lower];
        compressed_mp[sm - upper];
        for(auto x: nums){
            sm += x;
            compressed_mp[sm];
            compressed_mp[sm - lower];
            compressed_mp[sm - upper];
        } 
        
        ll cnt = 0;
        for(auto &x: compressed_mp){
            x.second = cnt++;
        }

        segTree.assign(4*(cnt+1),0);
        sm = 0;
        update(1, 0, cnt-1, compressed_mp[sm], 1);
        long long ans = 0;
        for(auto x: nums){
            // -lower + pre[j] >=  pre[i-1] >= -upper + pre[j]
            sm += x;
            ans += query(1, 0, cnt-1, compressed_mp[-upper + sm], compressed_mp[-lower +  sm]);
            update(1, 0, cnt-1, compressed_mp[sm], 1);
        }
        
        return ans;
    }
};


// Method1: using ordered_set.
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// typedef tree<pair<long long,int>, null_type, less<pair<long long,int>>, rb_tree_tag, tree_order_statistics_node_update> pbds;

// class Solution {
// public:
//     int countRangeSum(vector<int>& nums, int lower, int upper) {
//         pbds st;
//         st.insert({0, -1});
//         // for each j => #values satisifying 
//         // -lower + pre[j] >=  pre[i-1] >= -upper + pre[j]
//         // can be added = Ans.
//         long long sm = 0;
//         long long ans = 0;
//         for(int j = 0; j < nums.size(); j++){
//             sm += nums[j];
//             long long l = sm - upper;
//             long long r = sm - lower;
//             ans += st.order_of_key({r + 1, -2}) - st.order_of_key({l, -2});  
//             st.insert({sm, j});
//         }

//         return ans;
//     }
// };
223865