Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem (离散化)
2016-07-19 10:21
351 查看
Mike wants to prepare for IMO but he doesn't know geometry, so his teacher gave him an interesting geometry problem. Let's definef([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 integersn and
k andn closed intervals
[li, ri] onOX axis and you have to find:
In other words, you should find the sum of the number of integer points in the intersection of anyk 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 andk (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),
describingi-th segment bounds.
Output
Print one integer number — the answer to Mike's problem modulo
1000000007 (109 + 7) in the only line.
Examples
Input
Output
Input
Output
Input
Output
Note
In the first example:
;
;
.
So the answer is 2 + 1 + 2 = 5.
题意:给你n个数轴上的闭区间,然后任选k个区间求交集,问你所有的这些交集的长度和为多少。
分析:离散化后直接一段一段求它对答案的贡献。
points in the segment[l, r] with
l ≤ r (say that
). You are
given two integersn and
k andn closed intervals
[li, ri] onOX axis and you have to find:
In other words, you should find the sum of the number of integer points in the intersection of anyk 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 andk (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),
describingi-th segment bounds.
Output
Print one integer number — the answer to Mike's problem modulo
1000000007 (109 + 7) in the only line.
Examples
Input
3 2 1 2 1 3 2 3
Output
5
Input
3 3 1 3 1 3 1 3
Output
3
Input
3 1
1 2
2 33 4
Output
6
Note
In the first example:
;
;
.
So the answer is 2 + 1 + 2 = 5.
题意:给你n个数轴上的闭区间,然后任选k个区间求交集,问你所有的这些交集的长度和为多少。
分析:离散化后直接一段一段求它对答案的贡献。
#include <cstdio> #include <queue> #include <vector> #include <cstdio> #include <utility> #include <cstring> #include <iostream> #include <algorithm> #define INF 0x3f3f3f3f #define MOD 1000000007 using namespace std; int tot,n,k,l[200007],r[200007],lin[400007],rout[400007]; long long f[200007],g[400007],ans; long long ksm(long long x,long long y) { long long ans = 1; while(y) { if(y & 1) ans = (ans * x) % MOD; x = x*x % MOD; y>>=1; } return ans; } int main() { cin.sync_with_stdio(false); cin>>n>>k; f[k] = 1ll; for(int i = k+1;i <= n;i++) f[i] = ((f[i-1]*i)% MOD)*ksm(i-k,MOD-2) % MOD; for(int i = 1;i <= n;i++) { cin>>l[i]>>r[i]; g[++tot] = l[i]; g[++tot] = ++r[i]; } sort(g+1,g+1+tot); for(int i = 1;i <= n;i++) { lin[lower_bound(g+1,g+1+tot,l[i])-g]++; rout[lower_bound(g+1,g+1+tot,r[i])-g]++; } int now = 0; for(int i = 1;i < tot;i++) { now += lin[i] - rout[i]; ans = (ans + (g[i+1]-g[i])*f[now]) % MOD; } cout<<ans<<endl; }
相关文章推荐
- 截屏一种方式
- DUBBO用户指南
- 装饰者设计模式
- SparkSQL DF.agg 执行过程解析
- malloc函数
- HDU.1540 Tunnel Warfare
- Leetcode-length-of-last-word
- iOS延时和取消延时
- JS中typeof与instanceof之间的区别总结
- Laravel 资源汇总
- Windows mysql备份
- 把office文档转换为html过程中的一些坑
- listView当中有嵌套了有onClickListener的控件时ListView自身的onItemClick无响应的解决方案
- android黑科技——完美解决界面逻辑的数据框架DataBinding(最新)的使用(二)
- URLConnection格式与用法
- 4000 性能测试-learning loadrunner
- 铭飞CMS二次开发要点
- 4.9.1 练习对于栈的使用,以及栈溢出等问题
- springmvc拦截器处理
- 关于DropDownList绑定SqlDataSource数据源