<p>You are given the array <code>nums</code> consisting of <code>n</code> positive integers. You computed the sum of all non-empty continuous subarrays from the array and then sorted them in non-decreasing order, creating a new array of <code>n * (n + 1) / 2</code> numbers.</p>

<p><em>Return the sum of the numbers from index </em><code>left</code><em> to index </em><code>right</code> (<strong>indexed from 1</strong>)<em>, inclusive, in the new array. </em>Since the answer can be a huge number return it modulo <code>10<sup>9</sup> + 7</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,4], n = 4, left = 1, right = 5
<strong>Output:</strong> 13 
<strong>Explanation:</strong> All subarray sums are 1, 3, 6, 10, 2, 5, 9, 3, 7, 4. After sorting them in non-decreasing order we have the new array [1, 2, 3, 3, 4, 5, 6, 7, 9, 10]. The sum of the numbers from index le = 1 to ri = 5 is 1 + 2 + 3 + 3 + 4 = 13. 
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,4], n = 4, left = 3, right = 4
<strong>Output:</strong> 6
<strong>Explanation:</strong> The given array is the same as example 1. We have the new array [1, 2, 3, 3, 4, 5, 6, 7, 9, 10]. The sum of the numbers from index le = 3 to ri = 4 is 3 + 3 = 6.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,4], n = 4, left = 1, right = 10
<strong>Output:</strong> 50
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == nums.length</code></li>
	<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
	<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
	<li><code>1 &lt;= left &lt;= right &lt;= n * (n + 1) / 2</code></li>
</ul>

static const auto __optimize__ = []()
{
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  return 0;
}();

class Solution {
 public:
  int *a = nullptr;
  void quickS(int a[], int s, int e, int lp, int rp)
  {
    if (s >= e) return;
    if (s == e-1) {
      if (a[s] > a[e]) swap(a[s], a[e]);
      return;
    }
    swap(a[(s+e)/2], a[e]);
    int j = s, pivot = a[e];

    for (int i = s; i < e; i++)
    {
      if (a[i] < pivot)
      {
        if (i != j) swap(a[i], a[j]);
        j++;
      }
    }
    /* swap the pivot & a[j] */
    a[e] = a[j];
    a[j] = pivot;

    if (j < lp) quickS(a, j+1, e, lp, rp);
    else if (j > rp) quickS(a, s, j-1, lp, rp);
    else {
      if (j < rp) quickS(a, j+1, e, rp, rp);
      if (j > lp) quickS(a, s, j-1, lp, lp);
    }
  }

  int rangeSum(vector<int>& nums, int n, int left, int right)
  {
    int sz = nums.size();
    int asz = sz*(sz+1)/2;
    //int *a = new int[asz];
    int pos = 1;

    if (a == nullptr) a = new int[1000*1001/2];

    a[0] = nums[0];
    for (; pos < sz; pos++) a[pos] = a[pos-1] + nums[pos];

    for (int x = sz-1; x > 0; x--)
    {
      int decr = a[pos-x-1];
      for (int y = 0; y < x; y++)
      {
        a[pos] = a[pos-x] - decr;
        pos++;
      } 
    }

    quickS(a, 0, asz-1, left-1, right-1);

    long sum = 0;
    for (int x = left-1; x < right; x++)
      sum += a[x];
    if (sum > 1000000007)
        sum %= 1000000007;
    return (int)sum;
  }      
};
17354