<p>A split of an integer array is <strong>good</strong> if:</p>

<ul>
	<li>The array is split into three <strong>non-empty</strong> contiguous subarrays - named <code>left</code>, <code>mid</code>, <code>right</code> respectively from left to right.</li>
	<li>The sum of the elements in <code>left</code> is less than or equal to the sum of the elements in <code>mid</code>, and the sum of the elements in <code>mid</code> is less than or equal to the sum of the elements in <code>right</code>.</li>
</ul>

<p>Given <code>nums</code>, an array of <strong>non-negative</strong> integers, return <em>the number of <strong>good</strong> ways to split</em> <code>nums</code>. As the number may be too large, return it <strong>modulo</strong> <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,1,1]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The only good way to split nums is [1] [1] [1].</pre>

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

<pre>
<strong>Input:</strong> nums = [1,2,2,2,5,0]
<strong>Output:</strong> 3
<strong>Explanation:</strong> There are three good ways of splitting nums:
[1] [2] [2,2,5,0]
[1] [2,2] [2,5,0]
[1,2] [2,2] [5,0]
</pre>

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

<pre>
<strong>Input:</strong> nums = [3,2,1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There is no good way to split nums.</pre>

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

<ul>
	<li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
</ul>

# TC: O(nlogn)
# SC: O(n)
class Solution:
    def waysToSplit(self, nums: List[int]) -> int:
        n = len(nums)
        prefix = [0] * n
        cur_sum = 0
        ans = 0
        for i in range(n):
            cur_sum += nums[i]
            prefix[i] = cur_sum

        for j in range(1, n - 1):
            # 对于每一次遍历 j的位置是确定的 所以右墙的位置是固定的
            # 所以prefix[j]和prefix[n-1]都是已知的
            # 那么寻找左墙的位置
            # 需要满足的条件
            # 1.    left   <=   mid
            #    prefix[i] <= prefix[j] - prefix[i]
            # -> prefix[i] <= 0.5 * prefix[j]
            # 2.    mid    <=  right
            #    prefix[j] - prefix[i] <= prefix[n - 1] - prefix[j]
            # -> prefix[i] >= 2 * prefix[j] - prefix[n-1]

            # 所以寻找prefix[i]，需要满足下面几个两个条件
            # 1. 0 <= i < j
            # 2. 2 * prfix[j] - prefix[n-1] <= prefix[i] <= 0.5 * prefix[j]
            # 现在问题就变成了 在prefix[]中搜索 (prefix是单调递增的)
            # 满足上述条件的prefix[i]的最小左边界l和prefix[i]的最大右边界r，(r-l+1)就是满足条件的i的位置的个数
            # print(f"{2 * prefix[j] - prefix[n-1]} <= prefix[i] <= {0.5 * prefix[j]}")
            left = self._searchLeft(prefix, 2 * prefix[j] - prefix[n - 1], j)
            right = self._searchRight(prefix, 0.5 * prefix[j], j)
            # print(left, right)
            if left > right: continue
            ans += right - left + 1
            ans %= (10**9 + 7)
        return ans


    def _searchLeft(self, prefix, target, j):
        left = 0
        right = j - 1
        while left < right:
            mid = left + (right - left) // 2
            if prefix[mid] >= target:
                right = mid
            else:
                left = mid + 1
        if prefix[left] < target:
            return left + 1
        return left

    def _searchRight(self, prefix, target, j):
        left = 0
        right = j - 1
        while left < right:
            mid = left + (right - left + 1) // 2
            if prefix[mid] <= target:
                left = mid
            else:
                right = mid - 1
        if prefix[right] > target:
            return right - 1
        return right
1843