CF712E [Memort and Casinos]
题意
每次询问一段区间[l,r],求从最左边走到最右边(r+1)的概率(若走到l-1,则GG了),每个点上写有向右走的概率。支持单点修改。
思考
若只查询一次,那只要知道每个点在不走到l-1的情况下,向右移动一格的概率就行了,最后乘起来就是答案。
但我们忽略了一件事情,若从一个区间的某一点出发,从左边走出去和右边走出去的概率和为1(不可能停在中间),于是我们要设计一个状态,并能够合并,这样才有可能优化复杂度。
设 [l,r] 区间的L为从第l个点(最左边)出发,在右边走出去的概率,R为第R个点出发,在左边走出去的概率。
[L1 | --> | R1] | [L2 | --> | R2] |
---|---|---|---|---|---|
[ L | -- | -- | -- | --> | R ] |
请记住,他们每个的实际意义。因为每个点要么从左边走出去,要么从右边走出去,所(1-L1)的实际意义就是从(左边出发,不从右边走出),即(从左边出发,从左边走出去的概率)。
那么若有两个连续的区间,合并成一个新区间会有怎样的答案? 设左右两个区间的答案分别为L1,R1,L2,R2,则:
L = L1*L2 走到右边还要再走到更右边
+ L1(1-L2)(1-R1)*L2 第一次向右边走失败了,再退回来再向右走
+ L1(1-L2)(1-R1)(1-L2)(1-R1)*L2 反反复复.......
+.......
整理一下,
L = L1L2+L1L2(1-L2)(1-R1)+L1L2(1-L2)2+.......
(1-L2)(1-R1)L = L1L2(1-L2)(1-R1)+L1L2(1-L2)2+.......
作差,
L(1-(1-L2)(1-R1))=L1L2
L=L1L2/(1-(1-L2)(1-R1))
R也可以类似地得到,但注意它们的和不一定为1,因为是两个不同的端点。
简单地线段树维护。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=1E5+;
int n,m;
double x,y,a[maxn];
struct tree{int l,r;double L,R;}t[maxn*];
tree merge(tree x,tree y)
{
tree z;
z.l=x.l;z.r=y.r;
z.L=x.L*y.L/(-(-y.L)*(-x.R));
z.R=x.R*y.R/(-(-y.L)*(-x.R));//不要认为反一反就对了QAQ
return z;
}
void build(int l,int r,int num)
{
t[num].l=l,t[num].r=r;
if(l==r)
{
t[num].L=a[l];
t[num].R=-a[l];
return;
}
int mid=(l+r)>>;
build(l,mid,num*);build(mid+,r,num*+);
t[num]=merge(t[num*],t[num*+]);
}
void change(int pos,double val,int num)
{
if(t[num].l==t[num].r)
{
t[num].L=val;
t[num].R=-val;
return;
}
int mid=(t[num].l+t[num].r)>>;
if(pos<=mid)change(pos,val,num*);
else change(pos,val,num*+);
t[num]=merge(t[num*],t[num*+]);
}
tree ask(int L,int R,int num)
{
if(L<=t[num].l&&t[num].r<=R)return t[num];
int mid=(t[num].l+t[num].r)>>;
if(R<=mid)return ask(L,R,num*);
else if(mid<L)return ask(L,R,num*+);
else return merge(ask(L,R,num*),ask(L,R,num*+));
}
int main()
{
ios::sync_with_stdio(false);
cin>>n>>m;
for(int i=;i<=n;++i)
{
cin>>x>>y;
a[i]=x/y;
}
build(,n,);
while(m--)
{
int opt,d;
cin>>opt;
if(opt==)
{
cin>>d>>x>>y;
change(d,x/y,);
}
else
{
int l,r;
cin>>l>>r;
cout<<fixed<<setprecision()<<ask(l,r,).L<<endl;
}
}
return ;
}
CF712E [Memort and Casinos]的更多相关文章
- 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 ...
- 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 ...
- Codeforces 712E Memory and Casinos
Description There are n casinos lined in a row. If Memory plays at casino \(i\), he has probability ...
- cf 712E Memory and Casinos
题意:有一行$n(n \leq 100000)$个方格,从左往右第$i$个方格的值为$p_i(p_i = \frac{a}{b}, 1 \leq a < b \leq 1e9)$,有两种操作,一 ...
- Codeforces Round #370 (Div. 2) E. Memory and Casinos (数学&&概率&&线段树)
题目链接: http://codeforces.com/contest/712/problem/E 题目大意: 一条直线上有n格,在第i格有pi的可能性向右走一格,1-pi的可能性向左走一格,有2中操 ...
- 挖坑:CF712E
#include<cstdio> #include<cstring> #include<algorithm> #define maxn 1000005 using ...
随机推荐
- 目前用到的一些os.path方法
这里主要记录下os.path.join()的用法 目录结构如下 在readconfig.py中进行试验,如下 1.使用os.path.realpath(__file__)获取文件所在目录 import ...
- python+selenium基础之XPATH定位(第一篇)
世界上最远的距离大概就是明明看到一个页面元素矗在那里,但是我却定位不到!! selenium定位元素的方法有很多种,像是通过id.name.class_name.tag_name.link_text等 ...
- php中文件操作常用函数有哪些
php中文件操作常用函数有哪些 一.总结 一句话总结:读写文件函数 判断文件或者目录是否存在函数 创建目录函数 file_exists() mkdir() file_get_content() fil ...
- You Don't Know JS: Scope & Closures (第2章: Lexical Scope)
2种主要的models for how scope work. 最普遍的是Lexical Scope. 另一种 Dynamic Scope.(在Appendix a中介绍.和Lexical Scope ...
- hadoop常见面试题
Q1.什么是 Hadoop? Hadoop 是一个开源软件框架,用于存储大量数据,并发处理/查询在具有多个商用硬件(即低成本硬件)节点的集群上的那些数据.总之,Hadoop 包括以下内容: HDFS( ...
- linux创建定时任务,定时执行sql
终于弄清楚一个问题了.linux创建定时任务,定时执行sql,其中分为两个case. case-1 sql语句较少,因此直接在 shell脚本中 写sql语句.如下: [oracle@Oracle11 ...
- Session重点整理
首先明确几个概念 (1)JSessionID:通过tomcat运行的Java项目,为新用户生成的随机字符串.(应该是tomcat设置的,我没试过别的服务器,如有错误请指正) (2)Session请求( ...
- 网络编程socketserver实现并发
import socketserver import struct import json import os class FtpServer(socketserver.BaseRequestHand ...
- Redis php常用操作
$redis = new redis(); //连接 $redis->connect('127.0.0.1',6379); // //设置值 $result = $redis->set(' ...
- rsyslog的配置文件使用方法
参考地址: http://www.rsyslog.com/doc/v8-stable/configuration/property_replacer.html rsyslog消息模板的定义规则 &qu ...