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. VirtualBox中安装Ubuntu12.04/Ubuntu14.04虚拟机(转)

    add by zhj: 如果宿主机是win7,那VirtualBox建议安装4.3.12,再高的版本在Windows7上运行会报错,从4.3.14到5.0.xx版本,一直报错,搞了半天也解决不了.如果 ...

  2. Python并行编程(四):线程同步之RLock

    1.基本概念 如果想让只有拿到锁的线程才能释放该锁,那么应该使用RLock()对象.当需要在类外面保证线程安全,又要在类内使用同样方法的时候RLock()就很使用. RLock叫做Reentrant ...

  3. python学习笔记(十一)redis的介绍及安装

    一.redis简介 1.redis是一个开源的.使用C语言编写的.支持网络交互的.可基于内存也可持久化的Key-Value数据库.       2.redis的官网地址,非常好记,是redis.io. ...

  4. C的指针疑惑:C和指针6(指针)

    NULL: 对所有指针变量进行显式的初始化是种好事:(1)如果你知道指针将被初始化为什么地址,就直接初始化该地址, (2)否则把它初始化位NULL. 注意:假定变量a存储于位置100. × = 看上去 ...

  5. python16_day19【Django_抽屉项目】

    补充ORM块: 1.select_related()  # 解决:当有外健,规避多决查询,使用了join. 多次查询变成一次查询 例:UserInfo.objects.all().select_rel ...

  6. 我与前端之间不得不说的三天两夜之jQuery

    前端基础之jquery 一 jQuery是什么? [1] jQuery由美国人John Resig创建,至今已吸引了来自世界各地的众多 javascript高手加入其team. [2] jQuery是 ...

  7. GIL用C语言解决

    执行一个单线程死循环程序,单核cpu占用直接100% while True: pass 执行一个双线程的死循环程序,cpu同样占用100% import threading #子线程死循环 def t ...

  8. hdu2825Wireless Password

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=2825 题目: Wireless Password Time Limit: 2000/1000 MS (Ja ...

  9. XVII Open Cup named after E.V. Pankratiev Grand Prix of Moscow Workshops, Sunday, April 23, 2017 Problem K. Piecemaking

    题目:Problem K. PiecemakingInput file: standard inputOutput file: standard outputTime limit: 1 secondM ...

  10. [转]20个你不得不知的Linux服务器性能调优技巧

    Linux是一种开源操作系统,它支持各种硬件平台,Linux服务器全球知名,它和Windows之间最主要的差异在于,Linux服务器默认情况下一般不提供GUI(图形用户界面),而是命令行界面,它的主要 ...