<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 SegmentTree
{
public:
    SegmentTree(vector<int>& arr) : n(arr.size())
    {
        t.resize(4*n);
        build(arr,1,0,n-1);
    }
    long long sum(int l, int r)
    {
        return sum(1,0,n-1,l,r);
    }
    void update(int p, int newV)
    {
        return update(1,0,n-1,p,newV);
    }
    void add(int p, int dif)
    {
        return add(1,0,n-1,p,dif);
    }
private:
    long long sum(int v, int tl, int tr, int l, int r) {
        if (l > r) return 0;
        if (l == tl && r == tr) return t[v];
        int tm = (tl + tr) / 2;
        return sum(v*2, tl, tm, l, min(r, tm))
            + sum(v*2+1, tm+1, tr, max(l, tm+1), r);
    }
    void update(int v, int tl, int tr, int pos, int new_val) {
        if (tl == tr)  t[v] = new_val;
        else {
            int tm = (tl + tr) / 2;
            if (pos <= tm)
                update(v*2, tl, tm, pos, new_val);
            else
                update(v*2+1, tm+1, tr, pos, new_val);
            t[v] = t[v*2] + t[v*2+1];
        }
    }
    void add(int v, int tl, int tr, int pos, int dif) {
        if (tl == tr)  t[v] += dif;
        else {
            int tm = (tl + tr) / 2;
            if (pos <= tm)
                add(v*2, tl, tm, pos, dif);
            else
                add(v*2+1, tm+1, tr, pos, dif);
            t[v] = t[v*2] + t[v*2+1];
        }
    }
    void build(vector<int>& a, int v, int tl, int tr) {
        if (tl == tr) {
            t[v] = a[tl];
        } else {
            int tm = (tl + tr) / 2;
            build(a, v*2, tl, tm);
            build(a, v*2+1, tm+1, tr);
            t[v] = t[v*2] + t[v*2+1];
        }
    }
    int n;
    vector<long long> t;
};

class Solution {
public:
    int countRangeSum(vector<int>& nums, int lower, int upper){
        int sz = nums.size();
        vector<long long> sum(sz+1);
        for(int i = 0; i < sz; ++i) sum[i+1] = sum[i]+nums[i];
        vector<long long> A = sum;
        sort(begin(A),end(A));
        vector<int> v(sz+1,1);    
        SegmentTree tree(v);
        int count = 0;
        for(const auto &x : sum){
            int idx = lower_bound(A.begin(), A.end(), x)-A.begin();
            int low = lower_bound(A.begin(), A.end(), x+lower)-A.begin();
            int up = upper_bound(A.begin(), A.end(), x+upper)-A.begin()-1;
            tree.add(idx, -1);
            count += tree.sum(low,up);
            cout<<count<<endl;
        }
        return count;
    }
};
92881