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;
    }
    
  • 相关阅读:
    POJ 3660 Cow Contest (floyd求联通关系)
    POJ 3660 Cow Contest (最短路dijkstra)
    POJ 1860 Currency Exchange (bellman-ford判负环)
    POJ 3268 Silver Cow Party (最短路dijkstra)
    POJ 1679 The Unique MST (最小生成树)
    POJ 3026 Borg Maze (最小生成树)
    HDU 4891 The Great Pan (模拟)
    HDU 4950 Monster (水题)
    URAL 2040 Palindromes and Super Abilities 2 (回文自动机)
    URAL 2037 Richness of binary words (回文子串,找规律)
  • 原文地址:https://www.cnblogs.com/aziint/p/9172819.html
Copyright © 2011-2022 走看看