Description
Mike wants to prepare for IMO but he doesn't know geometry, so his teacher gave him an interesting geometry problem. Let's definef([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 integersn and 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?
Input
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.
Output
Print one integer number — the answer to Mike's problem modulo 1000000007 (109 + 7) in the only line.
Sample Input
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
Hint
In the first example:
;
;
.
So the answer is 2 + 1 + 2 = 5.
我们考虑每一个点的贡献
若一个点被i段区间覆盖
那么贡献就是C(k,i)
我们记录i=k到n的数量直接计算即可
对于线段统计我们可以打标记然后直接扫过去
#include<map>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
long long mod=1000000007;
struct line
{
int l;
int x;
bool operator< (line y) const
{
return l<y.l;
}
}a[500001];
int ss[200001];
long long pow1[200001],pow2[200001];
inline long long power(long long x,int y)
{
long long xt=1;
while(y!=0)
{
if(y%2==1)
xt=xt*x%mod;
x=x*x%mod;
y=y/2;
}
return xt;
}
inline long long prepare()
{
long long i;
pow1[0]=1;
pow2[0]=1;
for(i=1;i<=200000;i++)
{
pow1[i]=pow1[i-1]*i%mod;
pow2[i]=power(pow1[i],mod-2);
}
}
inline long long cale(long long k,long long n)
{
return (pow1[n]*pow2[n-k]%mod)*pow2[k]%mod;
}
int main()
{
prepare();
int n,k;
scanf("%d%d",&n,&k);
int i;
int p=0;
for(i=1;i<=n;i++)
{
int l,r;
scanf("%d%d",&l,&r);
p++;
a[p].l=l;
a[p].x=1;
p++;
a[p].l=r+1;
a[p].x=-1;
}
sort(a+1,a+1+p);
int d=1,sx=0,la=a[1].l;
while(d<=p)
{
int sum=0;
while(a[d].l==la)
{
sx+=a[d].x;
d++;
}
ss[sx]+=a[d].l-la;
la=a[d].l;
}
long long ans=0;
for(i=k;i<=n;i++)
ans=(ans+cale(k,i)*(long long)ss[i]%mod)%mod;
printf("%I64d\n",ans);
return 0;
}