# 区间和的个数

<p>给你一个整数数组 <code>nums</code> 以及两个整数 <code>lower</code> 和 <code>upper</code> 。求数组中，值位于范围 <code>[lower, upper]</code> （包含 <code>lower</code> 和 <code>upper</code>）之内的 <strong>区间和的个数</strong> 。</p>

<p><strong>区间和</strong> <code>S(i, j)</code> 表示在 <code>nums</code> 中，位置从 <code>i</code> 到 <code>j</code> 的元素之和，包含 <code>i</code> 和 <code>j</code> (<code>i</code> ≤ <code>j</code>)。</p>

<p> </p>
<strong>示例 1：</strong>

<pre>
<strong>输入：</strong>nums = [-2,5,-1], lower = -2, upper = 2
<strong>输出：</strong>3
<strong>解释：</strong>存在三个区间：[0,0]、[2,2] 和 [0,2] ，对应的区间和分别是：-2 、-1 、2 。
</pre>

<p><strong>示例 2：</strong></p>

<pre>
<strong>输入：</strong>nums = [0], lower = 0, upper = 0
<strong>输出：</strong>1
</pre>

<p> </p>

<p><strong>提示：</strong></p>

<ul>
	<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
	<li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li>
	<li><code>-10<sup>5</sup> <= lower <= upper <= 10<sup>5</sup></code></li>
	<li>题目数据保证答案是一个 <strong>32 位</strong> 的整数</li>
</ul>


## template

```python

class Solution:
    def countRangeSum(self, nums: List[int], lower: int, upper: int) -> int:

        prefix = [0]
        last = 0
        for num in nums:
            last += num
            prefix.append(last)

        print(prefix)

        def count(pp, left, right):

            if left == right:
                return 0
            else:
                mid = (left + right) // 2

                ans = count(pp, left, mid) + count(pp, mid + 1, right)

                i1, i2, i3 = left, mid + 1, mid + 1
                while i1 <= mid:
                    while i2 <= right and pp[i2] - pp[i1] < lower:
                        i2 += 1
                    while i3 <= right and pp[i3] - pp[i1] <= upper:
                        i3 += 1
                    ans += i3 - i2
                    i1 += 1

                result = []
                i1, i2 = left, mid + 1
                while i1 <= mid and i2 <= right:
                    if pp[i1] <= pp[i2]:
                        result.append(pp[i1])
                        i1 += 1
                    else:
                        result.append(pp[i2])
                        i2 += 1
                while i1 <= mid:
                    result.append(pp[i1])
                    i1 += 1
                while i2 <= right:
                    result.append(pp[i2])
                    i2 += 1

                for i in range(len(result)):
                    pp[i + left] = result[i]

                return ans

        return count(prefix, 0, len(prefix) - 1)


```

## 答案

```python

```

## 选项

### A

```python

```

### B

```python

```

### C

```python

```