<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) {
        long long ans=0;
        int j=0;
        int mn=maxK+1,mx=-1;
        if(minK==maxK){
            for(int i=0;i<nums.size();){
                int e=nums[i];
                if(e!=minK){
                    i++;
                    continue;
                }
                long long c=0;
                while(i<nums.size() && e==nums[i]){
                    c++;
                    i++;
                }
                ans+=(c*(c+1))/2;
            }
            return ans;
        }
        for(int i=0;i<nums.size();i++){
            if(nums[i]<minK)nums[i]=0;
            else if(nums[i]>maxK)nums[i]=4;
            else if(nums[i]==minK)nums[i]=1;
            else if(nums[i]==maxK)nums[i]=3;
            else nums[i]=2;
        }
        minK=1;
        maxK=3;
        vector<int> temp;
        for(int i=0;i<nums.size();i++){
            if(nums[i]==0 || nums[i]==4)temp.push_back(i);
        }
        int a=-1,b=-1;
        for(int i=0;i<nums.size();i++){
            if(nums[i]==1){
                a=i;
            }else if(nums[i]==3){
                b=i;
            }else if(nums[i]==0 || nums[i]==4){
                j=i+1;
                a=-1;
                b=-1;
            }
            if(a!=-1 && b!=-1){
                int k=max(a,b);
                int x=min(a,b);
                auto it=lower_bound(temp.begin(),temp.end(),k);
                if(it==temp.end()){
                    if(j!=0)ans+=(nums.size()-k)*(x-j+1);
                    else{
                        if(x>0)
                        ans+=(nums.size()-k)*(x-j+1);
                        else ans+=(nums.size()-k);
                    } 
                }else{
                    if(j!=0)ans+=(*it-k)*(x-j+1);
                    else{
                        if(x>0)
                        ans+=(*it-k)*(x-j+1);
                        else ans+=(*it-k);
                    }
                        
                }
                j=x+1;
                if(k==a){
                    b=-1;
                }else{
                    a=-1;
                }
            }   
        }
        // for(auto it:nums)cout<<it<<" ";
        return ans;
    }
};
83100