cf 712E Memory and Casinos
题意:有一行$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 ;
}
cf 712E Memory and Casinos的更多相关文章
- Codeforces 712E Memory and Casinos
Description There are n casinos lined in a row. If Memory plays at casino \(i\), he has probability ...
- Codeforces Round #370 (Div. 2) E. Memory and Casinos 线段树
E. Memory and Casinos 题目连接: http://codeforces.com/contest/712/problem/E Description There are n casi ...
- Memory and Casinos CodeForces - 712E (概率,线段树)
题目链接 题目大意:$n$个点, 每个点$i$有成功率$p_i$, 若成功走到$i+1$, 否则走到走到$i-1$, 多组询问, 求从$l$出发, 在$l$处不失败, 最后在$r$处胜利的概率 设$L ...
- Codeforces Round #370 (Div. 2) E. Memory and Casinos (数学&&概率&&线段树)
题目链接: http://codeforces.com/contest/712/problem/E 题目大意: 一条直线上有n格,在第i格有pi的可能性向右走一格,1-pi的可能性向左走一格,有2中操 ...
- CF712E Memory and Casinos
设\(f[i]\)为从\(i\)到\(r+1\)且不走出区间的概率 \(f[i]=p[i]f[i+1]+(1-p[i])f[i-1]\) \(f[i]-f[i-1]=p[i](f[i+1]-f[i-1 ...
- CF712E Memory and Casinos 期望概率
题意:\(n\)个赌场,每个赌场有\(p_{i}\)的胜率,如果赢了就走到下一个赌场,输了就退回上一个赌场,规定\(1\)号赌场的上一个是\(0\)号赌场,\(n\)号赌场的下一个是\(n + 1\) ...
- 「CF712E」Memory and Casinos「线段树」「概率」
题解 解法1:(官方做法) 一段区间的\(L\)定义为从最左边开始出发,最左不失败,一直到最右边胜利的概率,\(R\)定义为从最右边开始出发,最左不失败,又回到最右边胜利的概率 考虑一个区间\([l, ...
- 【CF712E】Memory and Casinos(数学 期望 DP)
题目链接 大意 给出一个序列,当你在某个点时,有一个向右走的概率\(P_i\)(向左为\(1-P_i\)), 给出\(M\)个操作,操作有两类: 1 X Y Z:把\(P_X\)的值修改为\(\fra ...
- simotion读写CF卡,保存/读取变量
simotion读写CF卡功能 1 使用西门子的Simotion运动控制器时,有时需要用到 读/写 CF卡的功能.主要来自以下几个方面的需求. 1)用户数据量较大,可保持(retain)存储区的容量不 ...
随机推荐
- Shell 重定向
一直没搞懂 &> 和 <& 是表示什么意思. 今天自己总结一下,希望自己能理解它的真正含义. 重定向标准输入输出,切记 “1” 和 “>”之间没有空格 $ > ...
- 【LINUX】VI相关命令
任何用户最常做的事要数创建和编辑文件,包括文档.报告和文字,vi(Visual Editor)是一个有效而相对简单的全荧幕编辑,使用vi,只要记著少量基本指令,就可以开始起步,再学习其他更复杂的指令, ...
- Spring Data JPA
转自: http://www.cnblogs.com/WangJinYang/p/4257383.html Spring 框架对 JPA 的支持 Spring 框架对 JPA 提供的支持主要体现在如下 ...
- C# MDI子窗体互相操作
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- itellij idea导入web项目并部署到tomcat
概述 主要分为项目配置和tomcat配置两大步骤. 一.项目配置 打开idea,选择导入项 选择将要打开的项目路径后,继续选择项目的原本类型(后续引导设置会根据原本的项目类型更新成idea的项目),此 ...
- CS193P - 2016年秋 第三讲 Swift 语言及 Foundation 框架
这一讲介绍一些 Swift 的重点概念.特别是一些有别于其它语言的地方.但本质上还都是语法糖. 想充分理解这一讲的内容,最好的方式就是 打开 playgound,亲自动手来实验. 1,Optional ...
- swift:谈谈swift几种常见属性的区别
一.前奏 Swift作为一门新语言,经过几年的发展,逐渐趋于完善,目前已经更新到3.0版本,它汇集许多其他语言的特点,例如JS.Paython.C++等,完全区别于OC.个人感觉它没有完全的OOP和O ...
- 浅析 Magento网站建站空间的选择
对 Magento稍有了解的人都知道,作为一个功能异常强大的网络商城程序,Magento的运行对主机空间的要求是非常高的:很多 Magento建站公司都会推荐 VPS 甚至独立服务器来运行 Magen ...
- Javascript的GET、POST请求
POST.GET传输数据大小限制 HTTP协议规范没有对URL长度进行限制,也没有限制消息主体的大小,所以从理论上讲,GET.POST是没有大小限制的.那又为什么在使用过程中会有大小限制呢?? GET ...
- UIAlertController警告视图和操作表单
//创建一个myAlert1操作表单对象(UIAlertControllerStyleActionSheet为操作表单,UIAlertControllerStyleAlert为警告视图) UIAler ...