count
to 0, which will hold the final result.countSubarrays
that takes a bound B
and returns the number of subarrays with all elements less than or equal to B
.countSubarrays
, initialize cur
to 0, which tracks the number of valid subarrays ending at the current position.cur
when an element is less than or equal to B
, and resetting cur
to 0 when an element is greater than B
.cur
to count
for each iteration, accumulating the number of valid subarrays.countSubarrays(right) - countSubarrays(left - 1)
.