zoukankan      html  css  js  c++  java
  • Codeforces 712E Memory And Casinos

    Description

    (n(nle 10^5)) 个点,在 (i)(p[i]) 的概率走到 (i+1)(1-p[i]) 的概率走到 (i-1) 。有 (Q(Qle10^5)) 次操作。操作有两种:

    • 单点修改概率。
    • 询问从 (L) 走到 (R+1) ,且不经过小于 (L) 的点的概率。

    Solution

    • (f[i]) :从 (i)(R+1) 不经过小于 (i) 的点的概率。

    [f[L-1]=0,f[R+1] =1 ]

    [f[i] = p[i] imes f[i+1]+(1-p[i]) imes f[i-1] ]

    [f[i]-f[i-1]=p[i] imes(f[i+1]-f[i-1]) ]

    • (g[i]=f[i]-f[i-1])

    [g[i]=p[i] imes (g[i+1]+g[i]) ]

    [g[i+1]=cfrac{1-p[i]}{p[i]} imes g[i] ]

    • (u[i]=cfrac{1-p[i]}{p[i]})

    则有

    [sum_{i=L}^{R+1}g[i]=f[R+1]-f[L-1]=1 ]

    [g[L] imes(1+u[L]+u[L] imes u[L+1]+cdots +u[L] imes u[L+1] imes cdots imes u[R])=1 ]

    [f[L]=g[L]=cfrac{1}{1+u[L]+u[L] imes u[L+1]+cdots +u[L] imes u[L+1] imes cdots imes u[R]} ]

    • (A_{L,R}=u[L] imes u[L+1] imes cdots imes u[R])
    • (B_{L,R}=u[L]+u[L] imes u[L+1]+cdots +u[L] imes u[L+1] imes cdots imes u[R])

    [B_{L,R}=B_{L,mid}+A_{L,mid} imes B_{mid+1,R} ]

    用线段树维护 (A​)(B​) 即可。

    #include<bits/stdc++.h>
    using namespace std;
    
    template <class T> inline void read(T &x) {
    	x = 0; static char ch = getchar(); for (; ch < '0' || ch > '9'; ch = getchar());
    	for (; ch >= '0' && ch <= '9'; ch = getchar()) (x *= 10) += ch - '0';
    }
    
    #define N 100001
    #define rep(i, a, b) for (int i = a; i <= b; i++)
    #define ll long long
    
    double A[N << 2], B[N << 2];
    
    #define ls rt << 1
    #define rs ls | 1
    #define mid (l + r >> 1)
    void update(int rt, int l, int r, int pos, double val) {
    	if (l == r) { A[rt] = B[rt] = (1.0 - val) / val; return; }
    	if (pos <= mid) update(ls, l, mid, pos, val);
    	else update(rs, mid + 1, r, pos, val);
    	A[rt] = A[ls] * A[rs], B[rt] = B[ls] + A[ls] * B[rs];
    }
    
    #define pdd pair<double, double>
    pdd query(int rt, int l, int r, int L, int R) {
    	if (L <= l && r <= R) return pdd(A[rt], B[rt]);
    	if (R <= mid) return query(ls, l, mid, L, R);
    	if (L > mid) return query(rs, mid + 1, r, L, R);
    	if (L <= mid && R > mid) {
    		pdd ansl = query(ls, l, mid, L, R), ansr = query(rs, mid + 1, r, L, R);
    		return pdd(ansl.first * ansr.first, ansl.second + ansl.first * ansr.second);
    	}
    }
    
    int main() {
    	int n, Q; read(n), read(Q);
    	rep(i, 1, n) {
    		double a, b; read(a), read(b);
    		update(1, 1, n, i, a / b);
    	}
    	while (Q--) {
    		int op; read(op);
    		if (op == 1) {
    			int pos; double a, b; read(pos), read(a), read(b);
    			update(1, 1, n, pos, a / b);
    		}
    		else {
    			int l, r; read(l), read(r);
    			pdd ans = query(1, 1, n, l, r);
    			printf("%.12lf
    ", ans.second <= 2e15 ? 1.0 / (1.0 + ans.second) : 0);
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    《JavaScript 闯关记》之初探
    《JavaScript 闯关记》之简介
    《JavaScript 闯关记》
    JavaScript检测之basevalidate.js
    如何排版 微信公众号「代码块」
    android开发之路03
    android开发之路02(浅谈BroadcastReceiver)
    android开发之路01
    软件工程复习(一)
    软件工程—人件(一)
  • 原文地址:https://www.cnblogs.com/aziint/p/9172819.html
Copyright © 2011-2022 走看看