CodeForces - 138C - Mushroom Gnomes - 2 - ( 线段树 + 离散化 )

题目链接:点击进入
题目

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

题意

现在有 n 棵树, m 个蘑菇,每棵树给出位置 a [ i ] ,高度 h [ i ] ,以及到左边的概率 l [ i ] ,覆盖区间 [ a [ i ] − h [ i ] , a [ i ] - 1 ] 到右边的概率 r [ i ] ,覆盖区间为 [ a [ i ] + 1 , a [ i ] + h [ i ] ] ,每个蘑菇有一定的价值,如果蘑菇被一棵或者多棵树覆盖,则其价值为0,现在问最终对蘑菇价值的总和的期望。

思路

对于每个蘑菇,考虑不被砸到的概率,价值*不被砸到的概率就是这个蘑菇的价值期望。
因为,每棵树倒下的范围为两个区间 [ a − h , a - 1 ] 与 [ a + 1 , a + h ],那么,区间内的蘑菇不被砸到的概率分别为( 1 - l * 0.01 ) 与 ( 1 - r * 0.01 ) ( l 、r 代表树向左右倒的概率,乘 0.01 是因为题目给的百分比 ) ,其实就相当于区间内的点不被砸到的概率为这些,那么对整个区间内的点都乘上不被砸到的概率,不就是区间修改🐎,可以用线段树解决。
每个蘑菇都有一个位置,就是一个点,最后查询蘑菇不被砸到的概率,就是查询点不被砸到的概率,单点查询。
但是数据范围达到了 1e9,4e9 的空间也开不了这么大。又因为每个树可以产生 4 个点,再算上每个蘑菇的点,最多 4n + m 个点,开线段树也就是 2e6 的空间,因此可以离散化一下。
最后每个蘑菇的价值乘上它不被砸到的概率的和就是最终答案。

代码
// Problem: CF138C Mushroom Gnomes - 2
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF138C
// Memory Limit: 250 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//#pragma GCC optimize(3)//O3
//#pragma GCC optimize(2)//O2
#include<iostream>
#include<string>
#include<map>
#include<set>
//#include<unordered_map>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<stack>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<fstream>
#define X first
#define Y second
#define best 131 
#define INF 0x3f3f3f3f3f3f3f3f
#define pii pair<int,int>
#define lowbit(x) x & -x
#define inf 0x3f3f3f3f
//#define int long long
//#define double long double
//#define rep(i,x,y) for(register int i = x; i <= y;++i)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double pai=acos(-1.0);
const int maxn=1e6+10;
const int mod=1e9+7;
const double eps=1e-9;
const int N=5e3+10;
/*--------------------------------------------*/
inline int read()
{
    int k = 0, f = 1 ;
    char c = getchar() ;
    while(!isdigit(c)){if(c == '-') f = -1 ;c = getchar() ;}
    while(isdigit(c)) k = (k << 1) + (k << 3) + c - 48 ,c = getchar() ;
    return k * f ;
}
/*--------------------------------------------*/

int n,m,x[maxn],cnt;
struct node
{
	int a;
	int h;
	int l;
	int r;
}p[maxn];
struct Node
{
	int pos;
	int val;
}a[maxn];
struct tree
{
	int l;
	int r;
	double lazy;
}t[maxn<<2];

void pushdown(int p)  
{
	t[p<<1].lazy=t[p<<1].lazy*t[p].lazy;
	t[p<<1|1].lazy=t[p<<1|1].lazy*t[p].lazy;
	t[p].lazy=1;
}
void build(int p,int l,int r)
{
	t[p].l=l,t[p].r=r,t[p].lazy=1;
	if(l==r)
		return ;
	int mid=l+r>>1;
	build(p<<1,l,mid);
	build(p<<1|1,mid+1,r);
}
void change(int p,int l,int r,double k)
{
	if(l<=t[p].l&&t[p].r<=r)
	{
		t[p].lazy*=k;
		return ;
	}
	pushdown(p);
	int mid=t[p].l+t[p].r>>1;
	if(l<=mid) change(p<<1,l,r,k);
	if(r>mid) change(p<<1|1,l,r,k);
}
double ask(int p,int pos)
{
	if(t[p].l==t[p].r)
		return t[p].lazy;
	pushdown(p);
	int mid=t[p].l+t[p].r>>1;
	if(pos<=mid) return ask(p<<1,pos);
	else return ask(p<<1|1,pos);
}

int main() 
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);cout.tie(0);
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d%d%d",&p[i].a,&p[i].h,&p[i].l,&p[i].r);
		x[++cnt]=p[i].a-p[i].h;
		x[++cnt]=p[i].a-1;
		x[++cnt]=p[i].a+p[i].h;
		x[++cnt]=p[i].a+1;
	}
	for(int i=1;i<=m;i++) 
		scanf("%d%d",&a[i].pos,&a[i].val),x[++cnt]=a[i].pos;
	sort(x+1,x+cnt+1);
	int len=unique(x+1,x+cnt+1)-x-1;
	build(1,1,cnt);
	for(int i=1;i<=n;i++)
	{
		int pos1=lower_bound(x+1,x+cnt+1,p[i].a-p[i].h)-x;
		int pos2=lower_bound(x+1,x+cnt+1,p[i].a-1)-x;
		int pos3=lower_bound(x+1,x+cnt+1,p[i].a+1)-x;
		int pos4=lower_bound(x+1,x+cnt+1,p[i].a+p[i].h)-x;
		change(1,pos1,pos2,(100-p[i].l)*0.01);
		change(1,pos3,pos4,(100-p[i].r)*0.01);
	}
	double ans=0;
	for(int i=1;i<=m;i++)
	{
		int pos=lower_bound(x+1,x+cnt+1,a[i].pos)-x;
		ans=ans+a[i].val*ask(1,pos);
	}
	printf("%.10lf\n",ans);
    return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值