任意门:http://codeforces.com/contest/689/problem/E

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

Copy
3 2
1 2
1 3
2 3
output

Copy
5
input

Copy
3 3
1 3
1 3
1 3
output

Copy
3
input

Copy
3 1
1 2
2 3
3 4
output

Copy
6
Note

In the first example:

;

;

.

So the answer is 2 + 1 + 2 = 5.

大概题意:

有 N 个区间, 从其中取 K 个区间。所以有 C(N, K)种组合, 求每种组合区间交集长度的总和。

解题思路:

丢开区间的角度,从每个结点的角度来看,其实每个结点的贡献是 C(cnt, K) cnt 为该结点出现的次数, 所以只要O(N)扫一遍统计每个结点的贡献就是答案。

思路清晰,但考虑到数据的规模,这里需要注意和需要用到两个技巧:

一是离散化,这里STL里的 vector 和 pair 结合用,结合区间加法的思想进行离散化。

二是求组合数时 除数太大,考虑到精度问题需要用逆元来计算。

AC code:

 #include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+;
const int mod = 1e9+;
long long fac[maxn]; long long qpow(long long a,long long b) //快速幂
{
long long ans=;a%=mod;
for(long long i=b;i;i>>=,a=a*a%mod)
if(i&)ans=ans*a%mod;
return ans;
} long long C(long long n,long long m) //计算组合数
{
if(m>n||m<)return ;
long long s1=fac[n], s2=fac[n-m]*fac[m]%mod; //除数太大,逆元处理
return s1*qpow(s2,mod-)%mod;
}
int n,k;
int l[maxn],r[maxn]; //左端点, 右端点
int main()
{
fac[]=;
for(int i=;i<maxn;i++) //预处理全排列
fac[i]=fac[i-]*i%mod; scanf("%d%d",&n,&k);
for(int i=;i<=n;i++){
scanf("%d",&l[i]);
scanf("%d",&r[i]);
}
vector<pair<int,int> >op;
for(int i=;i<=n;i++){ //离散化
op.push_back(make_pair(l[i]-,)); //区间加法标记
op.push_back(make_pair(r[i],-));
}
sort(op.begin(),op.end()); //升序排序
long long ans = ; //初始化
int cnt=;
int la=-2e9;
for(int i=;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 题目连接: http://www.codeforces.com/contest/689/problem/E Description Mike ...

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

  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. 切换myEclipse工作空间后设置,myEclipse添加注释/设置豆沙背景颜色/调节字体大小

    一.添加注释 操作位置: 注释规范 Files/** * @文件名称: ${file_name} * @文件路径: ${package_name} * @功能描述: ${todo} * @作者: ${ ...

  2. TeamCity 持续集成工具

    https://www.jetbrains.com/teamcity/ null

  3. 你的UI设计够不够趣味性

    这周要做一个设计,在研究怎么修改的时候,想到了要加入一些符合产品调性的趣味设计,但是要怎么加入才能增加用户对产品的印象,进而增加好感与认可呢,我们今天就来研究一下很多有意思的APP里的一些趣味设计. ...

  4. python 爬虫系列03--职位爬虫

    职位爬虫 import requests from lxml import etree cookie = { 'Cookie':'user_trace_token=20181015184304-692 ...

  5. 安装Newton版Swift,配合keystone认证

    主控节点安装配置 Keystone 认证服务 主控节点 IP:192.168.81.11 yum install centos-release-openstack-newton -y yum upda ...

  6. Tomcat配置自定义访问日志 --- 获取请求头部信息

    使用tomcat,搭建完个人网站后,默认记录来访游客的信息是十分有限的,主要有ip和路径以及方法等. 有时候为了获取更多来访信息,比如请求的头部信息,这个时候就需要我们手动配置log了. 开始 进入T ...

  7. 防盗链与springboot代理模式(图片文件转发)

    在搭建自己的博客网站的时候,很有可能要引入一些外部图片,毕竟多数人最开始不是在自己的平台上写博客. 因某种需要,搬运自己以前写的博客到自己的网站时,在图片这一步可能会出现问题,无法显示.其中往往就是防 ...

  8. JQuery脚本-通过禁用按钮防止表单重复提交

    <script type="text/javascript"> /* jquer 脚本,避免重复提交 隐藏点击的submit,后在他之后插入同名button伪装成被隐藏 ...

  9. centos 6.5搭建LNMP环境

    1:查看环境: 1 2 [root@10-4-14-168 html]# cat /etc/redhat-release CentOS release 6.5 (Final) 2:关掉防火墙 1 [r ...

  10. django(6)model表语句操作、Form操作、序列化操作

    1.model建表操作之创建索引.元数据 # 单表操作,创建表 class User(models.Model): name = models.CharField(max_length=32) ema ...