Mike and Geometry Problem

题目链接:

http://acm.hust.edu.cn/vjudge/contest/121333#problem/I

Description

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 n 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

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

Hint

题意:

横轴上有n个区间,每次取其中的k个区间,记录区间交集所覆盖的整点;

问对于所有的区间取法,一共覆盖了多少次整点;

题解:

实际上先求出每个整点被多少个区间所覆盖;

假设某点被m条边覆盖,则C(m, k)即为该点一共被覆盖的次数;

(若 m < k 则说明不可能处于k个区间的交集区);

前提:离散化各点! Map[l]++; Map[r+1]--;

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 201000
#define mod 1000000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std; int n;
LL k;
map<int,int> mp; LL x,y,gcd;
void ex_gcd(LL a,LL b)
{
if(!b) {x=1;y=0;gcd=a;}
else {ex_gcd(b,a%b);LL temp=x;x=y;y=temp-a/b*y;}
}
LL f1[maxn],f2[maxn];
/*分子n!,f[i]为(i!)%mod的值*/
void F1()
{
f1[0]=1;
for(int i=1;i<maxn;i++)
f1[i]=(f1[i-1]*i)%mod;
}
/*分母m!,f[i]为(1/i!)%mod的值--逆元*/
void F2()
{
f2[0]=1;
for(int i=1;i<maxn;i++)
{
ex_gcd(i,mod);while(x<0) {x+=mod;y-=i;}
f2[i]=(f2[i-1]*(x%mod))%mod;
}
}
LL C_m_n(LL m,LL n)
{
/*ans=m!/(m-n)!n!*/
LL ans=(((f1[m]*f2[m-n])%mod)*f2[n])%mod;
return ans;
} int main(int argc, char const *argv[])
{
//IN; F1(); F2();
while(scanf("%d %I64d",&n,&k) != EOF)
{
mp.clear();
for(int i=1; i<=n; i++) {
LL x,y; scanf("%I64d %I64d", &x,&y);
mp[x]++;
mp[y+1]--;
} LL last = 0;
LL ans = 0, cur = 0;
map<int,int>::iterator it;
for(it=mp.begin(); it!=mp.end(); it++) {
LL x = it->first, y = it->second;
if(cur >= k)
ans = (ans + C_m_n(cur, k)*(x-last)) % mod;
last = x;
cur += y;
} printf("%I64d\n", ans);
} return 0;
}

CodeForces 689E Mike and Geometry Problem (离散化+组合数)的更多相关文章

  1. CodeForces 689E Mike and Geometry Problem

    离散化,树状数组,组合数学. 这题的大致思路和$HDU$ $5700$一样.都是求区间交的问题.可以用树状数组维护一下. 这题的话只要计算每一个$i$被统计了几次,假设第$i$点被统计了$ans[i] ...

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

    E. Mike and Geometry Problem 题目连接: http://www.codeforces.com/contest/689/problem/E Description Mike ...

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

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

  4. Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 【逆元求组合数 && 离散化】

    任意门:http://codeforces.com/contest/689/problem/E E. Mike and Geometry Problem time limit per test 3 s ...

  5. codeforces 689E E. Mike and Geometry Problem(组合数学)

    题目链接: E. Mike and Geometry Problem time limit per test 3 seconds memory limit per test 256 megabytes ...

  6. codeforces 361 E - Mike and Geometry Problem

    原题: Description Mike wants to prepare for IMO but he doesn't know geometry, so his teacher gave him ...

  7. Codeforces 798C. Mike and gcd problem 模拟构造 数组gcd大于1

    C. Mike and gcd problem time limit per test: 2 seconds memory limit per test: 256 megabytes input: s ...

  8. 【算法系列学习】codeforces C. Mike and gcd problem

    C. Mike and gcd problem http://www.cnblogs.com/BBBob/p/6746721.html #include<iostream> #includ ...

  9. codeforces#410C Mike and gcd problem

    题目:Mike and gcd problem 题意:给一个序列a1到an ,如果gcd(a1,a2,...an)≠1,给一种操作,可以使ai和ai+1分别变为(ai+ai+1)和(ai-ai+1); ...

随机推荐

  1. dex

    数字交叉连接设备(Dendenkosha Electronic Exchange),就是常说的电子交换器.   数字交叉连接设备完成的主要是STM-N信号的交叉连接功能,它是一个多端口器件,它实际上相 ...

  2. NDK(21)JNI的5大正确性缺陷及优化技巧(注意是正确性缺陷)

    转自 : http://www.ibm.com/developerworks/cn/java/j-jni/index.html JNI 编程缺陷可以分为两类: 性能:代码能执行所设计的功能,但运行缓慢 ...

  3. HDU 3949 XOR(高斯消元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 题意:给出一个长度为n的数列A.选出A的所有子集(除空集外)进行抑或得到2^n-1个数字,去重排 ...

  4. 基于XMPP的即时通信系统的建立(五)— openfire

    现决定使用Openfire作为服务端,Openfire采用Java开发,基于XMPP的实时开源协作服务器.单台可支持上万并发用户. Openfire体系结构 Openfire体系由其提供的服务器端.客 ...

  5. PHP运行模式的深入理解

    PHP运行模式有4钟:1)cgi 通用网关接口(Common Gateway Interface))2) fast-cgi 常驻 (long-live) 型的 CGI3) cli  命令行运行   ( ...

  6. 图表框架HelloCharts(3)饼状图

    1 效果图 2 xml文件 activity_pie_chart.xml <FrameLayout xmlns:android="http://schemas.android.com/ ...

  7. Android 开发问题集合

    1.屏幕横.竖切换 修改“AndroidManifest.xml”的android:screenOrientation 一般需要:landscape.portrait 2.修改应用名字 修改“Andr ...

  8. 海康、大华IpCamera RTSP地址和格式

    海康:rtsp://[username]:[password]@[ip]:[port]/[codec]/[channel]/[subtype]/av_stream说明:username: 用户名.例如 ...

  9. css中将div定位居中

    一直,我是认为定一个width,然后写一句margin:0 auto,就可以,但是有时也会不管用. 例如当我要定一个宽度为700的div,用相对定位定在中间.任你怎么拉伸都是居中.而position: ...

  10. Spring AOP (上)

    工作忙,时间紧,不过事情再多,学习是必须的.记得以前的部门老大说过:“开发人员不可能一天到晚只有工作,肯定是需要自我学习.第一:为了更充实自己,保持进步状态.第二:为了提升技术,提高开发能力.第三:保 ...