<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 solve(int st, int en, vector<int> &nums, int mink, int maxk){
        if(en<=st){
            return 0;
        }
        long long ans = 0;
        int i,j;
        vector<int> v1(en-st+1,0),v2(en-st+1,0);
        for(i=st;i<en;i++){
            v1[i-st+1] = v1[i-st];
            v2[i-st+1] = v2[i-st];
            if(nums[i]==mink){
                v1[i-st+1]++;;
            }
            if(nums[i]==maxk){
                v2[i-st+1]++;
            }
        }
        // cout<<"\n";
        int n = en-st+1;
        // for(i=0;i<v1.size();i++){
        //     cout<<v1[i]<<" ";
        // }
        // cout<<"\n";
        // cout<<n<<" ";
        for(i=st;i<en;i++){
            j = i-st;
            int res1 = upper_bound(v1.begin(),v1.end(),v1[j])-v1.begin();
            int res2 = upper_bound(v2.begin(),v2.end(),v2[j])-v2.begin();
            int res = max(res1,res2);
            // cout<<res<<" ";
            ans+=max(n-res,0);
        }
        // cout<<ans<<"\n";
        return ans;
    }
    long long countSubarrays(vector<int>& nums, int minK, int maxK) {
        long long res = 0;
        int i, n = nums.size(),c=0;
        for(i=0;i<n;i++){
            if(nums[i]>maxK||nums[i]<minK){
                res += solve(i-c,i,nums,minK,maxK);
                c=0;
            }
            else{
                c++;
            }
        }
        if(c!=0){
            res += solve(i-c,i,nums,minK,maxK);
        }
        return res;
    }
};
92400