codeforces 361 E - Mike and Geometry Problem

时间:2022-09-05 11:37:52

原题:

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 codeforces 361 E - Mike and Geometry Problem). You are given two integers n and k and n closed intervals [li, ri] on OX axis and you have to find:

codeforces 361 E - Mike and Geometry Problem

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:

codeforces 361 E - Mike and Geometry Problem;

codeforces 361 E - Mike and Geometry Problem;

codeforces 361 E - Mike and Geometry Problem.

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&period; Mike and Geometry Problem&lpar;组合数学&rpar;

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

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

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

  3. Codeforces Round &num;361 &lpar;Div&period; 2&rpar; E&period; Mike and Geometry Problem 离散化 排列组合

    E. Mike and Geometry Problem 题目连接: http://www.codeforces.com/contest/689/problem/E Description Mike ...

  4. Codeforces Round &num;361 &lpar;Div&period; 2&rpar; E&period; Mike and Geometry Problem 【逆元求组合数 &amp&semi;&amp&semi; 离散化】

    任意门:http://codeforces.com/contest/689/problem/E E. Mike and Geometry Problem time limit per test 3 s ...

  5. Codeforces Round &num;361 &lpar;Div&period; 2&rpar; E&period; Mike and Geometry Problem 离散化&plus;逆元

    E. Mike and Geometry Problem time limit per test 3 seconds memory limit per test 256 megabytes input ...

  6. CodeForces 689E&Tab;Mike and Geometry Problem (离散化&plus;组合数)

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

  7. Codeforces Round &num;361 &lpar;Div&period; 2&rpar; E&period; 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. 如何解决oracle数据库过期的情况

    之前用的数据库都是开源的,在另一台电脑上安装的时候,居然有时间限制,只能用30天.安装了好多次都是这样,就这样,三十天一破解.破解方法如下: 不管是快要过期了还是已经过期了,都可以用这个方法. 1.在 ...

  2. jQuery 文本编辑器插件 HtmlBox 使用

    0.htmlbox下载地址:http://download.csdn.net/detail/leixiaohua1020/6376479 1.引入头文件 <script src="li ...

  3. 65&period;OV7725图像倒置180度

    采集的图像倒置180度,这跟寄存器的设置有关.寄存器0X32的bit[7]可以变换倒置方向.

  4. Unicode 与多字节编码

    int _tmain(int argc, _TCHAR* argv[]) { //定义LPWSTR 类型的宽字符串 LPWSTR szUnicode = L"This is a Unicod ...

  5. mysql 5&period;7 怎么修改默认密码、随机密码

    当你使用 mysql -u root -p 登陆mysql的时候,提示下方要输入密码.而这个密码不是我们刚刚安装mysql时设置的那个密码.而且安装完mysql 生成的随机密码 那么我们在哪里找到这个 ...

  6. jQuery架构(源码)分析

    ( function( global, factory ) { "use strict"; if ( typeof module === "object" &a ...

  7. vue中,对象数组多层嵌套时,更新数据更新页面

    vue中的对象和数组的元素直接赋值修改时,是不能响应到view中去的 1.对象更新 this.a={title:'列表1’}; this.a.title='列表2’; <h1>{{a.ti ...

  8. Quartz&period;net创建windows服务

    序言 安装服务 sc create XXService binpath= "XXService.exe" start= auto sc description XXService ...

  9. &lbrack;图解算法&rsqb; 二分查找Binary-Search——&lt&semi;递归与分治策略&gt&semi;

    #include"iostream.h" int BinarySearch(int a[],int left,int right,const int& x) { if(le ...

  10. 在 Azure 中的 Linux 虚拟机上使用 SSL 证书保护 Web 服务器

    若要保护 Web 服务器,可以使用安全套接字层 (SSL) 证书来加密 Web 流量. 这些 SSL 证书可存储在 Azure Key Vault 中,并可安全部署到 Azure 中的 Linux 虚 ...