-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2444.cpp
More file actions
38 lines (35 loc) · 877 Bytes
/
2444.cpp
File metadata and controls
38 lines (35 loc) · 877 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3", "unroll-loops")
class Solution
{
public:
long long countSubarrays(vector<int> &nums, int minK, int maxK)
{
long long ans = 0;
int maxi = -1, mini = -1;
int s = nums.size();
for (int r = 0, l = 0; r < s; r++)
{
int x = nums[r];
if (x < minK || x > maxK)
{ // x exceeds the bound
l = r + 1; // move l to r+1
continue;
}
if (x == maxK)
maxi = r; // position for maxK
if (x == minK)
mini = r; // position for minK
ans += max((min(maxi, mini) - l + 1), 0);
}
return ans;
}
};
auto init = []()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
return 'c';
}();