hdu 5491 Desiderium(扫描线)

来源:互联网 发布:屏蔽视频广告软件 编辑:程序博客网 时间:2024/06/06 09:03

Desiderium

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 324    Accepted Submission(s): 130


Problem Description
There is a set of intervals, the size of this set is n.

If we select a subset of this set with equal probability, how many the expected length of intervals' union of this subset is?

We assume that the length of empty set's union is 0, and we want the answer multiply 2n modulo 109+7.
 

Input
The first line of the input is a integer T, meaning that there are T test cases.

Every test cases begin with a integer n ,which is size of set.

Then n lines follow, each contain two integers l,r describing a interval of [l,r].

1n100,000.

1,000,000,000lr1,000,000,000.
 

Output
For every test case output the answer multiply 2n modulo 109+7
 

Sample Input
210 120 21 3
 

Sample Output
17
Hint
For the second sample, the excepted length is $\frac{0+2+2+3}{4}=\frac{7}{4}$.
 

有一条数轴,还有一个区间的集合,集合大小为nn。现在等概率的从集合中选出集合的一个子集,求取出的子集的区间并集的期望长度。空集的区间并长度被认为是00

题解:扫描线的思想。


#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <string>#include <map>#include <cstdlib>#include <cmath>#include <vector>#include <set>#include <queue>using namespace std;typedef long long ll;const int maxn=2e5+10;const ll mod=1e9+7;struct node {    int l,r;} a[maxn];int X[maxn],flag[maxn];int n;ll p[maxn];void init() {    p[0]=1;    for(int i=1; i<maxn; i++)        p[i]=p[i-1]*2%mod;}bool cmp(node a,node b) {    return a.l<b.l||(a.l==b.l&&a.r<b.r);}int main() {    int t;    init();    cin>>t;    while(t--) {        scanf("%d",&n);        ll ans=0;        ll sum=0;        int m=0;        for(int i=0; i<n; i++) {            scanf("%d%d",&a[i].l,&a[i].r);            //  sum=(sum+(ll)(a[i].r-a[i].l)*p[n-1]%mod)%mod;            X[m++]=a[i].l;            X[m++]=a[i].r;        }        sort(X,X+m);        int k=0;        X[k++]=X[0]; ///去重        for(int j=1; j<m; j++)            if(X[j-1]!=X[j])X[k++]=X[j];        m=k;        sort(a,a+n,cmp);        int f=0;        memset(flag,0,sizeof flag);        for(int i=0; i<n; i++) {            int lx=lower_bound(X,X+m,a[i].l)-X;            int rx=lower_bound(X,X+m,a[i].r)-X;            flag[lx]++;            flag[rx]--;        }        for(int i=0; i<m-1; i++) {            ll len=X[i+1]-X[i];            f+=flag[i];            sum=(sum+len*(p[n-f])%mod*(p[f]-1)%mod+2*mod)%mod;        }        printf("%I64d\n",sum);    }    return 0;}


0 0
原创粉丝点击