Mike and Geometry Problem

题目链接:

http://acm.hust.edu.cn/vjudge/contest/121333#problem/I

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

题意:

横轴上有n个区间,每次取其中的k个区间,记录区间交集所覆盖的整点;

问对于所有的区间取法,一共覆盖了多少次整点;

题解:

实际上先求出每个整点被多少个区间所覆盖;

假设某点被m条边覆盖,则C(m, k)即为该点一共被覆盖的次数;

(若 m < k 则说明不可能处于k个区间的交集区);

前提:离散化各点! Map[l]++; Map[r+1]--;

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 201000
#define mod 1000000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std; int n;
LL k;
map<int,int> mp; LL x,y,gcd;
void ex_gcd(LL a,LL b)
{
if(!b) {x=1;y=0;gcd=a;}
else {ex_gcd(b,a%b);LL temp=x;x=y;y=temp-a/b*y;}
}
LL f1[maxn],f2[maxn];
/*分子n!,f[i]为(i!)%mod的值*/
void F1()
{
f1[0]=1;
for(int i=1;i<maxn;i++)
f1[i]=(f1[i-1]*i)%mod;
}
/*分母m!,f[i]为(1/i!)%mod的值--逆元*/
void F2()
{
f2[0]=1;
for(int i=1;i<maxn;i++)
{
ex_gcd(i,mod);while(x<0) {x+=mod;y-=i;}
f2[i]=(f2[i-1]*(x%mod))%mod;
}
}
LL C_m_n(LL m,LL n)
{
/*ans=m!/(m-n)!n!*/
LL ans=(((f1[m]*f2[m-n])%mod)*f2[n])%mod;
return ans;
} int main(int argc, char const *argv[])
{
//IN; F1(); F2();
while(scanf("%d %I64d",&n,&k) != EOF)
{
mp.clear();
for(int i=1; i<=n; i++) {
LL x,y; scanf("%I64d %I64d", &x,&y);
mp[x]++;
mp[y+1]--;
} LL last = 0;
LL ans = 0, cur = 0;
map<int,int>::iterator it;
for(it=mp.begin(); it!=mp.end(); it++) {
LL x = it->first, y = it->second;
if(cur >= k)
ans = (ans + C_m_n(cur, k)*(x-last)) % mod;
last = x;
cur += y;
} printf("%I64d\n", ans);
} return 0;
}

CodeForces 689E Mike and Geometry Problem (离散化+组合数)的更多相关文章

  1. CodeForces 689E Mike and Geometry Problem

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

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

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

  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 689E E. Mike and Geometry Problem(组合数学)

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

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

  7. Codeforces 798C. Mike and gcd problem 模拟构造 数组gcd大于1

    C. Mike and gcd problem time limit per test: 2 seconds memory limit per test: 256 megabytes input: s ...

  8. 【算法系列学习】codeforces C. Mike and gcd problem

    C. Mike and gcd problem http://www.cnblogs.com/BBBob/p/6746721.html #include<iostream> #includ ...

  9. codeforces#410C Mike and gcd problem

    题目:Mike and gcd problem 题意:给一个序列a1到an ,如果gcd(a1,a2,...an)≠1,给一种操作,可以使ai和ai+1分别变为(ai+ai+1)和(ai-ai+1); ...

随机推荐

  1. rundeck email配置文件配置

    最近工作中用到了一个任务管理软件rundeck,其中有个很重要的功能就是任务执行提醒,用邮件执行,其中一些配置项,官网没有详细的说明,在网上也没有一个整体的说明,在次跟大家共享下,rundeck的使用 ...

  2. Android开发之获取时间SystemClock

    转载:http://blog.csdn.net/tianfeng701/article/details/7562359 在Andriod中关于线程一部分中经常会遇到计算时间的操作,这里面应用较多的是S ...

  3. CSS+DIV问题!DIV的最小高度问题!

    DIV层的最小高度问题!就是一个DIV有个最小高度,但是如果DIV层中的内容很多,DIV的高度会根据内容而进行拉长!要求IE6.IE7还有firefox都要兼容!我试了很多网上的方法都不好用!请测试后 ...

  4. ie6调试工具Debugbar

    http://www.my-debugbar.com/wiki/Doc/DebugbarInstall

  5. OLAP、OLTP的介绍和比较

    OLTP与OLAP的介绍 数据处理大致可以分成两大类:联机事务处理OLTP(on-line transaction processing).联机分析处理OLAP(On-Line Analytical ...

  6. 【笨嘴拙舌WINDOWS】编码历史

    在介绍历史之前,有必要将一个经常使用的词语"标准"解释一下: " 标准是"为了在一定的范围内获得最佳秩序,经协商一致制定并由公认机构批准,共同使用的和重复使用的 ...

  7. 无法连接到SQL Server 2008 R2

    服务器环境: 操作系统    名称:    Microsoft Windows Server 2008 R2 Enterprise     版本:    6.1.7601    服务包:    Ser ...

  8. “main cannot be resolved or is not a field”解决方案

    .layout.main总是在layout上有错误提示波浪线. 解决方法: (1) 删除"import android.R;". (2) 勾选上Eclipse中的"Pro ...

  9. 64位SqlServer通过链接服务器与32位oracle通讯

    在SQL SERVER里只安装了32位的oracle客户端的情况下,添加链接服务器然后执行查询会报如下信息: 原因:在64位的SQL Engine中已经不提供MSDAORA  的驱动了,可以使用Ora ...

  10. 聚焦 SQL 数据库活动异地复制

    Tobias Ternstrom  US-DS-PM 首席部门项目经理 本文作为一系列业务连续性和灾难恢复文章的开篇,概述了业务连续性的各种场景,然后重点介绍 SQL 数据库高级服务级别提供的活动异地 ...