E. Memory and Casinos

Codeforces
IDCF712E
Time4000ms
Memory512MB
Difficulty
data structuresmathprobabilities
English · Original
Chinese · Translation
Formal · Original
There are _n_ casinos lined in a row. If Memory plays at casino _i_, he has probability _p__i_ to win and move to the casino on the right (_i_ + 1) or exit the row (if _i_ = _n_), and a probability 1 - _p__i_ to lose and move to the casino on the left (_i_ - 1) or also exit the row (if _i_ = 1). We say that Memory _dominates_ on the interval _i_... _j_ if he completes a walk such that, * He starts on casino _i_. * He never looses in casino _i_. * He finishes his walk by winning in casino _j_. Note that Memory can still walk left of the 1\-st casino and right of the casino _n_ and that always finishes the process. Now Memory has some requests, in one of the following forms: * 1 _i_ _a_ _b_: Set . * 2 _l_ _r_: Print the probability that Memory will _dominate_ on the interval _l_... _r_, i.e. compute the probability that Memory will first leave the segment _l_... _r_ after winning at casino _r_, if she starts in casino _l_. It is guaranteed that at any moment of time _p_ is a **non-decreasing sequence**, i.e. _p__i_ ≤ _p__i_ + 1 for all _i_ from 1 to _n_ - 1. Please help Memory by answering all his requests! ## Input The first line of the input contains two integers _n_ and _q_(1 ≤ _n_, _q_ ≤ 100 000), — number of casinos and number of requests respectively. The next _n_ lines each contain integers _a__i_ and _b__i_ (1 ≤ _a__i_ < _b__i_ ≤ 109) — is the probability _p__i_ of winning in casino _i_. The next _q_ lines each contain queries of one of the types specified above (1 ≤ _a_ < _b_ ≤ 109, 1 ≤ _i_ ≤ _n_, 1 ≤ _l_ ≤ _r_ ≤ _n_). It's guaranteed that there will be at least one query of type 2, i.e. the output will be non-empty. Additionally, it is guaranteed that _p_ forms a non-decreasing sequence at all times. ## Output Print a real number for every request of type 2 — the probability that boy will "dominate" on that interval. Your answer will be considered correct if its absolute error does not exceed 10 - 4. Namely: let's assume that one of your answers is _a_, and the corresponding answer of the jury is _b_. The checker program will consider your answer correct if |_a_ - _b_| ≤ 10 - 4. [samples]
有 #cf_span[n] 个赌场排成一行。如果 Memory 在赌场 #cf_span[i] 玩,他有概率 #cf_span[pi] 获胜并移动到右边的赌场 (#cf_span[i + 1]),或离开该行(如果 #cf_span[i = n]);也有概率 #cf_span[1 - pi] 失败并移动到左边的赌场 (#cf_span[i - 1]),或同样离开该行(如果 #cf_span[i = 1])。 我们说 Memory 在区间 #cf_span[i... j] 上 _支配_,如果他完成了一次行走,使得 注意,Memory 仍可以走到第 #cf_span[1] 个赌场的左侧或第 #cf_span[n] 个赌场的右侧,并且总是会结束该过程。 现在 Memory 有一些请求,形式如下: 保证在任何时刻,#cf_span[p] 是一个 *非递减序列*,即对所有 #cf_span[i] 从 #cf_span[1] 到 #cf_span[n - 1],都有 #cf_span[pi ≤ pi + 1]。 请帮助 Memory 回答所有请求! 输入的第一行包含两个整数 #cf_span[n] 和 #cf_span[q](#cf_span[1 ≤ n, q ≤ 100 000]),分别表示赌场数量和请求数量。 接下来的 #cf_span[n] 行每行包含两个整数 #cf_span[ai] 和 #cf_span[bi](#cf_span[1 ≤ ai < bi ≤ 109]),表示在赌场 #cf_span[i] 获胜的概率 #cf_span[pi]。 接下来的 #cf_span[q] 行每行包含上述类型之一的查询(#cf_span[1 ≤ a < b ≤ 109],#cf_span[1 ≤ i ≤ n],#cf_span[1 ≤ l ≤ r ≤ n])。 保证至少存在一个类型为 #cf_span[2] 的查询,即输出非空。此外,保证 #cf_span[p] 始终构成一个非递减序列。 对每个类型为 #cf_span[2] 的请求,输出一个实数——该区间上男孩“支配”的概率。你的答案若绝对误差不超过 #cf_span[10 - 4],则被视为正确。 具体而言:假设你的一个答案是 #cf_span[a],判题标准答案是 #cf_span[b],当 #cf_span[|a - b| ≤ 10 - 4] 时,判题程序将认为你的答案正确。 ## Input 输入的第一行包含两个整数 #cf_span[n] 和 #cf_span[q](#cf_span[1 ≤ n, q ≤ 100 000]),分别表示赌场数量和请求数量。接下来的 #cf_span[n] 行每行包含两个整数 #cf_span[ai] 和 #cf_span[bi](#cf_span[1 ≤ ai < bi ≤ 109]),表示在赌场 #cf_span[i] 获胜的概率 #cf_span[pi]。接下来的 #cf_span[q] 行每行包含上述类型之一的查询(#cf_span[1 ≤ a < b ≤ 109],#cf_span[1 ≤ i ≤ n],#cf_span[1 ≤ l ≤ r ≤ n])。保证至少存在一个类型为 #cf_span[2] 的查询,即输出非空。此外,保证 #cf_span[p] 始终构成一个非递减序列。 ## Output 对每个类型为 #cf_span[2] 的请求,输出一个实数——该区间上男孩“支配”的概率。你的答案若绝对误差不超过 #cf_span[10 - 4],则被视为正确。具体而言:假设你的一个答案是 #cf_span[a],判题标准答案是 #cf_span[b],当 #cf_span[|a - b| ≤ 10 - 4] 时,判题程序将认为你的答案正确。 [samples]
Let $ n $ be the number of casinos, indexed $ 1 $ to $ n $. For each casino $ i $, define the win probability as $ p_i = \frac{a_i}{b_i} $, where $ 1 \leq a_i < b_i \leq 10^9 $, and $ p_i \leq p_{i+1} $ for all $ 1 \leq i < n $. Define a random walk on the integers $ \mathbb{Z} $, starting at position $ x \in [l, r] $, with transition rules: - From state $ i \in \{2, \dots, n-1\} $: move to $ i+1 $ with probability $ p_i $, to $ i-1 $ with probability $ 1 - p_i $. - From state $ 1 $: move to $ 0 $ (left exit) with probability $ 1 - p_1 $, to $ 2 $ with probability $ p_1 $. - From state $ n $: move to $ n+1 $ (right exit) with probability $ p_n $, to $ n-1 $ with probability $ 1 - p_n $. Define the event "dominates on $ [l, r] $" as: starting at $ l $, the walk reaches $ r+1 $ before reaching $ l-1 $. Let $ f(l, r) $ denote the probability that, starting at $ l $, the walk reaches $ r+1 $ before $ l-1 $. Then, for each query of type 2 (interval $ [l, r] $), output $ f(l, r) $. For each query of type 1 (update casino $ i $ with new $ a_i, b_i $), update $ p_i \leftarrow \frac{a_i}{b_i} $, maintaining the non-decreasing property. Objective: Answer each type-2 query with $ f(l, r) $, computed via the standard gambler’s ruin formula for non-uniform probabilities on a line segment with absorbing barriers at $ l-1 $ and $ r+1 $, given $ p_i $ for $ i \in [l, r] $. --- **Formal Definition of $ f(l, r) $:** Let $ p_i \in [0,1] $ for $ i = l, l+1, \dots, r $, with $ p_i \leq p_{i+1} $. Define: $$ f(l, r) = \left(1 + \sum_{k=l}^{r} \prod_{j=l}^{k} \frac{1 - p_j}{p_j} \right)^{-1} $$ This is the probability of reaching $ r+1 $ before $ l-1 $, starting from $ l $, in a birth-death chain with state-dependent transition probabilities $ p_i $. --- **Given:** - $ n, q \in \mathbb{N} $, $ 1 \leq n, q \leq 10^5 $ - Initial $ a_i, b_i $ for $ i = 1 $ to $ n $, defining $ p_i = \frac{a_i}{b_i} $ - Updates: type 1: set $ a_i, b_i $ for some $ i $, update $ p_i $ - Queries: type 2: compute $ f(l, r) = \left(1 + \sum_{k=l}^{r} \prod_{j=l}^{k} \frac{1 - p_j}{p_j} \right)^{-1} $ **Output:** For each type-2 query, output $ f(l, r) $ with absolute error $ \leq 10^{-4} $.
Samples
Input #1
3 13
1 3
1 2
2 3
2 1 1
2 1 2
2 1 3
2 2 2
2 2 3
2 3 3
1 2 2 3
2 1 1
2 1 2
2 1 3
2 2 2
2 2 3
2 3 3
Output #1
0.3333333333
0.2000000000
0.1666666667
0.5000000000
0.4000000000
0.6666666667
0.3333333333
0.2500000000
0.2222222222
0.6666666667
0.5714285714
0.6666666667
API Response (JSON)
{
  "problem": {
    "name": "E. Memory and Casinos",
    "description": {
      "content": "There are _n_ casinos lined in a row. If Memory plays at casino _i_, he has probability _p__i_ to win and move to the casino on the right (_i_ + 1) or exit the row (if _i_ = _n_), and a probability 1 ",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 4000,
      "memory_limit": 524288
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF712E"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "There are _n_ casinos lined in a row. If Memory plays at casino _i_, he has probability _p__i_ to win and move to the casino on the right (_i_ + 1) or exit the row (if _i_ = _n_), and a probability 1 ...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "有 #cf_span[n] 个赌场排成一行。如果 Memory 在赌场 #cf_span[i] 玩,他有概率 #cf_span[pi] 获胜并移动到右边的赌场 (#cf_span[i + 1]),或离开该行(如果 #cf_span[i = n]);也有概率 #cf_span[1 - pi] 失败并移动到左边的赌场 (#cf_span[i - 1]),或同样离开该行(如果 #cf_span[i = ...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "Let $ n $ be the number of casinos, indexed $ 1 $ to $ n $. For each casino $ i $, define the win probability as $ p_i = \\frac{a_i}{b_i} $, where $ 1 \\leq a_i < b_i \\leq 10^9 $, and $ p_i \\leq p_{i+1}...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments