Editorial
Consider a query interval . Every contiguous group of bulbs that are on has exactly one left endpoint. Therefore, the number of groups equals the number of positions that are left endpoints of groups.
For each , define the indicator random variable to be if bulb is the left endpoint of a group and otherwise.
At the first position of the interval, the bulb to its left is ignored, so
For , bulb must be on and bulb must be off. By independence,
By linearity of expectation, the answer is
For every , precompute
and let . Then each query is answered in time as
The preprocessing takes time, all queries take time, and the memory usage is .
Because each input probability has at most two digits after the decimal point, it can be converted to an integer from to . This makes every intermediate value exact. Using double is also sufficient, but float is unsafe because the maximum answer can be about .
Solution written by GPT5.6