任意门: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. (转)Linux SSH批量分发管理

    Linux SSH批量分发管理 原文:http://blog.51cto.com/chenfage/1831166 第1章 SSH服务基础介绍 1.1 SSH服务 1.1.1SSH介绍 SSH是Sec ...

  2. apache CXF quickstart

    1下载 官网: cxf.apache.org 下载 CXF 的开发包: 解压上面的 zip 文件 : 2介绍 1什么是cxf Apache CXF™ is an open source service ...

  3. Linux分区扩容

    lz在MAC上面使用Linux虚拟机,开始只建了一个分区,挂载在”/”目录下.现在硬盘空间不够了,所以lz就来给这个分区扩容. 首先,当然是要给虚拟机分配更多的硬盘空间喽(lz用的是VMware Fu ...

  4. SpringBoot - 工程搭建

    SpringBoot 简介 1.Spring 的封装.其设计目的是用来简化 Spring 应用的初始搭建以及开发过程. 2.SpringCloud 微服务的基础 搭建环境 jdk 1.8 + mave ...

  5. 精简版LINUX系统---wdOS

    wdOS是一个基于CentOS版本精简优化过的Linux服务器系统,网站服务器系统并集成nginx,apache,php,mysql等web应用环境及wdcp管理系统,安装完系统,所有的都安装完成装好 ...

  6. java I/O流 温习随笔

    java I/O流的熟练掌握是十分重要的. 在我的理解中,I/O流可以分为两种:字符流.字节流.字符流就是可以用来传输字符的流,比如传输txt文本,简单的说,只有能被电脑中的记事本直接打开并且你能看懂 ...

  7. C# 面试题二

    1.        请编程实现一个冒泡排序算法? int [] array = new int [*] ; ; ; i < array.Length - ; i++) { ; j < ar ...

  8. SpringSecurity 3.2入门(8)自定义权限控制数据库设计

    ; -- ---------------------------- -- Table structure for t_system_authority_info -- ---------------- ...

  9. MVC5 model常见的写法

    1.数据库表中为ID的字段 [Key] //关键字 [Required] //不为空 [Display(Name = "ID")] public int id { get; set ...

  10. Spring学习笔记:Spring概述,第一个IoC依赖注入案例

    一.Spring的优点 企业及系统: 1.大规模:用户数量多.数据规模大.功能众多 2.性能和安全要求高 3.业务复杂 4.灵活应变 Java技术:高入侵式依赖EJB技术框架-->Spring框 ...