您的位置:首页 > 其它

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

2016-07-19 10:21 351 查看
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 33 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: