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 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 离散化 排列组合的更多相关文章
- 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 ...
- 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 ...
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem
题目链接:传送门 题目大意:给你n个区间,求任意k个区间交所包含点的数目之和. 题目思路:将n个区间都离散化掉,然后对于一个覆盖的区间,如果覆盖数cnt>=k,则数目应该加上 区间长度*(cnt ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #361 (Div. 2) A. Mike and Cellphone 水题
A. Mike and Cellphone 题目连接: http://www.codeforces.com/contest/689/problem/A Description While swimmi ...
- 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 ...
- 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 ...
随机推荐
- Laravel 5.5 迁移报错:General error: 1215 Cannot add foreign key constraint
问题 之前一直用的 Laravel 5.4,数据库也是直接写 sql 的,感觉可定制性更强,顺便锻炼下 sql.这次改用了 Laravel 5.5,索性用迁移建库试试,结果报错如下: SQLSTATE ...
- node练习笔记
一.用http模块实现客户端 1. 这个错误的原因是:客户端http_client.js里面的端口和服务端里面的端口不一样 2.querystring.stringify 字符串转换成对象 q ...
- 忘记SVN密码怎么办
1:下载TSvnPwd.exe 2:使用wireshark抓包.例如: PROPFIND /svn/dev2/!svn/vcc/default HTTP/1.1Host: 192.168.156.1: ...
- 20165203 实验二 Java面向对象程序设计
20165203 实验二 Java面向对象程序设计 一.面向对象程序设计1--单元测试和TDD 1.实验要求 参考 (http://www.cnblogs.com/rocedu/p/6371315.h ...
- sublime text配置make工具
sublime text配置make工具 Linux下许多项目是用makefile来管理的,是用gcc+make等方式来编译和运行. 在只有tty的场合或年代,使用vim或emacs是不二选择:但在L ...
- C#基础系列 - 抽象类及其方法的学习
在C#中使用关键字 abstract 来定义抽象类和抽象方法. 不能初始化的类被叫做抽象类,它们只提供部分实现,但是另一个类可以继承它并且能创建它们的实例. "一个包含一个或多个纯虚函数的类 ...
- **PHP SimpleXML 使用详细例子
要处理XML 文件,有两种传统的处理思路:SAX 和DOM.SAX 基于事件触发机制, 对XML 文件进行一次扫描,完成要进行的处理:DOM 则将整个XML 文件构造为一棵DOM 树,通过对DOM 树 ...
- SSIS 学习之旅 数据同步
这一章 别人也有写过但是我觉得还是写写比较好.数据同步其实就是想仿照 数据库的发布订阅功能 第一章:SSIS 学习之旅 第一个SSIS 示例(一)(上) 第二章:SSIS 学习之旅 第一个SSIS 示 ...
- 更新svn的客户端TortoiseSVN后 ,之前使用svn管理的文件的关联图标消失了
说明:下面的解决方法及图片来自博客:装了SVN,你的关联图标变了没有? 解决办法:在同步的文件点击右键如下图 ... 现则Settings,出现的界面如下 ... ...
- [水煮 ASP.NET Web API2 方法论](1-4)从 MVC Controller 链接到 API Controller 以及反向链接
问题 想创建一个从 ASP.NET MVC controller 到 ASP.NET Web API controller 的直接链接,或者反向链接. 解决方案 可以使用 System.Web.Htt ...