<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:        
        valid_ranges, current_range = [], []
        while nums:
            if nums[0] < minK or nums[0] > maxK:  # Reset happens.
                valid_ranges.append(current_range.copy())  # Copy to prevent modification.
                current_range.clear()
                nums.pop(0)
                continue
            
            current_range.append(nums.pop(0))
        
        valid_ranges.append(current_range)  # Don't forget last piece of range.

        total_count = 0

        range_counts = []  # In a valid range, collect subarray counts at each idx.
        min_idx_queue, max_idx_queue = [], []  # Track min & max indices in a valid range.

        while valid_ranges:
            valid_range = valid_ranges.pop(0)
            if minK == maxK:  # Special case: min = max.
                total_count += (1 + len(valid_range)) * len(valid_range) // 2
                continue
            
            for idx, num in enumerate(valid_range):
                if idx == 0:  # 1st num of valid range.
                    range_counts.append(0)
                    if num == minK:
                        min_idx_queue.append(idx)
                    if num == maxK:
                        max_idx_queue.append(idx)

                    continue
                
                range_counts.append(range_counts[-1])  # Directly inherit last count.

                if num == minK:
                    if max_idx_queue and not min_idx_queue:  # Min & max both finally occur.
                        range_counts[-1] += max_idx_queue[-1] + 1

                    if min_idx_queue and max_idx_queue:  # Min & max have already occurred.
                        if min_idx_queue[-1] < max_idx_queue[-1]:
                            range_counts[-1] += max_idx_queue[-1] - min_idx_queue[-1]
                    
                    min_idx_queue.append(idx)
                
                if num == maxK:
                    if min_idx_queue and not max_idx_queue:  # Min & max both finally occur.
                        range_counts[-1] += min_idx_queue[-1] + 1

                    if min_idx_queue and max_idx_queue:  # Min & max have already occurred.
                        if max_idx_queue[-1] < min_idx_queue[-1]:
                            range_counts[-1] += min_idx_queue[-1] - max_idx_queue[-1]
                    
                    max_idx_queue.append(idx)
                
                total_count += range_counts[-1]
            
            range_counts.clear()  # Reset for next valid range.
            min_idx_queue.clear()
            max_idx_queue.clear()
            
        return total_count

27700