Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem (离散化)

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 andn closed intervals [li, ri] onOX axis and you have to find:

In other words, you should find the sum of the number of integer points in the intersection of anyk 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 andk (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), describingi-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个数轴上的闭区间,然后任选k个区间求交集,问你所有的这些交集的长度和为多少。


分析:离散化后直接一段一段求它对答案的贡献。


#include <cstdio>
#include <queue>    
#include <vector>    
#include <cstdio>    
#include <utility>    
#include <cstring>    
#include <iostream>    
#include <algorithm>    
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std;
int tot,n,k,l[200007],r[200007],lin[400007],rout[400007];
long long f[200007],g[400007],ans;
long long ksm(long long x,long long y)
{
	long long ans = 1;
	while(y)
	{
		if(y & 1) ans = (ans * x) % MOD;
		x = x*x % MOD;
		y>>=1;
	}
	return ans;
}
int main()
{
	cin.sync_with_stdio(false);
	cin>>n>>k;
	f[k] = 1ll;
	for(int i = k+1;i <= n;i++) f[i] = ((f[i-1]*i)% MOD)*ksm(i-k,MOD-2) % MOD;
	for(int i = 1;i <= n;i++) 
	{
		cin>>l[i]>>r[i];
		g[++tot] = l[i];
		g[++tot] = ++r[i];
	}
	sort(g+1,g+1+tot);
	for(int i = 1;i <= n;i++)
	{
		lin[lower_bound(g+1,g+1+tot,l[i])-g]++;
		rout[lower_bound(g+1,g+1+tot,r[i])-g]++;
    }
    int now = 0;
    for(int i = 1;i < tot;i++)
    {
    	now += lin[i] - rout[i];
		ans = (ans + (g[i+1]-g[i])*f[now]) % MOD; 
	}
	cout<<ans<<endl;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值