Codeforces 712E Memory And Casinos

Description

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

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

Solution

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

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

\[f[i] = p[i]\times f[i+1]+(1-p[i])\times f[i-1] \]

\[f[i]-f[i-1]=p[i]\times(f[i+1]-f[i-1]) \]

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

\[g[i]=p[i]\times (g[i+1]+g[i]) \]

\[g[i+1]=\cfrac{1-p[i]}{p[i]} \times 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]\times(1+u[L]+u[L]\times u[L+1]+\cdots +u[L]\times u[L+1]\times \cdots \times u[R])=1 \]

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

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

\[B_{L,R}=B_{L,mid}+A_{L,mid}\times 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\n", ans.second <= 2e15 ? 1.0 / (1.0 + ans.second) : 0);
		}
	}
	return 0;
}
posted @ 2018-06-12 14:25  aziint  阅读(132)  评论(0编辑  收藏  举报
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.