cf 712E Memory and Casinos

时间:2021-01-01 15:07:14

题意:有一行$n(n \leq 100000)$个方格,从左往右第$i$个方格的值为$p_i(p_i = \frac{a}{b}, 1 \leq a < b \leq 1e9)$,有两种操作,一种是将某个方格的值更新为另一个分数表示的有理数,另一种操作是寻味区间$[l, r](l \leq r)$的权值$w(l, r)$;$w(l, r)$如下定义:

方格在位置$i$有$p_i$的概率向右移动一格,有$1-p_i$的概率向左移动一格。$w(l, r)$表示方格初始位置在$l$并且以在位置$r$向右移动(下一个位置为$r+1$)为终结,移动过程始终不超出区间范围的概率值。

分析:对于任一区间$[l, r]$,设$f(i)$表示目前在位置$i$,在移动合法的情况下到达终结状态的概率值。那么显然有$f(i) = p_if(i + 1) + (1 - p_i)f(i - 1)$,注意边界情况是$f(l - 1) = 0$, 且$f(r + 1) = 1$,我们设$w(l, r) = f(l) = \Delta$,那么可以得到递推关系$f(r + 1) = 1 = g(r + 1) + f(r - 1)$,其中$g(r + 1) = \frac{\prod_{i \leq r - 1}(1 - p_i)}{\prod_{i \leq r}p_i} $,理论上我们可以用$g(i)$前缀和得到任意区间的和,用线段树分别维护奇数位置和偶数位置即可。然而,由于$g(i)$可能会非常大,以至于double存储失效,因此此方法并不可行。

用分类统计的方法来解,考虑小规模问题与大规模问题之间的联系,$[l, r]$中间一任意位置为$m$,讨论方格穿过$m$的次数(等比求和),于是可以得到具有局部可累加性质的递推关系。用线段上进行点维护和区间查询即可。单次询问复杂度$O(log(n))$。

code:

 #include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <ctime>
#include <cmath>
#include <iostream>
#include <assert.h>
#pragma comment(linker, "/STACK:102400000,102400000")
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define mp std :: make_pair
#define st first
#define nd second
#define keyn (root->ch[1]->ch[0])
#define lson (u << 1)
#define rson (u << 1 | 1)
#define pii std :: pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define type(x) __typeof(x.begin())
#define foreach(i, j) for(type(j)i = j.begin(); i != j.end(); i++)
#define FOR(i, s, t) for(int i = (s); i <= (t); i++)
#define ROF(i, t, s) for(int i = (t); i >= (s); i--)
#define dbg(x) std::cout << x << std::endl
#define dbg2(x, y) std::cout << x << " " << y << std::endl
#define clr(x, i) memset(x, (i), sizeof(x))
#define maximize(x, y) x = max((x), (y))
#define minimize(x, y) x = min((x), (y))
using namespace std;
typedef long long ll;
const int int_inf = 0x3f3f3f3f;
const ll ll_inf = 0x3f3f3f3f3f3f3f3f;
const int INT_INF = (int)((1ll << ) - );
const double double_inf = 1e30;
const double eps = 1e-;
typedef unsigned long long ul;
typedef unsigned int ui;
inline int readint() {
int x;
scanf("%d", &x);
return x;
}
inline int readstr(char *s) {
scanf("%s", s);
return strlen(s);
} class cmpt {
public:
bool operator () (const int &x, const int &y) const {
return x > y;
}
}; int Rand(int x, int o) {
//if o set, return [1, x], else return [0, x - 1]
if (!x) return ;
int tem = (int)((double)rand() / RAND_MAX * x) % x;
return o ? tem + : tem;
}
ll ll_rand(ll x, int o) {
if (!x) return ;
ll tem = (ll)((double)rand() / RAND_MAX * x) % x;
return o ? tem + : tem;
} void data_gen() {
srand(time());
freopen("in.txt", "w", stdout);
int kases = ;
//printf("%d\n", kases);
while (kases--) {
ll sz = ;
printf("%d %d\n", sz, sz);
FOR(i, , sz) {
int x = Rand(1e2, );
int y = Rand(1e9, );
if (x > y) swap(x, y);
printf("%d %d\n", x, y);
}
FOR(i, , sz) {
int o = Rand(, );
if (o) {
printf("1 ");
int pos = Rand(, );
int x = Rand(1e9, ), y = Rand(1e9, );
if (x > y) swap(x, y);
printf("%d %d %d\n", pos, x, y);
} else {
printf("2 ");
int x = Rand(, ), y = Rand(1e3, );
if (x > y) swap(x, y);
printf("%d %d\n", x, y);
}
}
}
} const int maxn = 1e5 + ;
struct Seg {
double l1, l2, r1, r2;
}seg[maxn << ];
int n, q;
pii a[maxn]; void push_up(int u) {
seg[u].l2 = seg[lson].l2 * seg[rson].l2 / ( - seg[lson].r1 * seg[rson].l1);
seg[u].l1 = seg[lson].l1 + seg[lson].l2 * seg[lson].r2 * seg[rson].l1 / ( - seg[lson].r1 * seg[rson].l1);
seg[u].r1 = seg[rson].r1 + seg[rson].r2 * seg[rson].l2 * seg[lson].r1 / ( - seg[lson].r1 * seg[rson].l1);
seg[u].r2 = seg[lson].r2 * seg[rson].r2 / ( - seg[lson].r1 * seg[rson].l1);
} double query1(int u, int l, int r, int L, int R);
double query3(int u, int l, int r, int L, int R);
double query4(int u, int l, int r, int L, int R); double query(int u, int l, int r, int L, int R) {
if (l == L && R == r) return seg[u].l2;
int mid = (l + r) >> ;
if (R <= mid) return query(lson, l, mid, L, R);
else if (L >= mid) return query(rson, mid, r, L, R);
double lhs = query(lson, l, mid, L, mid), rhs = query(rson, mid, r, mid, R);
double L1 = query1(rson, mid, r, mid, R), R1 = query3(lson, l, mid, L, mid);
return lhs * rhs / (. - L1 * R1);
} double query3(int u, int l, int r, int L, int R) {
if (l == L && r == R) return seg[u].r1;
int mid = (l + r) >> ;
if (R <= mid) return query3(lson, l, mid, L, R);
else if (L >= mid) return query3(rson, mid, r, L, R);
double tem = query3(rson, mid, r, mid, R);
double R2 = query4(rson, mid, r, mid, R);
double R1 = query3(lson, l, mid, L, mid);
double L2 = query(rson, mid, r, mid, R);
double L1 = query1(rson, mid, r, mid, R);
return tem + R2 * R1 * L2 / (. - L1 * R1);
} double query4(int u, int l, int r, int L, int R) {
if (l == L && r == R) return seg[u].r2;
int mid = (l + r) >> ;
if (R <= mid) return query4(lson, l, mid, L, R);
else if (L >= mid) return query4(rson, mid, r, L, R);
double lhs = query4(lson, l, mid, L, mid) * query4(rson, mid, r, mid, R);
double rhs = query3(lson, l, mid, L, mid) * query3(rson, mid, r, mid, R);
return lhs / (. - rhs);
} double query1(int u, int l, int r, int L, int R) {
if (l == L && R == r) return seg[u].l1;
int mid = (l + r) >> ;
if (R <= mid) return query1(lson, l, mid, L, R);
else if (L >= mid) return query1(rson, mid, r, L, R);
double tem = query1(lson, l, mid, L, mid);
double L1 = query1(rson, mid, r, mid, R);
double L2 = query(lson, l, mid, L, mid);
double R2 = query4(lson, l, mid, L, mid);
double R1 = query3(lson, l, mid, L, mid);
return tem + L2 * L1 * R2 / (. - R1 * L1);
} void build(int u, int l, int r) {
if (r - l < ) {
double p = (double)a[l].first / a[l].nd;
seg[u].l1 = - p;
seg[u].l2 = p;
seg[u].r1 = p;
seg[u].r2 = - p;
return;
}
int mid = (l + r) >> ;
build(lson, l, mid), build(rson, mid, r);
push_up(u);
} void update(int u, int l, int r, int L, int R, int lhs, int rhs) {
if (l == L && r == R) {
double p = (double)lhs / rhs;
seg[u].l1 = - p;
seg[u].l2 = p;
seg[u].r1 = p;
seg[u].r2 = - p;
return;
}
int mid = (l + r) >> ;
if (R <= mid) update(lson, l, mid, L, R, lhs, rhs);
else update(rson, mid, r, L, R, lhs, rhs);
push_up(u);
} double __get(int x, int y) {
return query(, , n + , x, y + );
} void __set(int x, int y, int z) {
update(, , n + , x, x + , y, z);
} int main() {
//data_gen(); return 0;
//C(); return 0;
int debug = ;
if (debug) freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
while (~scanf("%d%d", &n, &q)) {
FOR(i, , n) scanf("%d%d", &a[i].first, &a[i].nd);
build(, , n + );
FOR(i, , q) {
int op, x, y, z;
scanf("%d%d%d", &op, &x, &y);
if (op == ) {
z = readint();
__set(x, y, z);
} else {
double ans = __get(x, y);
printf("%.10f\n", ans);
}
}
}
return ;
}