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 7HintFor the second sample, the excepted length is $\frac{0+2+2+3}{4}=\frac{7}{4}$.
Source
解题思路:周六实验室聚餐,所以这一次BC只能。。。。,今天下午来了坐了一下,发现自己也只能做出前两道题了,后面两道,感觉最后一题自己可以做做,但是正确与否我也不知道。。。。
求期望,我们可以采用这样一种方式打比方对于一个区间[a,b]如果被x个大的区间覆盖,则这个区间对最终区间长度的贡献为(b-a)*(2^x-1)*(2^(n-x)),这样我们只需要计算区间覆盖的次数即可,由于题目给出的l,r范围比较大,因此我们首先应该离散化,然后进行统计,最后计算结果即可。
#include <ctime>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 110010;
const ll mod = 1000000007;
map<ll, ll> myMap;
ll l[maxn], r[maxn];
vector<ll> vec;
ll cnt[2*maxn];
ll power_mod(ll a, ll b) {
ll ret = 1;
while(b) {
if(b&1) ret = ret * a % mod;
a = a * a % mod;
b >>= 1;
}
return ret;
}
int main() {
//freopen("aa.in", "r", stdin);
int T, n;
ll ans;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
vec.clear();
myMap.clear();
ans = 0;
for(int i = 1; i <= n; ++i) {
scanf("%I64d %I64d", &l[i], &r[i]);
vec.push_back(l[i]);
vec.push_back(r[i]);
}
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
memset(cnt, 0, sizeof(cnt));
for(int i = 1; i <= n; ++i) {
int s = lower_bound(vec.begin(), vec.end(), l[i]) - vec.begin();
int e = lower_bound(vec.begin(), vec.end(), r[i]) - vec.begin();
cnt[s]++;
cnt[e]--;
}
int t_n = vec.size();
for(int i = 1; i < t_n; ++i) {
cnt[i] += cnt[i-1];
}
for(int i = 0; i < t_n; ++i) {
myMap[vec[i]] = cnt[i];
}
map<ll, ll>::iterator it = myMap.begin();
map<ll, ll>::iterator pit = it; it++;
for( ; it != myMap.end(); ++it, ++pit) {
ll x = pit->second;
ans = (ans + (((it->first - pit->first) * (power_mod(2, x) - 1)) % mod * power_mod(2, n - x)) % mod) % mod;
}
if(ans < 0) ans += mod;
printf("%I64d\n", ans);
}
return 0;
}