<p>You are given an integer array <code>nums</code> and two integers <code>minK</code> and <code>maxK</code>.</p>

<p>A <strong>fixed-bound subarray</strong> of <code>nums</code> is a subarray that satisfies the following conditions:</p>

<ul>
	<li>The <strong>minimum</strong> value in the subarray is equal to <code>minK</code>.</li>
	<li>The <strong>maximum</strong> value in the subarray is equal to <code>maxK</code>.</li>
</ul>

<p>Return <em>the <strong>number</strong> of fixed-bound subarrays</em>.</p>

<p>A <strong>subarray</strong> is a <strong>contiguous</strong> part of an array.</p>

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

<pre>
<strong>Input:</strong> nums = [1,3,5,2,7,5], minK = 1, maxK = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> The fixed-bound subarrays are [1,3,5] and [1,3,5,2].
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,1,1,1], minK = 1, maxK = 1
<strong>Output:</strong> 10
<strong>Explanation:</strong> Every subarray of nums is a fixed-bound subarray. There are 10 possible subarrays.
</pre>

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

<ul>
	<li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i], minK, maxK &lt;= 10<sup>6</sup></code></li>
</ul>

class Solution:
    def count(self,nums,l,r,minK,maxK,mink_ind,maxk_ind):
        if r-l<=1:
            return 0
        if not mink_ind or not maxk_ind:
            return 0
        mink_ind.append(10000000)
        maxk_ind.append(10000000)
        p_min=0
        p_max=0
        recent_min=-1
        recent_max=-1
        ans=0
        seg_length=r-(l+1)
        while p_min<len(mink_ind)-1 or p_max<len(maxk_ind)-1:
            if mink_ind[p_min]<maxk_ind[p_max]:
                recent_min=mink_ind[p_min]
                current_seg_end=min(mink_ind[p_min+1]-1, maxk_ind[p_max]-1,seg_length-1)
                ans+=(recent_max+1)*(current_seg_end-recent_min+1)
                p_min+=1
            elif mink_ind[p_min]>=maxk_ind[p_max]:
                recent_max=maxk_ind[p_max]
                current_seg_end=min(maxk_ind[p_max+1]-1,mink_ind[p_min]-1, seg_length-1)
                ans+=(recent_min+1)*(current_seg_end-recent_max+1)
                p_max+=1
        return ans
    def countSubarrays(self, nums: List[int], minK: int, maxK: int) -> int:
        l=-1
        ans=0
        mink_ind=[]
        maxk_ind=[]
        for i, n in enumerate(nums):
            if n<minK or n>maxK:
                ans+=self.count(nums,l,i,minK,maxK,mink_ind,maxk_ind)
                l=i
                mink_ind.clear()
                maxk_ind.clear()
            if n==minK:
                mink_ind.append(i-l-1)
            if n==maxK:
                maxk_ind.append(i-l-1)
        ans+=self.count(nums,l,len(nums),minK,maxK,mink_ind,maxk_ind)
        return ans
790