E. Mike and Geometry Problem

题目连接:

http://www.codeforces.com/contest/689/problem/E

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

3 2

1 2

1 3

2 3

Sample Output

5

Hint

题意

给你n个区间,然后让你暴力的C(n,k)选择k个区间,一直选下去

然后问你这个k个区间求交集之后 ,这个交集的大小累加下来的答案是多少。

题解

考虑第i个数,如果被cnt个区间覆盖了,那么他对答案的贡献就是C(cnt,k)

那么我们把所有操作离散化之后,再O(n)的去扫一遍就好了。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+7;
const int mod = 1e9+7;
long long fac[maxn];
long long qpow(long long a,long long b)
{
long long ans=1;a%=mod;
for(long long i=b;i;i>>=1,a=a*a%mod)
if(i&1)ans=ans*a%mod;
return ans;
}
long long C(long long n,long long m)
{
if(m>n||m<0)return 0;
long long s1=fac[n],s2=fac[n-m]*fac[m]%mod;
return s1*qpow(s2,mod-2)%mod;
}
int n,k;
int l[maxn],r[maxn];
int main()
{
fac[0]=1;
for(int i=1;i<maxn;i++)
fac[i]=fac[i-1]*i%mod;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%d",&l[i]);
scanf("%d",&r[i]);
}
vector<pair<int,int> >op;
for(int i=1;i<=n;i++){
op.push_back(make_pair(l[i]-1,1));
op.push_back(make_pair(r[i],-1));
}
sort(op.begin(),op.end());
long long ans = 0;
int cnt=0;
int la=-2e9;
for(int i=0;i<op.size();i++){
ans=(ans+C(cnt,k)*(op[i].first-la))%mod;
la=op[i].first;
cnt+=op[i].second;
}
cout<<ans<<endl;
}

Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 离散化 排列组合的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem

    题目链接:传送门 题目大意:给你n个区间,求任意k个区间交所包含点的数目之和. 题目思路:将n个区间都离散化掉,然后对于一个覆盖的区间,如果覆盖数cnt>=k,则数目应该加上 区间长度*(cnt ...

  4. Codeforces Round #410 (Div. 2)C. Mike and gcd problem

    题目连接:http://codeforces.com/contest/798/problem/C C. Mike and gcd problem time limit per test 2 secon ...

  5. Codeforces Round #361 (Div. 2) C. Mike and Chocolate Thieves 二分

    C. Mike and Chocolate Thieves 题目连接: http://www.codeforces.com/contest/689/problem/C Description Bad ...

  6. Codeforces Round #361 (Div. 2) B. Mike and Shortcuts bfs

    B. Mike and Shortcuts 题目连接: http://www.codeforces.com/contest/689/problem/B Description Recently, Mi ...

  7. Codeforces Round #361 (Div. 2) A. Mike and Cellphone 水题

    A. Mike and Cellphone 题目连接: http://www.codeforces.com/contest/689/problem/A Description While swimmi ...

  8. Codeforces Round #361 (Div. 2)——B. Mike and Shortcuts(BFS+小坑)

    B. Mike and Shortcuts time limit per test 3 seconds memory limit per test 256 megabytes input standa ...

  9. Codeforces Round #361 (Div. 2)A. Mike and Cellphone

    A. Mike and Cellphone time limit per test 1 second memory limit per test 256 megabytes input standar ...

随机推荐

  1. mysql 在windons下的备份命令

    1. @echo off set "Ymd=%date:~,4%%date:~5,2%%date:~8,2%" mysqldump -uroot -proot jy510 > ...

  2. 安装Visual Studio Scrum 1.0过程模板

    近几年里,Scrum变成了相当流行的软件开发方法学.因为它轻量.可迭代且快速等优点,以致于在敏捷开发中极受欢迎.微软甚至将TFS2010自带的MSF Agile5.0过程模板做得像Scrum,开发者们 ...

  3. drop out为什么能够防止过拟合

    来源知乎: dropout 的过程好像很奇怪,为什么说它可以解决过拟合呢?(正则化) 取平均的作用: 先回到正常的模型(没有dropout),我们用相同的训练数据去训练5个不同的神经网络,一般会得到5 ...

  4. 题解 UVA10048 【Audiophobia】

    这是一道很神奇的题 什么都不卡,就卡c++11(所以评测时要换成c++). 怎么说呐,其实就是跑一个弗洛依德,求图上两点间最大权值最小的路径,输出最大权值最小. P.S.本题n很小,直接floyd变形 ...

  5. 洛谷P2822组合数问题

    传送门啦 15分暴力,但看题解说暴力分有30分. 就是找到公式,然后套公式.. #include <iostream> #include <cstdio> #include & ...

  6. 洛谷P3119 草鉴定

    这个题调了一天.. 传送门 读完题目之后我们不难想出这个题是个tarjan缩点问题,因为尽量多的经过草场,所以一号点所在的强连通分量里左右的点都是不需要在进行走逆向边,所能到达的. 然后问题就落在怎么 ...

  7. const 和 #define区别_fenglovel_新浪博客

    const 和 #define区别 (2012-12-11 14:14:07) 转载▼ 标签: 杂谈   (1) 编译器处理方式不同 define宏是在预处理阶段展开. const常量是编译运行阶段使 ...

  8. MySQL学习笔记:三种组内排序方法

    由于MySQ没有提供像Oracle的dense_rank()或者row_number() over(partition by)等函数,来实现组内排序,想实现这个功能,还是得自己想想办法,最终通过创建行 ...

  9. CCF CSP 201503-3 节日

    CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201503-3 节日 问题描述 有一类节日的日期并不是固定的,而是以“a月的第b个星期c”的形 ...

  10. require demo 记录备份

    预览地址 http://127.0.0.1:8020/requireDemo/myNEW/index.html 注意 远程的 非模块的 empty: demo2