Mike wants to prepare for IMO but he doesn't know geometry, so his teacher gave him an interesting geometry problem. Let's define f([l, r]) = r - l + 1 to be the number of integer points in the segment [l, r] with l ≤ r (say that ). You are given two integers nand k and n closed intervals [li, ri] on OX axis and you have to find:
In other words, you should find the sum of the number of integer points in the intersection of any k of the segments.
As the answer may be very large, output it modulo 1000000007 (109 + 7).
Mike can't solve this problem so he needs your help. You will help him, won't you?
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of segments and the number of segments in intersection groups respectively.
Then n lines follow, the i-th line contains two integers li, ri ( - 109 ≤ li ≤ ri ≤ 109), describing i-th segment bounds.
Print one integer number — the answer to Mike's problem modulo 1000000007 (109 + 7) in the only line.
3 2 1 2 1 3 2 3
5
3 3 1 3 1 3 1 3
3
3 1 1 2 2 3 3 4
6
In the first example:
;
;
.
So the answer is 2 + 1 + 2 = 5.
题意:给N个区间,问所有这N个区间组成的K元组区间的公共部分长度之和。
思路:每个点分开考虑,对于某个点被覆盖的次数>=k就贡献C(x, k), 数的范围太大用map。
# include <bits/stdc++.h>
# define A first
# define B second
using namespace std;
typedef long long LL;
const int maxn = 2e5;
const LL mod = 1e9+7;
LL inv[maxn+30]={1,1}, fac[maxn+30]={1,1}, fi[maxn+30]= {1,1};
void init()
{
for(int i=2; i<=maxn; ++i)
{
fac[i] = i*fac[i-1]%mod;
inv[i] = (mod-mod/i)*inv[mod%i]%mod;
fi[i] = fi[i-1]*inv[i]%mod;
}
}
LL C(LL n, LL m)
{
return fac[n]*fi[m]%mod*fi[n-m]%mod;
}
map<int,int>m;
int main()
{
init();
int n, k, a, b;
scanf("%d%d",&n,&k);
for(int i=0; i<n; ++i)
{
scanf("%d%d",&a,&b);
++m[a];--m[b+1];
}
int l = m.begin()->A;
LL sum = 0, ans = 0;
for(auto it : m)
{
int dis = it.A - l;
if(sum >= k)
{
ans += C(sum, (LL)k)*dis;
ans %= mod;
}
sum += it.B;
l = it.A;
}
return 0*printf("%I64d\n",ans);;
}