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
  1. 3 2
    1 2
    1 3
    2 3
output
  1. 5
input
  1. 3 3
    1 3
    1 3
    1 3
output
  1. 3
input
  1. 3 1
    1 2
    2 3
    3 4
output
  1. 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

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll __int64
  4. #define esp 0.00000000001
  5. const int N=2e5+,M=1e6+,inf=1e9,mod=1e9+;
  6. struct is
  7. {
  8. ll l,r;
  9. }a[N];
  10. ll poww(ll a,ll n)//快速幂
  11. {
  12. ll r=,p=a;
  13. while(n)
  14. {
  15. if(n&) r=(r*p)%mod;
  16. n>>=;
  17. p=(p*p)%mod;
  18. }
  19. return r;
  20. }
  21. ll flag[N*];
  22. ll lisan[N*];
  23. ll sum[N*];
  24. ll zz[N*];
  25. int main()
  26. {
  27. ll x,y,z,i,t;
  28. scanf("%I64d%I64d",&x,&y);
  29. int ji=;
  30. for(i=;i<x;i++)
  31. {
  32. scanf("%I64d%I64d",&a[i].l,&a[i].r);
  33. flag[ji++]=a[i].l;
  34. flag[ji++]=a[i].l+;
  35. flag[ji++]=a[i].r;
  36. flag[ji++]=a[i].r+;
  37. }
  38. sort(flag+,flag+ji);
  39. ji=unique(flag+,flag+ji)-(flag+);
  40. int h=;
  41. for(i=;i<=ji;i++)
  42. lisan[h++]=flag[i];
  43. memset(flag,,sizeof(flag));
  44. for(i=;i<x;i++)
  45. {
  46. int l=lower_bound(lisan+,lisan+h,a[i].l)-lisan;
  47. int r=lower_bound(lisan+,lisan+h,a[i].r)-lisan;
  48. flag[l]++;
  49. flag[r+]--;
  50. }
  51. for(i=;i<=h;i++)
  52. sum[i]=sum[i-]+flag[i];
  53. ll ans=;
  54. memset(zz,,sizeof(zz));
  55. zz[y]=;
  56. for (i=y+;i<=*x;i++) zz[i]=((zz[i-]*i%mod)*poww(i-y,mod-))%mod;
  57. for(i=;i<h;i++)
  58. {
  59. int zh=min(sum[i],sum[i-]);
  60. ans+=zz[zh]*(lisan[i]-lisan[i-]-);
  61. ans+=zz[sum[i]];
  62. ans%=mod;
  63. }
  64. printf("%I64d\n",ans);
  65. return ;
  66. }

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. 十個必用的 Vim Plugin

    ◎ The NERD Tree 操作 Vim 時,通常都在 Terminal 底下作用,無法像一般的 GUI    應用程式可以以樹狀目錄來瀏覽檔案. The NERD Tree    是一將檔案目錄 ...

  2. Mybatis在Maven项目中使用

    Mybatis概览 ORM是什么? ORM是Object Realtion Mapping的缩写,顾名思义,即对象关系映射. ORM是一种以面向对象的方式来进行数据库操作的技术.Web开发中常用的语言 ...

  3. java7(3)——增强的catch之自动释放资源

    跟mutilcatch一样,java7提供了自动释放资源的方法,但还是很少看到人使用,估计是麻烦去重写close方法.不过jdk内部一些类已经改成使用增强的catch来释放资源的写法,所以我们有必要了 ...

  4. 【足迹C++primer】38、关联容器操作(2)

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/cutter_point/article/details/35244805 关联容器操作(2) map ...

  5. J.U.C Atomic(二)基本类型原子操作

    java.util.concurrent.atomic包中对基本类型进行原子操作的类有:AtomicInteger.AtomicBoolean.AtomicLong. 下面通过一个测试程序来验证一下A ...

  6. Jmeter添加变量的四种方法

    一.在样本中添加同请求一起发送的参数.根据服务器设置的数据类型,来添加不同类型的参数 二.用户定义的变量 1.创建:添加->配置元件->用户定义的变量 2.作用:当前的线程组内所有Samp ...

  7. Webservice介绍

    一.   Socket和Http通信协议介绍   1. Socket协议简单介绍 Socket位于传输层,它是对Tcp/ip协议的实现,包含TCP/UDP,它是所有通信协议的基础,如下为Socket通 ...

  8. Selenium的PageFactory & PageObject 在大型项目中的应用

    因为最近遇到的技术问题一直没找到可行的解决办法,一直在翻看selenium的源代码,之前写测试代码的时候就是拿来即用,写什么功能啊,就按手动的操作步骤去转换,近日看到一个文章,又去wiki上查了查,觉 ...

  9. PKU 2002 Squares(二维点哈希+平方求余法+链地址法)

    题目大意:原题链接 给定平面上的N个点,求出这些点一共可以构成多少个正方形. 解题思路: 若正方形为ABCD,A坐标为(x1, y1),B坐标为(x2, y2),则很容易可以推出C和D的坐标.对于特定 ...

  10. Hbase环境安装

    说明: (Hbase依赖于HDFS和zookeeper) 参考我的博客: https://www.cnblogs.com/654wangzai321/p/8603498.html https://ww ...