CodeForces - 138C: Mushroom Gnomes - 2 (线段树&概率&排序)
One day Natalia was walking in the woods when she met a little mushroom gnome. The gnome told her the following story:
Everybody knows that the mushroom gnomes' power lies in the magic mushrooms that grow in the native woods of the gnomes. There are n trees and m magic mushrooms in the woods: the i-th tree grows at a point on a straight line with coordinates ai and has the height of hi, the j-th mushroom grows at the point with coordinates bj and has magical powers zj.
But one day wild mushroommunchers, the sworn enemies of mushroom gnomes unleashed a terrible storm on their home forest. As a result, some of the trees began to fall and crush the magic mushrooms. The supreme oracle of mushroom gnomes calculated in advance the probability for each tree that it will fall to the left, to the right or will stand on. If the tree with the coordinate x and height h falls to the left, then all the mushrooms that belong to the right-open interval [x - h, x), are destroyed. If a tree falls to the right, then the mushrooms that belong to the left-open interval (x, x + h] are destroyed. Only those mushrooms that are not hit by a single tree survive.
Knowing that all the trees fall independently of each other (i.e., all the events are mutually independent, and besides, the trees do not interfere with other trees falling in an arbitrary direction), the supreme oracle was also able to quickly calculate what would be the expectation of the total power of the mushrooms which survived after the storm. His calculations ultimately saved the mushroom gnomes from imminent death.
Natalia, as a good Olympiad programmer, got interested in this story, and she decided to come up with a way to quickly calculate the expectation of the sum of the surviving mushrooms' power.
Input
The first line contains two integers n and m (1 ≤ n ≤ 105, 1 ≤ m ≤ 104) — the number of trees and mushrooms, respectively.
Each of the next n lines contain four integers — ai, hi, li, ri (|ai| ≤ 109, 1 ≤ hi ≤ 109, 0 ≤ li, ri, li + ri ≤ 100) which represent the coordinate of the i-th tree, its height, the percentage of the probabilities that the tree falls to the left and to the right, respectively (the remaining percentage is the probability that the tree will stand on).
Each of next m lines contain two integers bj, zj (|bj| ≤ 109, 1 ≤ zj ≤ 103) which represent the coordinate and the magical power of the j-th mushroom, respectively.
An arbitrary number of trees and mushrooms can grow in one point.
Output
Print a real number — the expectation of the total magical power of the surviving mushrooms. The result is accepted with relative or absolute accuracy 10 - 4.
Examples
1 1
2 2 50 50
1 1
0.5000000000
2 1
2 2 50 50
4 2 50 50
3 1
0.2500000000
题意:现在有N棵树,M棵蘑菇,每棵树给出位置Ai,高度Hi,以及倒左边的概率Li,覆盖区间[Ai-Hi,Ai)倒右边的概率Ri,覆盖区间为(Ai,Ai+Hi],没棵蘑菇有一定的价值,如果蘑菇被一颗或者多棵树覆盖,则其价值为0,现在问最终这么蘑菇价值的期望。
思路:蘑菇之间没有影响,单独考虑其价值的贡献,很显然我们可以用线段树来表示覆盖情况,每次下推对应区间1-P的概率,代表不被其覆盖的概率。
最终每个蘑菇*不被覆盖的概率即可。现在我们用排序来做这个题:
引申:我们已经见过很多次这样的题,有多个线段[Li,Ri],现在问这些线段覆盖了多少个点,我们可以把每个线段拆成两个端点(Li,-1),(Ri+1,1),然后排序blabla。省去了用线段树模拟的过程。 此题一样可以这样搞。
把每个线段拆成左闭右开的区间,然后排序,如果遇到左端点P*=0.01*(100-p),遇到右端点则P/=0.01*(100-p);但是这样一直除或者一直乘,会误差越来越大(比如有多个左端点在0除,会一直除.....),所以我们记录每个p的个数。最后再快速幂累乘。
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<=b;i++) #define pii pair<int,int> #define mp make_pair #define pb push_back #define F first #define S second using namespace std; const int maxn=400010; pii a[maxn],b[maxn]; int tot,cnt[110]; double qpow(double a,int x){ double res=1.0; while(x){ if(x&1) res=res*a; a=a*a; x>>=1; }return res; } int main() { int N,M,i,j,A,H,L,R; scanf("%d%d",&N,&M); rep(i,1,N){ scanf("%d%d%d%d",&A,&H,&L,&R); b[++tot]=mp(A-H,L); b[++tot]=mp(A,-L); //左闭右开 b[++tot]=mp(A+1,R); b[++tot]=mp(A+H+1,-R); } rep(i,1,M) scanf("%d%d",&a[i].F,&a[i].S); sort(a+1,a+M+1); sort(b+1,b+tot+1); double ans=0; int pos=1; rep(i,1,M){ while(pos<=tot&&b[pos].F<=a[i].F){ if(b[pos].S>0) cnt[b[pos].S]++; if(b[pos].S<0) cnt[-b[pos].S]--; pos++; } if(!cnt[100]){ double p=a[i].S; rep(j,1,99) p*=qpow(0.01*(100-j),cnt[j]); ans+=p; } } printf("%.12lf\n",ans); return 0; }