Codeforces Round #370 (Div. 2) E. Memory and Casinos 线段树
E. Memory and Casinos
题目连接:
http://codeforces.com/contest/712/problem/E
Description
There are n casinos lined in a row. If Memory plays at casino i, he has probability pi to win and move to the casino on the right (i + 1) or exit the row (if i = n), and a probability 1 - pi to lose and move to the casino on the left (i - 1) or also exit the row (if i = 1).
We say that Memory dominates on the interval i... j if he completes a walk such that,
He starts on casino i.
He never looses in casino i.
He finishes his walk by winning in casino j.
Note that Memory can still walk left of the 1-st casino and right of the casino n and that always finishes the process.
Now Memory has some requests, in one of the following forms:
1 i a b: Set .
2 l r: Print the probability that Memory will dominate on the interval l... r, i.e. compute the probability that Memory will first leave the segment l... r after winning at casino r, if she starts in casino l.
It is guaranteed that at any moment of time p is a non-decreasing sequence, i.e. pi ≤ pi + 1 for all i from 1 to n - 1.
Please help Memory by answering all his requests!
Input
The first line of the input contains two integers n and q(1 ≤ n, q ≤ 100 000), — number of casinos and number of requests respectively.
The next n lines each contain integers ai and bi (1 ≤ ai < bi ≤ 109) — is the probability pi of winning in casino i.
The next q lines each contain queries of one of the types specified above (1 ≤ a < b ≤ 109, 1 ≤ i ≤ n, 1 ≤ l ≤ r ≤ n).
It's guaranteed that there will be at least one query of type 2, i.e. the output will be non-empty. Additionally, it is guaranteed that p forms a non-decreasing sequence at all times.
Output
Print a real number for every request of type 2 — the probability that boy will "dominate" on that interval. Your answer will be considered correct if its absolute error does not exceed 10 - 4.
Namely: let's assume that one of your answers is a, and the corresponding answer of the jury is b. The checker program will consider your answer correct if |a - b| ≤ 10 - 4.
Sample Input
3 13
1 3
1 2
2 3
2 1 1
2 1 2
2 1 3
2 2 2
2 2 3
2 3 3
1 2 2 3
2 1 1
2 1 2
2 1 3
2 2 2
2 2 3
2 3 3
Sample Output
0.3333333333
0.2000000000
0.1666666667
0.5000000000
0.4000000000
0.6666666667
0.3333333333
0.2500000000
0.2222222222
0.6666666667
0.5714285714
0.6666666667
Hint
题意
在第i个位置,你有pi的概率走到i+1,有(1-pi)的概率走到i-1
单点修改概率
区间查询从L开始,从R离开的概率是多少
题解:
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
typedef pair<double,double> SgTreeDataType;
struct treenode
{
int L , R ;
SgTreeDataType A;
void updata(SgTreeDataType v)
{
A=v;
}
};
pair<double,double>tmp=make_pair(1.0,0);
treenode tree[maxn*4];
inline void push_up(int o)
{
tree[o].A.first = tree[o*2].A.first*tree[o*2+1].A.first;
tree[o].A.second = tree[o*2].A.second + tree[o*2+1].A.second * tree[o*2].A.first;
}
inline void build_tree(int L , int R , int o)
{
tree[o].L = L , tree[o].R = R;
if (R > L)
{
int mid = (L+R) >> 1;
build_tree(L,mid,o*2);
build_tree(mid+1,R,o*2+1);
}
}
inline void update(int QL,int QR,SgTreeDataType v,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) tree[o].updata(v);
else
{
int mid = (L+R)>>1;
if (QL <= mid) update(QL,QR,v,o*2);
if (QR > mid) update(QL,QR,v,o*2+1);
push_up(o);
}
}
inline SgTreeDataType query(int QL,int QR,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) return tree[o].A;
else
{
int mid = (L+R)>>1;
SgTreeDataType AA=tmp,BB=tmp,CC;
if (QL <= mid) AA = query(QL,QR,2*o);
if (QR > mid) BB = query(QL,QR,2*o+1);
push_up(o);
CC.first=AA.first*BB.first;
CC.second=AA.second+BB.second*AA.first;
return CC;
}
}
int main()
{
int n,q;
scanf("%d%d",&n,&q);
build_tree(1,n,1);
for(int i=1;i<=n;i++)
{
double a,b;
cin>>a>>b;
double p = a/b;
update(i,i,make_pair((1.0-p)/p,(1.0-p)/p),1);
}
for(int i=1;i<=q;i++)
{
int op,a,b,c;
scanf("%d",&op);
if(op==1)
{
scanf("%d%d%d",&a,&b,&c);
double p = 1.0*b/(1.0*c);
pair<double,double>D=make_pair((1-p)/p,(1-p)/p);
update(a,a,D,1);
}
else
{
scanf("%d%d",&a,&b);
double p = query(a,b,1).second;
if(p<1e20)printf("%.12f\n",1.0/(1.0+p));
else printf("0.000000000000\n");
}
}
return 0;
}
Codeforces Round #370 (Div. 2) E. Memory and Casinos 线段树的更多相关文章
- Codeforces Round #370 (Div. 2) E. Memory and Casinos (数学&&概率&&线段树)
题目链接: http://codeforces.com/contest/712/problem/E 题目大意: 一条直线上有n格,在第i格有pi的可能性向右走一格,1-pi的可能性向左走一格,有2中操 ...
- Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树
题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...
- Codeforces Round #321 (Div. 2) E. Kefa and Watch 线段树hash
E. Kefa and Watch Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/prob ...
- Codeforces Round #223 (Div. 2) E. Sereja and Brackets 线段树区间合并
题目链接:http://codeforces.com/contest/381/problem/E E. Sereja and Brackets time limit per test 1 secon ...
- Codeforces Round #312 (Div. 2) E. A Simple Task 线段树+计数排序
题目链接: http://codeforces.com/problemset/problem/558/E E. A Simple Task time limit per test5 secondsme ...
- Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树
C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树扫描线
D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...
- Codeforces Round #227 (Div. 2) E. George and Cards 线段树+set
题目链接: 题目 E. George and Cards time limit per test:2 seconds memory limit per test:256 megabytes 问题描述 ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)
题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...
随机推荐
- FRM-40831 出现截断
此问题还有一种情况: 在form搜索界面使用时间范围搜索时遇到的 当在PRE-QUERY中调用如下查询时,如果CUX_SUM.PERIOD_NAME的字段长度要设置的长一些,个人建议是前面2个字段的2 ...
- sqlyong64位破解
姓名(Name):cr173 序列号(Code):8d8120df-a5c3-4989-8f47-5afc79c56e7c 或者(OR) 姓名(Name):cr173 序列号(Code):59adfd ...
- WebLogic 的一些基本概念
WebLogic 中的基本概念 上周参加了单位组织的WebLogic培训,为了便于自己记忆,培训后,整理梳理了一些WebLogic的资料,会陆续的发出来,下面是一些基本概念. Domain : 域是作 ...
- 欲实施CRM软件,必须先懂什么是CRM软件
CRM是Customer Relationship Management(客户关系管理)的缩写,它是利用信息科学技术,实现市场营销.销售.服务等活动自动化,使企业能更高效地为客户提供满意.周到的服务, ...
- [已解决]Eclipse 插件Maven在使用 add dependency,找不到包,解决办法
以Eclipse版本[Version: Luna Release (4.4.0),]为例, 依次打开:Window >show view > other > Maven Reposi ...
- Codeforces Round #231 (Div. 2) E.Lightbulb for Minister
题意:有n个点,问在一个m边形内哪个点与这n个点的距离平方和最小 题解:(ai-a0)^2=ai*ai+a0*a0-a*ai*a0 合起来就是a1*a1+...+an*an+n*a0*a0-2*a0* ...
- EasyUI关于 numberbox,combobox,validatebox 的几个小问题
在最近的项目中,首次使用到了 网页的一个布局框架——EasyUI,感觉这个框架特别牛,兼容性很不错,页面效果也挺不错,可是在使用标题上三个控件过程中遇到几个很奇特的问题,让我头疼不已,所以在此给广大I ...
- Maven学习(三) -- 仓库
标签(空格分隔): 学习笔记 坐标和依赖时任何一个构件在Maven世界中的逻辑表示方式:而构件的物理表示方式是文件,Maven通过仓库来同意管理这些文件. 任何一个构件都有其唯一的坐标,根据这个坐标可 ...
- Java 集合 - LinkedList
一.源码解析 (1). 属性 // 链表长度 transient int size = 0; // 链首和链尾 transient Node<E> first; transient Nod ...
- Logistic回归模型和Python实现
回归分析是研究变量之间定量关系的一种统计学方法,具有广泛的应用. Logistic回归模型 线性回归 先从线性回归模型开始,线性回归是最基本的回归模型,它使用线性函数描述两个变量之间的关系,将连续或离 ...