CF689E:Mike and Geometry Problem(组合数)

E. Mike and Geometry Problem
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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?

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.

Examples
input
3 2
1 2
1 3
2 3
output
5
input
3 3
1 3
1 3
1 3
output
3
input
3 1
1 2
2 3
3 4
output
6
Note

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);;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值