[CSP-S Simulation Test]: a maximum value (+ Mathematics segment tree)

Topic background

  $ Maxtir $ Favorite maximum.


Topic Portal (internal title 128)


Input Format

  The first four input lines $ 1 $ positive integer $ n, m, q $.
  $ $ 2 to $ n + 1 $ row, the first row $ i + 1 $ I $ $ input magic spar three attributes $ (x_i, y_i, p_i) $.
  Next $ Q $ lines of two positive integers $ l_i, r_i $, data guaranteed $ [l_i, r_i] $ mutually exclusive.


Output Format

  Output line a positive integer $ ans $ express answer.


Sample

Sample input:

3 3 2
1 1 500000004
2 2 333333336
3 3 1
1 2
2 3

Sample output:

4


Data range and tips

Sample explained:

$500000004\equiv\frac{1}{2}\mod(10^9+7),333333336\equiv\frac{1}{2}\mod(10^9+7)$

The final sequence of magic spar might be

$ (\ Otimes, \ otimes, 3), (\ otimes, 2,3), (1, \ otimes, 3), (1,2,3) $ are four of their probabilities is $ \ frac {1 } {3}, \ frac {1} {6}, \ frac {1} {3}, \ frac {1} {6} $

Two absorb energy are $ (0,3), (2,3), (1,3), (2,3) $, the final answer is $ 3 \ times \ frac {1} {3} +5 \ times \ frac {1} {6} +4 \ times \ frac {1} {3} +5 \ times \ frac {1} {6} = 4 $

data range:

For $ 100 \% $ data satisfies $ 0 \ leqslant y_i \ leqslant 10 ^ 9,1 \ leqslant q \ leqslant n, 0 \ leqslant p_i <10 ^ 9 + 7 $


answer

Start with $ 30 \% $ of sub-section to start ($ 10 \% $ of violence is really no technical content).

Because each magic to a minimum value, so it might be magic spar descending order.

A contribution can do magic spar if and only if it appears smaller than it and did not appear (the current magic).

Therefore, considering the probability of occurrence of such status, is seeking $ P (\ max \ limits_ {i = l} ^ rv_i \ geqslant x) $; click of formula, to give $ 1- \ prod \ limits_ {i = l } ^ r1-P (v_i \ geqslant x) $.

So we can be less than $ x $ number does not take up the option of probability minus the probability that all numbers are not selected, that is, $ P (v_i \ geqslant x) $.

Consider how to optimize the contribution of each change is the $ 1-P (v_i \ geqslant y) \ rightarrow 1-P (v_i \ geqslant x) $.

However, we can consider only $ q = \ prod \ limits_ {i = l} ^ r1-P (v_i \ geqslant x) $, so that you can $ \ Theta (\ log n) $ modified.

Think of ways to use nature interval mutually exclusive.

All intervals the left endpoint ordering, you can achieve the transformation point and interval can be optimized using the tree line.

Time complexity: $ \ Theta (m \ log q) $.

Expectations score: $ 100 $ points.

Actual score: $ 100 $ points.


Code time

#include<bits/stdc++.h>
#define L(x) x<<1
#define R(x) x<<1|1
using namespace std;
const int mod=1000000007;
struct rec{int x,y,p;}f[200001];
struct node{int nxt,to;}e[400001];
int head[200001],cnt;
int n,m,q;
you're [200001], the [200001];
long long b[200001];
long long tr[400001],lz[400001];
pair<int,int> p[200001];
long long ans;
bool cmp(rec a,rec b){return a.y<b.y;}
void add(int x,int p)
{
	e[++cnt].nxt=head[x];
	e[cnt].to=1LL*e[head[x]].to*(1-p)%mod;
	head[x]=cnt;
}
long long qpow(long long x,long long y)
{
	long long res=1;
	while(y)
	{
		if(y&1)res=res*x%mod;
		x=x*x%mod;y>>=1;
	}
	return res;
}
void pushup(int x){tr[x]=(tr[L(x)]+tr[R(x)])%mod;}
void build(int x,int l,int r)
{
	lz[x]=1;
	if(l==r){tr[x]=1;return;}
	int mid=(l+r)>>1;
	build(L(x),l,mid);
	build(R(x),mid+1,r);
	pushup(x);
}
void pushdown(int x)
{
	tr[L(x)]=lz[x]*tr[L(x)]%mod;
	tr[R(x)]=lz[x]*tr[R(x)]%mod;
	lz[L(x)]=lz[x]*lz[L(x)]%mod;
	lz[R(x)]=lz[x]*lz[R(x)]%mod;
	lz[x]=1;
}
void change(int x,int l,int r,int L,int R,int w)
{
	if(r<L||R<l)return;
	if(L<=l&&r<=R){tr[x]=tr[x]*w%mod;lz[x]=lz[x]*w%mod;return;}
	int mid=(l+r)>>1;
	pushdown(x);
	change(L(x),l,mid,L,R,w);
	change(R(x),mid+1,r,L,R,w);
	pushup(x);
}
void change(int x)
{
	if(r[x]<l[x])return;
	int res=qpow(1-(e[head[x]].to-b[x])%mod,mod-2);
	head[x]=e[head[x]].nxt;
	change(1,1,q,l[x],r[x],(1-e[head[x]].to+b[x])*res%mod);
}
int main()
{
	scanf("%d%d%d",&n,&m,&q);
	for(int i=1;i<=m;i++)scanf("%d%d%d",&f[i].x,&f[i].y,&f[i].p);
	for(int i=1;i<=q;i++)scanf("%d%d",&p[i].first,&p[i].second);
	sort(f+1,f+m+1,cmp);cnt=n;
	for(int i=1;i<=n;i++){e[i].to=1;head[i]=i;}
	for(int i=1;i<=m;i++)add(f[i].x,f[i].p);
	for(int i=1;i<=n;i++)b[i]=e[head[i]].to;
	int fl=1,fr=0;
	for(int i=1;i<=n;i++)
	{
		while(fl<=fr&&p[fl].second<i)fl++;
		while(p[fr+1].first<=i&&fr<q)fr++;
		l[i]=fl;r[i]=fr;
	}
	build(1,1,q);
	for(int i=m,j;i;i=j)
	{
		for(j=i;f[i].y==f[j].y&&j;j--)change(f[j].x);
		ans=(ans+tr[1]*(f[i].y-f[j].y))%mod;
	}
	ans=(1LL*f[m].y*q-ans)%mod;
	printf("%d\n",(ans+mod)%mod);
	return 0;
}

rp++

Guess you like

Origin www.cnblogs.com/wzc521/p/11815860.html
Recommended