#K42317. Counting Subarrays with at Least One Element Meeting a Threshold
Counting Subarrays with at Least One Element Meeting a Threshold
Counting Subarrays with at Least One Element Meeting a Threshold
You are given T test cases. For each test case, you are given an integer n representing the number of people in a queue and an integer x representing a ticket value threshold. You are also given an array of n integers denoting the ticket values for each person in the queue.
Your task is to count the number of contiguous subarrays that contain at least one element with a ticket value greater than or equal to x
.
Hint: The total number of contiguous subarrays in an array of length n is \(\frac{n(n+1)}{2}\). You can count the number of subarrays that do not contain any element \(\ge x\) (by finding consecutive segments where every element is less than \(x\) and summing up \(\frac{L(L+1)}{2}\) for each segment of length L), and subtract this from the total number of subarrays.
inputFormat
The first line of input contains an integer T
representing the number of test cases.
For each test case:
- The first line contains two space-separated integers
n
andx
wheren
is the number of elements in the array, andx
is the ticket threshold. - The second line contains
n
space-separated integers representing the ticket values.
Input is to be read from standard input (stdin).
outputFormat
For each test case, output a single integer representing the number of contiguous subarrays that contain at least one element with a ticket value \(\ge x\).
Each answer should be printed on its own line to standard output (stdout).
## sample2
5 3
1 2 3 4 5
4 10
8 5 7 11
12
4
</p>