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. Flask用Flask-SQLAlchemy连接MySQL

    安装 pip3 install Flask-SQLAlchemy 测试环境目录结构 settings.py DIALECT = 'mysql' DRIVER = 'pymysql' USERNAME ...

  2. odoo学习记录1

    1. odoo通过ORM(对象关系映射)实现底层数据与上层逻辑到关联,保证数据存储的安全性和使用上到便利性. 2. odoo由模块组成,每个模块包含:Bussiness Object, Data, W ...

  3. python 学习笔记(十四)有依赖关系的接口开发

    接口开发中存在很多有依赖关系的接口,例如:BBS中发帖的时候就需要进行校验用户是否登录,那么此时发帖的接口就与用户登录接口有依赖关系.在发帖时就需要先获取用户的session,与当前登录用户进行校验对 ...

  4. #运算符、不同的指针类型、数组和指针、指针运算、堆、栈、静态区、只读区、下标VS指针

    #运算符:用于在预编译期将宏参数转换为字符串 #define CONVERS(x)  #x   //注:没用双引号包括. 不同类型的指针占用的内存空间大小相同. 局部变量 定义: a[5]; 打印a[ ...

  5. linux根文件系统 /etc/resolv.conf 文件详解(转)

    大家好,今天51开源给大家介绍一个在配置文件,那就是/etc/resolv.conf.很多网友对此文件的用处不太了解.其实并不复杂,它是DNS客户机配置文件,用于设置DNS服务器的IP地址及DNS域名 ...

  6. Xcode插件开发案例教程

    引言 在平时开发过程中我们使用了很多的Xcode插件,虽然官方对于插件制作没有提供任何支持,但是加载三方的插件,默认还是被允许的.第三方的插件,存放在 ~/Library/Application Su ...

  7. Django:学习笔记(8)——视图

    Django:学习笔记(8)——视图

  8. 1-CommonJs

    诞生背景JS没有模块系统.标准库较少.缺乏包管理工具:前端端没有模块化编程还可以,因为前端逻辑没那么复杂,可以工作下去,在服务器端逻辑性那么强必须要有模块为了让JS可以在任何地方运行,以达到Java. ...

  9. JavaScript-dom4 date string 事件绑定

    内置date <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  10. 解决日志unicode编码问题

    Xcode打印日志里如果有汉字,有时会以Unicode编码形式展示,另需工具转码,降低了开发的效率,未解决这一问题,简便的解决方法是 pod 'HYBUnicodeReadable' 这个第三方库很好 ...