hdu5481Desiderium+区间覆盖次数
来源:互联网 发布:蓝卡巡更软件 编辑:程序博客网 时间:2024/05/29 05:57
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 10^9+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.
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
Hint
For the second sample, the excepted length is
Source
BestCoder Round #57 (div.2)
一个含有n个区间的集合,从该集合中等概率地选取子集,求所有子集中的所有区间的构成的并集长度的和。第二个样例解释:集合中含有2个区间:一个是[0,2],编号为1,一个是[1,3],编号为2。集合的子集有4个:1、空集,集合中区间的并的长度为02、{区间1},集合中区间的并的长度为23、{区间2},集合中区间的并的长度为24、{区间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。然后把所有端点按照位置从小到大排序。从左往右扫一遍,累加端点的权值,得到的当前的权值和就是当前所在区间的重叠次数。
期望=贡献值/子集数*2^n
对于包含空集的子集=2^n;
所以答案就是贡献值,即 累加每个区间的重叠次数*区间长度,即为所求。
对于区间重叠次数。用到了一个不知道啥的东西,看到别人的题解,好厉害啊。。a[i].x~a[i+1].x的区间的重叠次数就是前i个y的累加和。(仔细想想,其实就是离散化的思想。。)
#include<cstdio>#include<cmath>#include<cstring>#include<iostream>#include<algorithm>#include<cstdlib>#include<queue>#include<vector>#include<map>#include<stack>#include<set>#define pi acos(-1.0)#define EPS 1e-6 //log(x)#define e exp(1.0); //2.718281828#define mod 1000000007#define INF 0x7fffffff#define inf 0x3f3f3f3ftypedef long long LL;using namespace std;struct Node { int l,r; bool operator <(const Node &b)const { return l<b.l; }} a[200005];LL pow2[100005];void init() { pow2[0]=1; for(int i=1; i<100005; i++) pow2[i]=(pow2[i-1]*2)%mod;}int main() { init(); int n; int t; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=0; i<2*n; i=i+2) { int l,r; scanf("%d %d",&l,&r); a[i].l=l; a[i].r=1; a[i+1].l=r; a[i+1].r=-1; } sort(a,a+2*n); /*for(int i=0;i<2*n;i++){ printf("%d %d\n",a[i].l,a[i].r); }稍微观察一下就发现巧妙啊。。6啊。。orz */ LL ans=0; int cnt=0; for(int i=0; i<2*n-1; i++) { cnt+=a[i].r; ans=(ans+(pow2[n]-pow2[n-cnt]+mod)*(a[i+1].l-a[i].l))%mod; } printf("%I64d\n",ans); } return 0;}/* _ooOoo_ o8888888o 88" . "88 (| -_- |) O\ = /O ____/`---'\____ .' \\| |// `. / \\||| : |||// \ / _||||| -:- |||||- \ | | \\\ - /// | | | \_| ''\---/'' | | \ .-\__ `-` ___/-. / ___`. .' /--.--\ `. . __ ."" '< `.___\_<|>_/___.' >'"". | | : `- \`.;`\ _ /`;.`/ - ` : | | \ \ `-. \_ __\ /__ _/ .-` / /======`-.____`-.___\_____/___.-`____.-'====== `=---='^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ I have a dream!A AC deram!! orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz*/
- hdu5481Desiderium+区间覆盖次数
- HDU5091->线段树维护区间覆盖次数&&扫描线
- HDU 3577 Fast Arrangement(线段树功能:区间更新,查询区间的最大覆盖次数)
- 区间覆盖
- 区间选点+区间覆盖
- hdu 3577 线段树,成段更新 好题 查询区间的最大覆盖次数
- Codeforces Round #419 (Div. 2) B. Karen and Coffee【前缀和求区间覆盖次数】
- 点覆盖的次数
- 取小区间覆盖
- 区间覆盖问题
- 区间覆盖问题
- poj 3277 区间覆盖
- 贪心之区间覆盖
- 区间覆盖问题
- HDU1050 区间覆盖
- 区间覆盖问题
- 贪心区间覆盖--poj2378
- 区间覆盖问题(sdut2074
- NYOJ14——贪心
- 将SQLite中的数据转换为Excel表
- 防止手机短信诈骗你能轻松应对吗
- MongoDB查询语句理解
- 基于Linux与Busybox的Reboot命令流程分析
- hdu5481Desiderium+区间覆盖次数
- Error:java.lang.UnsupportedClassVersionError: com/android/dx/command/Main : Unsupported major错误解决
- linux (%和%%)(#和##)贪婪匹配规则
- scanner 循环输入+错误继续(catch 后 continue)
- 商米V1 Unity接口开发
- 论前台资源的访问+jsp的跳转
- LeetCode OJ-190. Reverse Bits
- 有限状态机模型
- 小胖说swift05--------swift中字符串截取方法(substring)