The Strange Galton Board
Time limit1sMemory limit1024 MB
A Galton board where a marble from each starting peg lands uniformly on its reachable bottom slots. Process drop queries, then answer range-sum expectation queries on the destination line.
- Level
Medium6 of 10
- Topics
- Math, Combinatorics, Prefix sum, Implementation
- Solved
- No attempts yet
Problem

Francis Galton invented the Galton board to demonstrate the central limit theorem: that for a large enough sample size, the binomial distribution approaches the normal distribution. A Galton board consists of a board with pegs set perpendicular to it. When a marble is dropped from the top, it repeatedly hits rows of pegs and bounces either left or right. Marbles that reach the bottom are separated by partitions, and an ordinary Galton board shows a normal distribution as in the figure above. In the strange Galton board of the strange land, however, a marble lands on every destination reachable from its starting point with equal probability. Because a similar number of marbles at every destination would be boring, the starting point from which the marble is dropped can be chosen.

For a strange Galton board of height 5, the numbering of starting points and destinations follows the rule shown above.
If a marble is dropped from starting point 2, it reaches every destination in [1, 5] with equal probability. If it is dropped from starting point 6, it reaches every destination in [3, 6] with equal probability.
Alice of the strange land dropped marbles onto the strange Galton board and tried to count the marbles at the destinations, but there were too many and she gave up. Instead she wants to estimate the counts by computing the expected number of marbles at the destinations. Help Alice by writing a program that outputs the expected values of the marbles.
The program performs two kinds of queries.
queries that drop marbles are given.
, : marbles are dropped from the -th starting point.
After all marbles have been dropped, queries ask for the expected number of marbles at the destinations.
: Output the expected number of marbles that land on destinations [].
The program terminates after processing all expectation queries.
Input
The first line gives the height . ()
The second line gives the number of marble-dropping queries and the number of expectation queries . (, )
Starting from the third line, queries are given. (, )
Starting from line 3 + , queries , are given. ()
Output
Output the result of each query in order, one per line.
Absolute or relative error up to is allowed.