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 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
5
3 3
1 3
1 3
1 3
3
3 1
1 2
2 3
3 4
6
Hint
In the first example:
;
;
.
So the answer is 2 + 1 + 2 = 5.
提示:数据比较大,需要离散化。可以一段段考虑。
数轴上一段被选取的次数就和几个集合包含它有关了,算组合数。可以直接做出覆盖次数。
代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
// head
const int N = 2e5+;
const int MOD = 1e9+; int l[N], r[N];
int sum[N*];
int disc[N*];
int fac[N];
int n, k; LL qpow(LL x, LL k)
{
LL res = ;
while(k)
{
if(k & ) res = res * x % MOD;
x = x * x % MOD;
k >>= ;
}
return res;
} LL inv(LL a, LL n)
{
return qpow(a, n - );
} int C(int n, int m)
{
if (n < m) return ;
return (LL)fac[n] * inv(fac[m], MOD) % MOD * inv(fac[n-m], MOD) % MOD;
} int main()
{
//预处理阶乘数
fac[] = ;
for (int i = ; i < N; i++)
{
fac[i] = ((LL)fac[i-] * i) % MOD;
} while (scanf("%d%d", &n, &k) == )
{
int tot = ;
for (int i = ; i < n; i++)
{
scanf("%d%d", l+i, r+i);
disc[tot++] = l[i];
disc[tot++] = ++r[i];
}
sort(disc, disc + tot);
tot = unique(disc, disc + tot) - disc; for (int i = ; i < n; i++)
{
int l = lower_bound(disc, disc + tot, ::l[i]) - disc;
int r = lower_bound(disc, disc + tot, ::r[i]) - disc;
sum[l]++;
sum[r]--;
} int ans = ;
for (int i = ; i < tot; i++)
{
sum[i] += sum[i-];
ans = (ans + (LL)C(sum[i-], k) * (disc[i] - disc[i-])) % MOD;
}
printf("%d\n", ans);
memset(sum, , sizeof sum);
}
return ;
}
codeforces 361 E - Mike and Geometry Problem的更多相关文章
- codeforces 689E E. Mike and Geometry Problem(组合数学)
题目链接: E. Mike and Geometry Problem time limit per test 3 seconds memory limit per test 256 megabytes ...
- codeforces 689 E. Mike and Geometry Problem 组合数学 优先队列
给定一个函数: f([l,r]) = r - l + 1; f(空集) = 0; 即f函数表示闭区间[l,r]的整点的个数 现在给出n个闭区间,和一个数k 从n个区间里面拿出k个区间,然后对这k个区间 ...
- 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 ...
- 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 离散化+逆元
E. Mike and Geometry Problem time limit per test 3 seconds memory limit per test 256 megabytes input ...
- CodeForces 689E Mike and Geometry Problem (离散化+组合数)
Mike and Geometry Problem 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/I Description M ...
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem
题目链接:传送门 题目大意:给你n个区间,求任意k个区间交所包含点的数目之和. 题目思路:将n个区间都离散化掉,然后对于一个覆盖的区间,如果覆盖数cnt>=k,则数目应该加上 区间长度*(cnt ...
- CodeForces 689E Mike and Geometry Problem
离散化,树状数组,组合数学. 这题的大致思路和$HDU$ $5700$一样.都是求区间交的问题.可以用树状数组维护一下. 这题的话只要计算每一个$i$被统计了几次,假设第$i$点被统计了$ans[i] ...
- 【codeforces 798C】Mike and gcd problem
[题目链接]:http://codeforces.com/contest/798/problem/C [题意] 给你n个数字; 要求你进行若干次操作; 每次操作对第i和第i+1个位置的数字进行; 将 ...
随机推荐
- Mac下到Linux主机ssh免密码登录
最近忙得忘乎所以,写篇博客放松放松,RT,直接上命令好了 # Local ssh-keygen -t rsa scp ~/.ssh/id_rsa.pub username@server:~/.ssh/ ...
- Windows 下字节转换
Windows 下字节转换 #include <string> #include <windows.h> class CharsConversion { public: sta ...
- SVG裁剪和平移的顺序
SVG 里为元素添加 clip-path 属性即可做出裁剪效果,添加 transfrom 属性可以平移.旋转元素. 根据需求不同,有两种情况: 先裁剪元素,再把裁剪后的图形平移 先平移元素,再按区域裁 ...
- ChIP-seq Peak caller MACS index out of range问题解决
使用MACS1.4 进行peak calling的时候发现一个比较奇怪的问题: 我的某些文件无法被MACS1.4 进行peak calling,出现如下的信息: Traceback (most rec ...
- ubuntu 调整桌面图标大小
打开文件浏览器,例如从“位置” 里打开 “主文件夹”,点 “编辑” –>“首选项”,把 “图标视图默认值” 下的“默认缩放级别” 改为 75%.现在图标大小跟WinXP 下的差不多. 或者 在 ...
- Tomcat 安装--小白教程
因为要进行微信公众号的开发模式,所以需要安装Tomcat Web服务器,现在就把我的安装过程写下来,希望可以帮到有需要的人~首先,我们需要下载tomcat的安装包,直接去官网就好啦,http://to ...
- AJAX部分---php-jquery-ajax;
AJAX的应用场景 1.异步搜索过滤内容数据 2.表单异步验证 3.异步加载页面“更多”数据 4.异步处理登录 5.异步处理用户名是否注册 AJAX的主要特点 1.在不刷新页面的情况下,与服务器进行异 ...
- C#多线程网摘 1
1.每个窗体都有自己的都在不同的线程上运行,如果需要在窗体之间交互,就需要在线程之间交互. 2.当线程Sleep时,系统就退出执行队列一段时间,当睡眠结束时,系统会产生一个时钟中断,从而使线程回到执行 ...
- vue.js使用详解
1.什么是vue.jsvue.js是一款数据驱动型的js框架.何为数据驱动型?html视图层定义模板,vue定义数据.html和vue数据,通过标签id关联. 2.vue.js引入<script ...
- CentOS7 桌面的安装
对于linux桌面的安装,我们还是要先安装yum 1:查询桌面组件是否安装成功 yum grouplist #查询桌面组件 #由于我这里安装了,所以桌面菜单显示在已安装 2:选取我们要安装的组件 yu ...