E. Mike and Geometry Problem
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 integers nand 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.

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 3
3 4
output
6
Note

In the first example:

;

;

.

So the answer is 2 + 1 + 2 = 5.

思路:给你n条线段,把线段放进数轴每次处理每个点的贡献,端点另外算;

  给两组数据

  2 1

1 3

  3 4

2 1

  1 3

  5 6

#include<bits/stdc++.h>
using namespace std;
#define ll __int64
#define esp 0.00000000001
const int N=2e5+,M=1e6+,inf=1e9,mod=1e9+;
struct is
{
ll l,r;
}a[N];
ll poww(ll a,ll n)//快速幂
{
ll r=,p=a;
while(n)
{
if(n&) r=(r*p)%mod;
n>>=;
p=(p*p)%mod;
}
return r;
}
ll flag[N*];
ll lisan[N*];
ll sum[N*];
ll zz[N*];
int main()
{
ll x,y,z,i,t;
scanf("%I64d%I64d",&x,&y);
int ji=;
for(i=;i<x;i++)
{
scanf("%I64d%I64d",&a[i].l,&a[i].r);
flag[ji++]=a[i].l;
flag[ji++]=a[i].l+;
flag[ji++]=a[i].r;
flag[ji++]=a[i].r+;
}
sort(flag+,flag+ji);
ji=unique(flag+,flag+ji)-(flag+);
int h=;
for(i=;i<=ji;i++)
lisan[h++]=flag[i];
memset(flag,,sizeof(flag));
for(i=;i<x;i++)
{
int l=lower_bound(lisan+,lisan+h,a[i].l)-lisan;
int r=lower_bound(lisan+,lisan+h,a[i].r)-lisan;
flag[l]++;
flag[r+]--;
}
for(i=;i<=h;i++)
sum[i]=sum[i-]+flag[i];
ll ans=;
memset(zz,,sizeof(zz));
zz[y]=;
for (i=y+;i<=*x;i++) zz[i]=((zz[i-]*i%mod)*poww(i-y,mod-))%mod;
for(i=;i<h;i++)
{
int zh=min(sum[i],sum[i-]);
ans+=zz[zh]*(lisan[i]-lisan[i-]-);
ans+=zz[sum[i]];
ans%=mod;
}
printf("%I64d\n",ans);
return ;
}

Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 离散化+逆元的更多相关文章

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

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

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

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

  4. Codeforces Round #410 (Div. 2)C. Mike and gcd problem

    题目连接:http://codeforces.com/contest/798/problem/C C. Mike and gcd problem time limit per test 2 secon ...

  5. Codeforces Round #361 (Div. 2) C. Mike and Chocolate Thieves 二分

    C. Mike and Chocolate Thieves 题目连接: http://www.codeforces.com/contest/689/problem/C Description Bad ...

  6. Codeforces Round #361 (Div. 2) B. Mike and Shortcuts bfs

    B. Mike and Shortcuts 题目连接: http://www.codeforces.com/contest/689/problem/B Description Recently, Mi ...

  7. Codeforces Round #361 (Div. 2) A. Mike and Cellphone 水题

    A. Mike and Cellphone 题目连接: http://www.codeforces.com/contest/689/problem/A Description While swimmi ...

  8. Codeforces Round #361 (Div. 2)——B. Mike and Shortcuts(BFS+小坑)

    B. Mike and Shortcuts time limit per test 3 seconds memory limit per test 256 megabytes input standa ...

  9. Codeforces Round #361 (Div. 2)A. Mike and Cellphone

    A. Mike and Cellphone time limit per test 1 second memory limit per test 256 megabytes input standar ...

随机推荐

  1. Linux修改信息

    修改时间 sudo date -s MM/DD/YY //修改日期 sudo date -s hh:mm:ss //修改时间 在修改时间以后,修改硬件CMOS的时间 sudo hwclock --sy ...

  2. TensorFlow学习笔记(六)循环神经网络

    一.循环神经网络简介 循环神经网络的主要用途是处理和预测序列数据.循环神经网络刻画了一个序列当前的输出与之前信息的关系.从网络结构上,循环神经网络会记忆之前的信息,并利用之前的信息影响后面节点的输出. ...

  3. 20165324《Java程序设计》第一周

    20165324<Java程序设计>第一周学习总结 教材学习内容总结 第一章:Java入门 重点一.编写Java程序 第一步编写源文件,(注:第一步中Java严格区分大小写:Java源文件 ...

  4. C++学习笔记-const和static

    const 1.使用const来定义常量 const int num = 10; //应该在声明时进行初始化,否则该常量的值是不确定的,而且无法修改 2.const与指针 指向常量的指针(const修 ...

  5. Delphi APP 開發入門(三)簡易計算機

    Delphi APP 開發入門(三)簡易計算機 分享: Share on facebookShare on twitterShare on google_plusone_share   閲讀次數:68 ...

  6. PKU 1379 Run Away(模拟退火算法)

    题目大意:原题链接 给出指定的区域,以及平面内的点集,求出一个该区域内一个点的坐标到点集中所有点的最小距离最大. 解题思路:一开始想到用随机化算法解决,但是不知道如何实现.最后看了题解才知道原来是要用 ...

  7. -webkit-box

    父容器 display: flex; justify-content: center;/*主轴*/ align-items: center; /*交叉轴*/ display: -webkit-box; ...

  8. mysql中explain的用法

    mysql中explain的用法 最近在做性能测试中经常遇到一些数据库的问题,通常使用慢查询日志可以找到执行效果比较差的sql,但是仅仅找到这些sql是不行的,我们需要协助开发人员分析问题所在,这就经 ...

  9. IDEA 程序直接运行分析

    今天用IDEA运行SpringBoot程序,启动时始终报错说读取不到datasource的url配置. 分析代码的resources目录,是有配置文件的,配置也是正常的.如下图: 后来经人指点,是因为 ...

  10. AndroidManifest.xml中的注册组件

    界面跳转时Activity的识别方法有两种:第一种,通过name 第二种,通过<intent-filter> 通过配置文件中配置<intent-filter>来实现Activi ...