Codeforces Round 370 (Div 2) E segment tree geometric progression interval merge Memory and casinos casino interval [l,r] L enter R first out probability __ item Bank-CF

Source: Internet
Author: User

E. Memory and casinos time limit/test 4 seconds Memory limit per test megabytes input standard input output Standa RD output

There are N casinos lined in a row. If Memory plays at casino I, him 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 with 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 of that and he starts on casino I. He never looses in casino I. He finishes him walk by winning in Casino J.

Note This Memory can still walk left of the 1-st casino and right of the casino-N and that always 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 would dominate on the interval l ... R, i.e compute the probability that Memory'll the leave l ... R after winning at Casino R, if she starts in casino L.

It is guaranteed this at any moment the 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 "I" of the input contains two integers n and q (1≤n, q≤100),-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'll be in least one query of type 2, i.e. the output would be non-empty. Additionally, it is guaranteed which p forms a non-decreasing sequence at the all times. Output

Print a real number for every request of type 2-the probability that boy would "dominate" on that interval. Your answer would be considered correct if it absolute error does not exceed 10-4.

Namely:let ' s assume this one of your answers is a, and the corresponding answer of the jury is B. The checker program would consider your answer correct if |a-b| ≤10-4. Example input

3 #
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 1 3 2 2 2 2 2 3 2 3-
3
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

#include <stdio.h> #include <iostream> #include <string.h> #include <string> #include < ctype.h> #include <math.h> #include <set> #include <map> #include <vector> #include <queue
> #include <bitset> #include <algorithm> #include <time.h> using namespace std; void Fre () {freopen ("c://test//input.in", "R", stdin); Freopen ("C://test//output.out", "w", stdout);} #define MS (x,y) Me  Mset (x,y,sizeof (x)) #define MC (x,y) memcpy (x,y,sizeof (x)) #define MP (x,y) Make_pair (x,y) #define LS o<<1 #define RS O<<1|1 #define Lson o<<1,l,mid #define RSON o<<1|1,mid+1,r #define FF #define SS second typedef l
Ong Long LL;
typedef unsigned long UL;
typedef unsigned int UI;
typedef pair<double, double> PD; Template <class T1, class t2>inline void Gmax (T1 &a, T2 b) {if (b>a) a = b;} template <class T1, class T2 >inline void Gmin (T1 &a, T2 b) {if (b<a) a = b;} const int N = 1e5 + 10, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int n, q;
PD a[1 << 18];
	PD Merge (PD X, PD y) {double L1 = x.ff;
	Double r1 = X.SS;
	Double L2 = y.ff;
	Double r2 = Y.SS;
	Double L = (L1 * L2)/(1-(1-L2) * R1);
	Double r = R2 + ((1-R2) * R1 * L2)/(1-(1-L2) * R1);
return{L, R};
		} void Build (int o, int l, int r) {if (L = = r) {double x, y;
		scanf ("%lf%lf", &x, &y);
		A[o] = {x/y, x/y};
	Return
	int mid = (L + r) >> 1;
	Build (Lson);
	Build (Rson);
A[o] = merge (A[ls], a[rs]); } PD V;
int P;
		void update (int o, int l, int r) {if (L = = r) {A[o] = V;
	Return
	int mid = (L + r) >> 1;
	if (P <= mid) update (Lson);
	else update (Rson);
A[o] = merge (A[ls], a[rs]);
int L, R;
	PD query (int o, int l, int r) {if (L >= l&&r <= R) return A[o];
	int mid = (L + r) >> 1;
	if (R <= mid) return query (Lson);
	else if (L > Mid) return query (Rson);
else return merge (query (Lson), query (Rson); int main () {while (~SCANF)("%d%d", &n, &q)
		{Build (1, 1, N);
			while (q--) {int op; scanf ("%d", &op);
				if (op = = 1) {double x, y;
				scanf ("%d%lf%lf", &p, &x, &y);
				V = {x/y,x/y};
			Update (1, 1, N);
				else {scanf ("%d%d", &l, &r);
			printf ("%.12f\n", Query (1, 1, N). ff);
}} return 0;
}/* "The" is "N (1e5) a casino, we started at Casino 1th, the probability of winning at the casino I was p[i] if the bet wins, it will enter the I+1 casino if the bet loses, will enter the first I-1 casino has M (1e5) An inquiry, asks you, regarding the inquiry interval [l,r]

If we start gambling from L position, the first time we get out of [L,r] is because R wins, not because of the probability of L losing.
"Type" segment tree geometric progression interval Merging "analysis" we ask for the interval [l,r], starting from the L position to gamble, the first step out of [L,r] because R wins rather than because of the probability of L losing.
We set it as L[l,r], obviously the first time from [l,r] The probability of the exit interval is 1-l[l,r] This problem at first thought difficult, but we can capture the shadow of the interval merge.

We consider the properties that have been up to [L,mid] and [mid+1,r] within two intervals and try to merge.
We set the L[l,mid] as L1 and set the l[mid+1,r] as L2.
So, we consider from mid->mid+1 1 times, 2 times, 3 times, ... The probability formula is L1 * L2 L1 * (1-L2) * probability of [the first time [L,mid],mid] enter [L,mid]] so we need to: for the interval [l,r], from the R position to gamble, the first step out of the [l,r] interval is because
R wins rather than because the probability of L losing is r[l,r].
Set the R[l,mid] as R1 and set the r[mid+1,r] as R2. Then the probability formula becomes--L1 * L2 L1 * (1-L2) * R1 * L2 L1 *((1-L2) * R1) ^2 * L2 ... a1=l1*l2 q= ((1-L2) *r1) infinite polynomial sum =a1/(1-q) We also request R we consider from the mid->mid+1 after 0 times, 1 times, 2 times, 3 times, ... The probability formula is R2 (1-R2) * R1 * L2 (1-R2) * R1 * (1-L2) * R1 * L2 (1-R2) * R1 * (1-L2) * R1) ^2 * L2 the same sum can then for inquiries, we can use the line segment tree

Maintain interval L and R and merge and finally get the answer. "Time Complexity && Optimization" O (QLOGN) * *


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.