#P3772. Memory-Based Expected Wins

    ID: 17022 Type: Default 1000ms 256MiB

Memory-Based Expected Wins

Memory-Based Expected Wins

In this problem, two roommates, Little R and Little B, play a series of n games. In each game the outcome is either a win for Little R or a win for Little B.

For game 1, Little R wins with probability \(p_1\) and Little B wins with probability \(1-p_1\). For the subsequent games \(i\) (\(2 \le i \le n\)), the probability of Little R winning depends on the outcome of the previous game:

  • If Little R won game \(i-1\), then Little R wins game \(i\) with probability \(p_i\) (and Little B wins with probability \(1-p_i\)).
  • If Little B won game \(i-1\), then Little R wins game \(i\) with probability \(q_i\) (and Little B wins with probability \(1-q_i\)).

Little D sometimes observes some of the game results, and he maintains a memory (which may be incomplete or even inconsistent with the actual outcomes). At any moment, his memory consists of some recorded game outcomes. Occasionally, he either adds a new result or forgets (deletes) a previously recorded result.

Your task is: each time Little D adds or deletes an entry in his memory, compute the expected total number of wins for Little R over the n games, based solely on his remembered information. Note that if a game result is known (by his memory), then that game’s outcome is considered fixed (1 if Little R won, 0 if Little B won), regardless of the probabilities. For games whose outcomes are not in memory, the probabilities described above apply. When computing the probability for a game that is not remembered, the outcome of the previous game is taken from its probability distribution (or fixed value if it is remembered).

Input details:

The input begins with an integer n denoting the total number of games.

The second line contains n floating‐point numbers \(p_1, p_2, \dots, p_n\), where \(p_i\) is the probability that Little R wins game \(i\) if the previous game (if exists) was won by Little R (or for game 1, no dependency applies).

The third line contains n-1 floating‐point numbers \(q_2, q_3, \dots, q_n\), where \(q_i\) is the probability that Little R wins game \(i\) if the previous game was won by Little B.

The fourth line contains an integer m representing the number of memory updates.

Then m lines follow. Each line represents an update and has one of the two formats:

  • 1 i r — Little D adds (or updates) a memory record for game i with outcome r (where r is 1 if Little R won and 0 if Little B won).
  • 2 i — Little D deletes the memory record for game i.

After each update, you are to output the expected total number of wins for Little R over the n games, based on the current memory information. For games without a remembered outcome, use the probabilities and the dependency from the previous game (which might be fixed if remembered, or a probability distribution if not) to compute the expected outcome.

Note: The remembered information does not necessarily reflect the actual outcomes, and each update should be handled independently, recomputing the expectation from game 1 through game n using the current memory.

inputFormat

The first line contains an integer n (\(1 \le n \le 10^5\)), the number of games.

The second line contains n space-separated floats: \(p_1, p_2, \dots, p_n\) (each between 0 and 1), where \(p_i\) is the probability that Little R wins game \(i\) when the previous game (or no game for \(i=1\)) was won by Little R.

The third line contains n-1 space-separated floats: \(q_2, q_3, \dots, q_n\), where \(q_i\) is the probability that Little R wins game \(i\) when the previous game was won by Little B.

The fourth line contains an integer m (\(1 \le m \le 10^5\)), the number of queries.

Each of the next m lines contains a query in one of the following formats:

  • 1 i r: Add or update the remembered outcome for game i (1-indexed) to be r (where r is either 0 or 1).
  • 2 i: Delete the remembered outcome for game i.

It is guaranteed that each query is valid.

outputFormat

After each query update, print a single line containing a floating-point number representing the expected total wins for Little R over all n games, based on the current memory. Answers within an absolute or relative error of \(10^{-6}\) will be accepted.

sample

5
0.5 0.6 0.3 0.8 0.7
0.4 0.5 0.2 0.9
3
1 3 1
1 2 0
2 3
3.54

3.04 2.8

</p>