Subarrays with Bounded Maximum — Solution & Editorial
Count subarrays with max ≤ HI minus subarrays with max ≤ LO−1. For a bound B, subarrays with all elements ≤ B are counted by summing consecutive run lengths of qualifying elements.
Complexity: O(N)
Watch out: The count reaches ~5×109 — 64-bit.