#P3772. [CTSC2017] 游戏

    ID: 2717 远端评测题 1500ms 500MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>2017线段树WC/CTSC/集训队Special JudgeO2优化

[CTSC2017] 游戏

Description

Xiao R and his roommate Xiao B are playing a game in their dorm. They play a total of nn games, and each game ends with either Xiao R winning or Xiao B winning.

In game 1, the probability that Xiao R wins is p1p_1, and the probability that Xiao B wins is 1p11 - p_1. Except for the first game, in each game the probability that Xiao R wins depends on whether Xiao R won the previous game.

Specifically:

  1. If in game i1i − 1 (1<in1 < i ≤ n) Xiao R won, then in game ii the probability that Xiao R wins is pip_i, and the probability that Xiao B wins is 1pi1 − p_i.

  2. If in game i1i − 1 (1<in1 < i ≤ n) Xiao B won, then in game ii the probability that Xiao R wins is qiq_i, and the probability that Xiao B wins is 1qi1 − q_i.

Xiao D often comes to watch Xiao R and Xiao B play, so he knows the outcomes of some games. He wants to know, under the information he currently knows, what the expected total number of games won by Xiao R is among the nn games.

Xiao D does not have a good memory: sometimes he recalls the outcome of some game and adds it to his known information; sometimes he forgets a previously remembered outcome and removes it from his known information. Your task is: whenever Xiao D adds or deletes one piece of information, compute, based on Xiao D’s current known information, the expected total number of games won by Xiao R among the nn games.

Note: If Xiao D forgets the result of a game and later recalls it again, the two remembered outcomes are not necessarily the same. You do not need to care whether his memory matches the actual situation; you only need to compute the answers according to his memory.

Input Format

The first line contains two positive integers n,mn, m and a string type\textrm{type}. They indicate that Xiao R and Xiao B play nn games in total, Xiao D performs mm operations of modifying his known information, and the data is of type type\textrm{type}. The string type\textrm{type} is provided to help obtain partial scores; you may not need this input. See [Constraints] for its meaning.

The next nn lines: the 1st line contains a real number p1p_1, meaning that in the first game the probability that Xiao R wins is p1p_1. Line ii (1<in1 < i ≤ n) contains two real numbers pi,qip_i, q_i. Here, pip_i means that if Xiao R won game i1i − 1, then in game ii the probability that Xiao R wins is pip_i; qiq_i means that if Xiao B won game i1i − 1, then in game ii the probability that Xiao R wins is qiq_i.

The next mm lines each describe one change to Xiao D’s known information. There are two types of operations.

  1. add i c means Xiao D recalls the outcome of game ii and adds it to his known information. If c=0c = 0 it means Xiao B won game ii; if c=1c = 1 it means Xiao R won game ii. It is guaranteed that i,ci, c are integers with 1in,0c11 ≤ i ≤ n, 0 ≤ c ≤ 1. If this is not the first operation, it is guaranteed that after the previous operation the known information does not contain the result of game ii.

  2. del i means Xiao D forgets the outcome of game ii and removes it from his known information. It is guaranteed that ii is an integer with 1in1 ≤ i ≤ n, and that after the previous operation the known information contains the result of game ii.

Output Format

For each operation, output one real number on a single line: after the operation, under the current known information, the expected total number of games won by Xiao R among the nn games.

3 3 A
0.3
0.5 0.2
0.9 0.8
add 1 1
add 3 0
del 1
2.350000
1.333333
0.432749

Hint

[Scoring]

Your answer is judged correct if its absolute error from the correct answer is within 10410^{-4}.

You receive full score only if all your answers are correct; otherwise you receive 00 points.

Please note the output format: output exactly one answer per line, and each answer must be a single real number. The length of each line must not exceed 5050. Wrong output formats will be judged as 00 points.

[Constraints]

For 100%100\% of the data, 1n2000001 ≤ n ≤ 200000, 1m2000001 ≤ m ≤ 200000, 0<pi,qi<10 < p_i, q_i < 1.

For 100%100\% of the data, input values have at most four decimal places.

There are 2020 test data points in total, each worth 55 points. The specific assumptions for each test point are shown in the table below:

[Xiao R teaches you math]

You may use the following formulas.

  1. Conditional probability

We write p(AB)p(A|B) to denote the probability that event AA occurs given that event BB occurs. Conditional probability can be computed by: p(AB)=p(AB)p(B)p(A|B)=\frac {p(AB)}{p(B)} where p(AB)p(AB) denotes the probability that both event BB and event AA occur, and p(B)p(B) denotes the probability that event BB occurs.

  1. Bayes’ formula (Bayes)

From conditional probability, we can derive Bayes’ formula: p(AB)=p(BA)p(A)p(B)p(A|B)=\frac {p(B|A)p(A)}{p(B)}

  1. Law of total probability

If a random variable xx has kk possible values, namely x1,x2,,xkx_1, x_2,\ldots , x_k, then p(A)=i=1kp(Ax=xi)p(x=xi)p(A)=\sum^{k}_{i=1} {p(A|x=x_i)p(x=x_i)}

[Warm reminder]

In this problem, if you want to get full score, you may need to consider errors introduced by floating-point arithmetic. Using only addition and multiplication will not introduce large errors, but please be cautious with subtraction and division.

  1. Subtracting two numbers that are close in value can introduce very large relative error.

  2. If the determinant of a matrix is very small, computing the inverse of that matrix can introduce considerable error.

Of course, even if your algorithm is mathematically correct but does not consider floating-point errors, you may still obtain partial scores.

Translated by ChatGPT 5