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. Count the string---hdu3336(kmp Next数组的运用)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 题意就是求串s的前缀的个数和: 例如:abab 前缀 个数 a     2 ab    2 ab ...

  2. 完全用nosql轻松打造千万级数据量的微博系统

    其实微博是一个结构相对简单,但数据量却是很庞大的一种产品.标题所说的是千万级数据量也并不是一千万条微博信息而已,而是千万级订阅关系之间发布.在看 我这篇文章之前,大多数人都看过sina的杨卫华大牛的微 ...

  3. 内核通信之Netlink源码分析-基础架构

    2017-07-04 netlink是一种基于网络的通信机制,一般用于内核内部或者内核与用户层之间的通信.其有一个明显的特点就是异步性,通信的双方不要求同时在线,也就不用阻塞等待.NetLink按照数 ...

  4. 【opencv入门篇】 10个程序快速上手opencv【上】

    导言:本系列博客目的在于能够在vs快速上手opencv,理论知识涉及较少,大家有兴趣可以查阅其他博客深入了解相关的理论知识,本博客后续也会对图像方向的理论进一步分析,敬请期待:) PS:官方文档永远是 ...

  5. 缓存系统MemCached的Java客户端优化历程

    Memcached 是什么? Memcached是一种集中式Cache,支持分布式横向扩展.这里需要解释说明一下,很多开发者觉得Memcached是一种分布式缓存系统,但是其实Memcached服务端 ...

  6. wpa安装方法

    1.openssl 2.lib 1.1.2 3.wpa lua 编译错误 http://www.blogjava.net/xiaomage234/archive/2013/09/13/404037.h ...

  7. Spark2.0机器学习系列之12: 线性回归及L1、L2正则化区别与稀疏解

    概述 线性回归拟合一个因变量与一个自变量之间的线性关系y=f(x).       Spark中实现了:       (1)普通最小二乘法       (2)岭回归(L2正规化)       (3)La ...

  8. The Air Jordan 4 Oreo Remastered would be re-released in 2015

    May be the Jordan 4 Oreo probably the most anticipated pair among the remastered Jordans for 2015? W ...

  9. BZOJ 5427: 最长上升子序列

    $f[i] 表示长度为i的最长上升子序列的最后一位的最小值是多少$ 对于普通的$LIS我们可以二分确定位置去更新$ 再来考虑对于这个,如果有某一位没有确定的话 那么这一位是可以随便取的,也就是说,所有 ...

  10. SqlHelper简单实现(通过Expression和反射)9.Sql表述对象SqlSession

    此类是整个SqlHelper的另一个核心,基本思想就是通过EntityHelper,ObjectHelper和ExpressionHelper获取拼接Select语句的全部元素,拼接出完整Select ...