hdu 5481||bestcoder #57 div 2 C Desiderium ()
Desiderium
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 427 Accepted Submission(s): 167
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.
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].
1≤n≤100,000.
−1,000,000,000≤l≤r≤1,000,000,000.
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].
1≤n≤100,000.
−1,000,000,000≤l≤r≤1,000,000,000.
Output
For every test case output the answer multiply 2n modulo 109+7.
Sample Input
2
1
0 1
2
0 2
1 3
Sample Output
1 7
View Code
Hint
For the second sample, the excepted length is $\frac{0+2+2+3}{4}=\frac{7}{4}$.比赛的时候并没有做出来...sad..
因为b题调了太久...这题基本就没时间了...果然还是太弱了QAQ
然后发现有时间也做不出...
题意真是好难懂...
转载一段题解:
一个含有n个区间的集合,从该集合中等概率地选取子集,求所有子集中的所有区间的构成的并集长度的和。
第二个样例解释:
集合中含有2个区间:一个是[0,2],编号为1,一个是[1,3],编号为2。
集合的子集有4个:
1、空集,集合中区间的并的长度为0
2、{区间1},集合中区间的并的长度为2
3、{区间2},集合中区间的并的长度为2
4、{区间1、区间2},集合中区间的并为[0,3],长度为3
考虑某一个区间对于答案的贡献:
若某个区间没有被其它区间覆盖,则该区间在子集中出现的总次数为:2^(n-1)次(总子集数-去掉该区间的子集总数)
若某个区间被覆盖了1次,即有2个区间重叠,那么该区间在子集中出现总次数为:2^(n-1)+2^(n-2)次(第1个区间在子集中出现的次数+第2个区间在子集中出现且第1个区间不出现的总次数)
…………
可以得到
若该区间被覆盖了k(k>=0)次,即有k+1个区间重叠,得到该区间在所有子集中出现的总次数:2^(n-1)+2^(n-2)+……+2^(n-k-1)
如何统计每一个区间的重叠次数?
对于输入的每一个区间的两个端点,给一个权值,左端点为1,右端点为-1。然后把所有端点按照位置从小到大排序。从左往右扫一遍,累加端点的权值,得到的当前的权值和就是当前所在区间的重叠次数。
累加每个区间的重叠次数*区间长度,即为所求。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /************************************************************************* 2 > File Name: code/bc/#57/C.cpp 3 > Author: 111qqz 4 > Email: rkz2013@126.com 5 > Created Time: 2015年09月30日 星期三 11时08分35秒 6 ************************************************************************/ 7 8 #include<iostream> 9 #include<iomanip> 10 #include<cstdio> 11 #include<algorithm> 12 #include<cmath> 13 #include<cstring> 14 #include<string> 15 #include<map> 16 #include<set> 17 #include<queue> 18 #include<vector> 19 #include<stack> 20 #include<cctype> 21 #define y1 hust111qqz 22 #define yn hez111qqz 23 #define j1 cute111qqz 24 #define ms(a,x) memset(a,x,sizeof(a)) 25 #define lr dying111qqz 26 using namespace std; 27 #define For(i, n) for (int i=0;i<int(n);++i) 28 typedef long long LL; 29 typedef double DB; 30 const int inf = 0x3f3f3f3f; 31 const int N=1E5+7; 32 const int mod = 1E9+7; 33 struct P 34 { 35 int x,y; 36 }a[N<<1]; 37 LL ans; 38 int n; 39 LL sum[N],p[N]; 40 bool cmp(P a,P b) {return a.x<b.x;} 41 42 void init() { p[0] = 1;for (int i = 1 ; i < N ; i ++) p[i] = p[i-1]*2 %mod;} 43 int main() 44 { 45 #ifndef ONLINE_JUDGE 46 freopen("in.txt","r",stdin); 47 #endif 48 49 init(); 50 int T; 51 scanf("%d",&T); 52 while(T--) 53 { 54 scanf("%d",&n); 55 for(int i = 1 ; i <= 2*n ; i+=2) 56 { 57 scanf("%d %d",&a[i].x,&a[i+1].x); 58 a[i].y=1; 59 a[i+1].y=-1; 60 } 61 62 sum[1]=p[n-1]; 63 for(int i = 2 ; i <= n ; i++ ) sum[i]=(sum[i-1]+p[n-i])%mod; 64 sort(a+1,a+2*n+1,cmp); 65 int ans=0; 66 int cnt=0; 67 for(int i = 1 ; i < 2*n ;i++) 68 { 69 cnt = cnt + a[i].y; 70 ans=(ans+sum[cnt]*(a[i+1].x-a[i].x)%mod)%mod; 71 } 72 printf("%d\n",ans); 73 } 74 75 76 #ifndef ONLINE_JUDGE 77 fclose(stdin); 78 #endif 79 return 0; 80 }