<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 countSubarrays(self, nums: List[int], minK: int, maxK: int) -> int:
        nums_arr = [[]]
        marks_arr = [[]]
        last_mark = None
        last_mark_count = 0
        for num in nums:
            if num < minK or num > maxK:
                if nums_arr[-1]:
                    nums_arr.append([])
                    marks_arr.append([])
                    last_mark = None
                    last_mark_count = 0
            else:
                if num == minK or num == maxK:
                    if not last_mark or num != last_mark:
                        marks_arr[-1].append(len(nums_arr[-1]))
                        last_mark = num
                        last_mark_count = 1
                    else:
                        if last_mark_count == 1:
                            marks_arr[-1].append(len(nums_arr[-1]))
                        else:
                            marks_arr[-1][-1] = len(nums_arr[-1])
                        last_mark_count += 1
                nums_arr[-1].append(num)

        ans = 0
        if minK == maxK:
            for n_arr in nums_arr:
                n = len(n_arr)
                ans += n * (n + 1) // 2
        else:
            for n_arr, mark_arr in zip(nums_arr, marks_arr):
                if len(mark_arr) < 2:
                    continue
                else:
                    mark_arr.append(len(n_arr))
                    c = 0
                    for i, j, k in zip(mark_arr, mark_arr[1:], mark_arr[2:]):
                        if n_arr[i] == n_arr[j]:
                            c += 1
                            continue
                        if k == len(n_arr) or n_arr[j] != n_arr[k]:
                            ans += (i + 1) * (k - j)
                        else:
                            ans += (i + 1) * (mark_arr[c+3] - j)
                        c += 1
        return ans




477