<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) {
        if (mink>maxk) return 0;
        int n=nums.size();
        long long ans=0;
        int ot=-1;
        deque<int> mn;
        deque<int> mx;
        vector<int> dp(n,0);
        for (int i=0;i<n;i++){
            if (nums[i]==mink) {
                mn.push_back(i);
                if (ot==-1) ot=i;
                if (!mx.empty()) {
                    int f=mx.front();
                    if (ot!=-1) f=min(f,ot);
                    ans+=(mx.back()-f+1);
                    dp[i]=(mx.back()-f+1);}
            }
            else if (nums[i]==maxk){
                mx.push_back(i);
                if (ot==-1) ot=i;
                if (!mn.empty()) {
                    int f=mn.front();
                    if (ot!=-1) f=min(f,ot);
                    ans+=(mn.back()-f+1);
                    dp[i]=(mn.back()-f+1);}
            }else if (nums[i]>mink && nums[i]<maxk){
                if (ot==-1) ot=i;
                // int f=ot;
                // if (!mn.empty()) f=min(mn.front(),f);
                // if (!mx.empty()) f=min(mx.front(),f);
                if (i>0) dp[i]=dp[i-1];
                ans+=dp[i];
            }else{
                if (maxk==mink && !mn.empty()){
                    long long x=mn.back()-mn.front()+1;
                    ans+=((x*(x+1))/2);
                    dp[i]=(x*(x+1))/2;
                }
                mn.clear();
                mx.clear();
                ot=-1;
            }
        }
        if (!mn.empty() && maxk==mink){
            long long x=mn.back()-mn.front()+1;
            ans+=((x*(x+1))/2);
        }
        return ans;
    }
};
88200