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


struct Node {
	// the value and priority of the node respectively
	long long int val, pri,size;
	// pointer to left and right child (NULL means no child)
	Node *left, *right;
	Node(long long int val) : val(val), pri(rand()),size(1), left(NULL), right(NULL){};
    void update() {
        size=1;
        if(left) size+=left->size;
        if(right) size+=right->size;
    }
    ~Node(){
        if(left) delete(left);
        if(right) delete(right);
    }
} *root;

/**
 * pass in root as pointer, left and right as references
 * to a node pointer so we can modify them
 * (alternatively, we can return left and right pointers
 * as an std::pair)
 */
void split(Node *root, long long int x, Node *&left, Node *&right) {
	if (!root) {
		left = right = NULL;
		return;
	}
	if (root->val <= x) {
		split(root->right, x, root->right, right);
		left = root;
        left->update();
        
	} else {
		split(root->left, x, left, root->left);
		right = root;
        right->update();
	}
}

/**
 * merge left and right pointers into root which
 * is a reference to a pointer to enable
 * modification within the function
 */
void merge(Node *&root, Node *left, Node *right) {
	if (!left || !right) {
		root = left ? left : right;
		return;
	}
    
	if (left->pri > right->pri) {
		merge(left->right, left->right, right);
		root = left;
        root->update();
	} else {
		merge(right->left, left, right->left);
		root = right;
        root->update();
	}
}
void insert(Node *&root,long long int k) {
    Node *a,*b;
    split(root,k,a,b);
    Node *ne = new Node(k);
    merge(b,ne,b);
    merge(root,a,b);
}
long long int get(Node *&root,long long int l,long long int r) {
    Node *a,*b,*ab,*c;
    split(root,l-1,a,ab);
    split(ab,r,b,c);
    long long int x=0;
    if(b) x=b->size;
    merge(ab,b,c);
    merge(root,a,ab);
    return x;
}
class Solution {
public:

    int countRangeSum(vector<int>& nums, int l,int r) {
        long long int pre = 0;
        Node *st = new Node(0);
        long long int ans =0;
        for(int i=0;i<nums.size();i++) {
            pre+=nums[i];
            ans+=get(st,pre-r,pre-l);
            insert(st,pre);
        }
        delete(st);
        return ans;
    }
    
};
199609