若 [i, j] 满足, 则 [i, j+1], [i, j+2]...[i,n]均满足
故设当前区间里个数为size, 对于每个 i ,找到刚满足 size == k 的 [i, j], ans += n - j + 1 .
i++ 的时候看看需不需要size-- 就可以更新了。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 #define LL long long 6 const int N = 200005; 7 int t, n, m, k; 8 int a[N]; 9 LL ans; 10 int main() 11 { 12 scanf("%d", &t); 13 while (t--) 14 { 15 scanf("%d%d%d", &n, &m, &k); 16 for (int i = 1; i <= n; i++) 17 scanf("%d", &a[i]); 18 ans = 0; 19 int j = 1, i = 1, size = 0; 20 while (i <= n && j <= n) 21 { 22 if (size < k) 23 { 24 if (a[j] >= m) size++; 25 j++; 26 } 27 while (size == k) 28 { 29 ans += n - (j-1) + 1; 30 if (a[i] >= m) size--; 31 i++; 32 } 33 } 34 printf("%I64d ",ans); 35 } 36 }