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 multiply2n 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
Input
The first line of the input is a integer T , meaning that there are T test cases.
Every test cases begin with a integern ,which is size of set.
Thenn 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
Then
Output
For every test case output the answer multiply 2n modulo 109+7 .
Sample Input
210 120 21 3
Sample Output
17HintFor the second sample, the excepted length is $\frac{0+2+2+3}{4}=\frac{7}{4}$.
有一条数轴,还有一个区间的集合,集合大小为n。现在等概率的从集合中选出集合的一个子集,求取出的子集的区间并集的期望长度。空集的区间并长度被认为是0。
题解:扫描线的思想。
#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
- hdu 5491 Desiderium(扫描线)
- hdu-5481 Desiderium
- hdu 5481 Desiderium
- HDU 5481 Desiderium
- hdu 5481 Desiderium
- hdu 5481 Desiderium(高效)
- hdu 5481 Desiderium
- HDU 5481:Desiderium(线段树)
- HDU 4007 线扫描
- HDU 3511扫描线
- HDU 1255 扫描线
- HDU 1255 扫描线
- hdu 3642(扫描线)
- hdu 3255(扫描线)
- hdu 4052(扫描线)
- HDU 4445 扫描线
- HDU 1828 (扫描线)
- hdu 5738(扫描线)
- BackboneJs入门学习[10]—View初探
- Lighttpd(linux lighttpd)中CGI执行流程
- *LeetCode-Palindrome Linked List
- [tomcat]配置images虚拟目录,并向前台返回图片路径
- 18、关机、重启和系统运行级别
- hdu 5491 Desiderium(扫描线)
- WebService之Axis2系列教程(一)Axis2的下载、安装和使用
- unity 中使用C#6.0
- CSS3 transition-timing-function 属性
- Android的ActivityNotFoundException异常
- [windows]_[避免线程死锁]
- [九度OnlineJudge][剑指Offer]题目1512:用两个栈实现队列
- 查询MySQL中某个数据库中有多少张表
- 盒子模型