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

class Seg {
private:
    int n;
    vector<ll> t;

    // Function to build the segment tree
    void build(vector<ll>& a, int v, int tl, int tr) {
        if (tl == tr) {
            t[v] = a[tl];
        } else {
            int tm = (tl + tr) / 2;
            build(a, 2 * v, tl, tm);
            build(a, 2 * v + 1, tm + 1, tr);
            t[v] = t[2 * v] + t[2 * v + 1];
        }
    }

    // Function to query the sum in the range [l, r]
    ll query(int v, int tl, int tr, int l, int r) {
        if (l > r) return 0;
        if (tl == l && tr == r) {
            return t[v];
        }
        int tm = (tl + tr) / 2;
        return query(2 * v, tl, tm, l, min(r, tm)) +
               query(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
    }

    // Function to update the value at index idx
    void update(int v, int tl, int tr, int idx) {
        if (tl == tr) {
            t[v]--; 
        } else {
            int tm = (tl + tr) / 2;
            if (idx <= tm) {
                update(2 * v, tl, tm, idx);
            } else {
                update(2 * v + 1, tm + 1, tr, idx);
            }
            t[v] = t[2 * v] + t[2 * v + 1];
        }
    }

public:
    // Constructor to initialize the segment tree
    Seg(vector<ll>& a) {
        n = a.size();
        t.resize(4 * n);
        build(a, 1, 0, n - 1);
    }

    // Public method to query the sum in the range [l, r]
    ll query(int l, int r) {
        return query(1, 0, n - 1, l, r);
    }

    // Public method to update the value at index idx
    void update(int idx) {
        update(1, 0, n - 1, idx);
    }
};
class Solution {
public:
    int countRangeSum(vector<int>& v, int lower, int upper) {
        int n=v.size();lower--;
        vector<ll> pref(n);
        map<ll,ll> m;
        map<ll,ll> m1;
        m[0]; m[upper]; m[lower];
        for(int i=0;i<n;i++){
            if(i>0) pref[i]+=pref[i-1];
            pref[i]+=v[i];
            m[pref[i]+upper]; m[pref[i]]; m[pref[i]+lower];
            m1[pref[i]]++;
        }
        int cnt=0;
        for(auto &x: m){
            x.second=cnt++;
        }
        vector<ll> freq(cnt+1);
        for(auto &x: m1){
            freq[m[x.first]]=x.second;
        }
        Seg st(freq);
        ll ans=0;
        ll sum=0;
        for(int i=0;i<n;i++){
            // st.update(m[pref[i]]);
            ll l=0; ll r=m[sum+upper];
            // cout<<l<<" "<<r<<"\n";
            ans+=st.query(l,r); 
            // cout<<ans<<"\n";
            r=m[sum+lower];
            // cout<<l<<" "<<r<<"\n";
            ans-=st.query(l,r);
            // cout<<ans<<"\n";
            st.update(m[pref[i]]);
            // cout<<st.query(m[pref[i]],m[pref[i]])<<"\n";
            sum=pref[i];
            
        }
        return ans;
    }
};
277229