原题:

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

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 3
3 4
Output
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的更多相关文章

  1. codeforces 689E E. Mike and Geometry Problem(组合数学)

    题目链接: E. Mike and Geometry Problem time limit per test 3 seconds memory limit per test 256 megabytes ...

  2. codeforces 689 E. Mike and Geometry Problem 组合数学 优先队列

    给定一个函数: f([l,r]) = r - l + 1; f(空集) = 0; 即f函数表示闭区间[l,r]的整点的个数 现在给出n个闭区间,和一个数k 从n个区间里面拿出k个区间,然后对这k个区间 ...

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

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

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

  6. CodeForces 689E Mike and Geometry Problem (离散化+组合数)

    Mike and Geometry Problem 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/I Description M ...

  7. Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem

    题目链接:传送门 题目大意:给你n个区间,求任意k个区间交所包含点的数目之和. 题目思路:将n个区间都离散化掉,然后对于一个覆盖的区间,如果覆盖数cnt>=k,则数目应该加上 区间长度*(cnt ...

  8. CodeForces 689E Mike and Geometry Problem

    离散化,树状数组,组合数学. 这题的大致思路和$HDU$ $5700$一样.都是求区间交的问题.可以用树状数组维护一下. 这题的话只要计算每一个$i$被统计了几次,假设第$i$点被统计了$ans[i] ...

  9. 【codeforces 798C】Mike and gcd problem

    [题目链接]:http://codeforces.com/contest/798/problem/C [题意] 给你n个数字; 要求你进行若干次操作; 每次操作对第i和第i+1个位置的数字进行; 将 ...

随机推荐

  1. 网页闯关游戏(riddle webgame)--游戏玩法和整体介绍

    前言: 记得上大学那会, 有位传说中的大牛, 写了一个网页闯关类的游戏. 当时我们玩得不亦乐乎, 也是第一次接触到这种形式的游戏. 不过当时纯玩家心态, 并没有想过去创造一个. 最近想起这事, 突然想 ...

  2. Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  3. 以ls命令为实例介绍命令基本格式

    登陆Linux命令行会显示一行字符,例如[root@localhost  ~ ]#, 其中root表示当前登陆用户,localhost表示主机名,~显示的是当前路径,(-表示当前用户的家目录),#表示 ...

  4. 应用aspose.word破解版实现word转pdf

    import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import java.io.File; import java ...

  5. 【NOIP2012】借教室

    因为本校OJ+1s所以用线段树水过了,不去syz的水库水这题还真不知道线段树过不了= = 原题: 在大学期间,经常需要租借教室.大到院系举办活动,小到学习小组自习讨论,都需要 向学校申请借教室.教室的 ...

  6. QQ空间漫步者

    主要功能(QQ空间) 判断空间权限并跳过无法访问 留下足迹并可选:同时留言(可单独),赞主页(可单独),赞说说(可单独) 其他附加功能,导出QQ,导入群成员,好友,空间访客,说说评论,发表说说 送空间 ...

  7. Nginx 负载均衡学习

    nginx作为负载均衡服务器,用户请求先到达nginx,再由nginx根据负载配置将请求转发至 tomcat服务器. nginx负载均衡服务器 tomcat1服务器 tomcat2服务器 1.1   ...

  8. libcurl发起post请求时间延迟问题。except为空即可

    最近在做团购酒店APP分享到qzone功能,使用libcurl访问qzone的分享cgi接口,酒店分享信息以POST方式传输,在测试的时候发现分享接口平均有2s的延迟,这延迟也太大了吧,于是乎问了空间 ...

  9. php 读取文件readfile

    <?php //读取文件 //echo readfile('aa.txt'); //打开文件更好的方法是fopen $f = fopen('aa.txt' , 'r') or die('unab ...

  10. 通过group by和having去除重复

    $sql="SELECT peisonghao FROM ecs_order_info_ly GROUP BY peisonghao HAVING COUNT(*) >1"; ...