<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 {
public:
    long long countSubarrays(vector<int>& nums, int minK, int maxK) {
        deque<int> q, p;
        int j = 0,  temp = 0;
        long long int ans = 0;
        bool left = false, right = false;
        vector<int> count(nums.size(), 0);
        vector<int> behind(nums.size(), 0);

        for (int i = 0; i < nums.size(); i++) {
            if (nums[i] < minK || nums[i] > maxK || nums[i] == minK || nums[i] == maxK) {
                count[i] = 0;
            }
            else {
                count[i] = i - 1 >=0 ? count[i-1] + 1 : 1;
            }
        }

        for (int i = nums.size() - 1; i >= 0; i--) {
            if (nums[i] < minK || nums[i] > maxK) {
                behind[i] = 0;
            }
            else {
                behind[i] = i + 1 <= nums.size() - 1 ? behind[i+1] + 1 : 1;
            }
        }
        // int ans = 0;

        for (int i = 0; i < nums.size(); i++) {
            if (nums[i] == minK) {
                left = true;
                right = false;
            }
            else if (nums[i] == maxK) {
                left = false;
                right = true;
            }
            else {
                continue;
            }
            for (int j = i; j < nums.size(); j++) {
                if (nums[j] < minK || nums[j] > maxK) {
                    left = false;
                    right = false;
                    break;
                }
                if ( (left && nums[j] == maxK) || (right && nums[j] == minK) ) {
                    ans += (( i - 1 >=0 ? count[i-1] + 1LL : 1LL) * (behind[j])) ;
                    left = false;
                    right = false;
                    break;
                }
            }
        }
        return ans;
    }
};
90000