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

static const int fastIO = [] {
	std::ios_base::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr);
	return 0;
}();

class Solution {
private:
	template<typename ValueType>
	class FenwickTreePointUpdateRangeQuery {
	private:
		int n{};
		vector<ValueType> bit;

		int log2_floor(ValueType i) {
			return i ? __builtin_clzll(1) - __builtin_clzll(i) : -1;
		}

	public:

		void init(int size) {
			n = size + 1;
			bit.assign(n, 0);
		}

		void init(int size, ValueType val) {
			n = size + 1;
			bit.assign(n, val);
			bit[0] = 0;
			for (size_t i = 0; i < n; i++) {
				size_t j = i + (i & -i);
				if (j < n) bit[j] += bit[i];
			}
		}

		void add(int idx, ValueType delta) {
			for (++idx; idx < n; idx += idx & -idx)
				bit[idx] += delta;
		}

		ValueType sum(int r) {
			ValueType ret = 0;
			for (++r; r > 0; r -= r & -r)
				ret += bit[r];
			return ret;
		}

		ValueType sum(int l, int r) {
			return sum(r) - sum(l - 1);
		}
	};

public:
	int countRangeSum(vector<int>& nums, long lower, long upper) {
		int n = (int)nums.size();
		vector<pair<long, int>> RSQIDX(n);
		RSQIDX[0] = { nums[0], 0 };
		for (int i = 1; i < n; i++) RSQIDX[i] = { nums[i] + RSQIDX[i - 1].first, i };
		std::sort(RSQIDX.begin(), RSQIDX.end());
		std::vector<int> indices(n);
		for (int i = 0; i < n; i++) indices[RSQIDX[i].second] = i;
		std::vector<long> RSQ(n);
		for (int i = 0; i < n; i++) RSQ[i] = RSQIDX[i].first;
		FenwickTreePointUpdateRangeQuery<int> ft;
		ft.init(n, 1);
		int ans = 0;
		for (int i = 0; i < n; i++) {
			auto start = std::lower_bound(RSQ.begin(), RSQ.end(), lower);
			auto end = std::upper_bound(RSQ.begin(), RSQ.end(), upper);
			if (end != RSQ.begin() && start != end) {
				end--;
				int s = std::distance(RSQ.begin(), start), e = std::distance(RSQ.begin(), end);
				ans += ft.sum(s, e);
			}
			ft.add(indices[i], -1);
			lower += nums[i];
			upper += nums[i];
		}
		return ans;
	}
};
83179