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

#include <iostream>
#include <random>
#include <array>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <algorithm>
#include <functional>
#include <queue>
#include <stack>
#include <bitset>

#include <math.h>

using namespace std;

using LL = long long;

class FenwickTree {
  public:
    FenwickTree(int n): bit_(n + 1) {}

    int ComputeSum(int k) {
      int sum = 0;

      while (k > 0) {
        sum += bit_[k];
        k -= (k & -k);
      }

      return sum;
    }

    int ComputeSum(int begin, int end) {
      return ComputeSum(end) - ComputeSum(begin - 1);
    }

    void Update(int k, int delta) {
      while (k < bit_.size()) {
        bit_[k] += delta;
        k += (k & -k);
      }
    }

  private:
    vector<int> bit_;
};

class Solution {
  public:
    int countRangeSum(vector<int>& nums, int lower, int upper) {
      int n = nums.size();

      vector<LL> prefSum(n + 1);

      for (int i = 0; i < n; ++i) {
        prefSum[i + 1] = prefSum[i] + nums[i];
      }

      auto rank = compress(prefSum, lower, upper);

      FenwickTree ft((n + 1) * 3 + 1);

      int ans = 0;

      for (int i = 0; i <= n; ++i) {
        LL num = rank[prefSum[i]];
        LL begin = rank[prefSum[i] - lower];
        LL end = rank[prefSum[i] - upper];

        if (begin > end) swap(begin, end);

        ans += ft.ComputeSum(begin + 1, end + 1);

        ft.Update(num + 1, 1);
      }

      return ans;
    }

  private:
    unordered_map<LL, int> compress(vector<LL> const& prefSum, int lower, int upper) const {
      vector<LL> sorted;

      for (int i = 0; i < prefSum.size(); ++i) {
        sorted.push_back(1LL * prefSum[i]);
        sorted.push_back(1LL * prefSum[i] - lower);
        sorted.push_back(1LL * prefSum[i] - upper);
      }

      sort(sorted.begin(), sorted.end());

      unordered_map<LL, int> rank;

      int k = 0;

      for (LL num: sorted) {
        if (!rank.contains(num)) {
          ++k;
          rank[num] = k;
        }
      }

      return rank;
    }
};

228716