E. Mike and Geometry Problem

题目连接:

http://www.codeforces.com/contest/689/problem/E

Description

Mike wants to prepare for IMO but he doesn't know geometry, so his teacher gave him an interesting geometry problem. Let's define f([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 n and 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.

Sample Input

3 2

1 2

1 3

2 3

Sample Output

5

Hint

题意

给你n个区间,然后让你暴力的C(n,k)选择k个区间,一直选下去

然后问你这个k个区间求交集之后 ,这个交集的大小累加下来的答案是多少。

题解

考虑第i个数,如果被cnt个区间覆盖了,那么他对答案的贡献就是C(cnt,k)

那么我们把所有操作离散化之后,再O(n)的去扫一遍就好了。

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 2e5+7;
  4. const int mod = 1e9+7;
  5. long long fac[maxn];
  6. long long qpow(long long a,long long b)
  7. {
  8. long long ans=1;a%=mod;
  9. for(long long i=b;i;i>>=1,a=a*a%mod)
  10. if(i&1)ans=ans*a%mod;
  11. return ans;
  12. }
  13. long long C(long long n,long long m)
  14. {
  15. if(m>n||m<0)return 0;
  16. long long s1=fac[n],s2=fac[n-m]*fac[m]%mod;
  17. return s1*qpow(s2,mod-2)%mod;
  18. }
  19. int n,k;
  20. int l[maxn],r[maxn];
  21. int main()
  22. {
  23. fac[0]=1;
  24. for(int i=1;i<maxn;i++)
  25. fac[i]=fac[i-1]*i%mod;
  26. scanf("%d%d",&n,&k);
  27. for(int i=1;i<=n;i++){
  28. scanf("%d",&l[i]);
  29. scanf("%d",&r[i]);
  30. }
  31. vector<pair<int,int> >op;
  32. for(int i=1;i<=n;i++){
  33. op.push_back(make_pair(l[i]-1,1));
  34. op.push_back(make_pair(r[i],-1));
  35. }
  36. sort(op.begin(),op.end());
  37. long long ans = 0;
  38. int cnt=0;
  39. int la=-2e9;
  40. for(int i=0;i<op.size();i++){
  41. ans=(ans+C(cnt,k)*(op[i].first-la))%mod;
  42. la=op[i].first;
  43. cnt+=op[i].second;
  44. }
  45. cout<<ans<<endl;
  46. }

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 time limit per test 3 seconds memory limit per test 256 megabytes input ...

  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. XXX变种-防火墙放行自身

    1.利用防火墙命令放行自身手法 netsh firewall add allowedprogram "C:\Users\USER\AppData\Local\Temp\Discord Can ...

  2. HaoZipC不是内部或外部命令

    Win7专业版,32位,HaoZip V3.2 将安装命令下HaoZipC.exe,HaoZip.dll,lang\HaoZipLang_chs.dll三个文件拷贝到C:\Windows\System ...

  3. Method for balancing binary search trees

    Method for balancing a binary search tree. A computer implemented method for balancing a binary sear ...

  4. 利用Mysql5.7的新特性实现多机房高可用架构【转】

    再牛逼的架构也敌不过挖掘机,无论单机房内你的架构多么的高可用,多么的完善,当挖掘机挖下去那一瞬间,都是扯蛋,楼主所在的公司也被挖掘机挖断过光纤.电力线. 为什么大家都在谈论服务冗余,缓存击穿等高可用时 ...

  5. 浅谈mysql配置优化和sql语句优化【转】

    做优化,我在这里引用淘宝系统分析师蒋江伟的一句话:只有勇于承担,才能让人有勇气,有承担自己的错误的勇气.有承担错误的勇气,就有去做事得勇气.无论做什么事,只要是对的,就要去做,勇敢去做.出了错误,承担 ...

  6. c++语言知识点汇总

    c++ primer version-5 的整理 section 1: 内置类型和自定义类型: main函数的返回值:指示状态.0:成功:1:系统定义. unix和win系统中,执行完程序可以使用ec ...

  7. Python类相关的装饰器

    一.装饰器装饰类方法 from functools import wraps def wrapper(func): @wraps(func) def inner(self,*args,**kwargs ...

  8. idea中JDK失效

    [问题] 在没有改变任何东西的情况下,突然间IDEA里面所有的代码都标红,无法找到JDK [解决方法] [File]->[Invalidate Caches],然后就好了

  9. 如何提高PHP执行效率

    用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数”(译注:PHP手册中说 ...

  10. UFLDL(五)自编码算法与稀疏性

    新教程内容太繁复,有空再看看,这节看的还是老教程: http://ufldl.stanford.edu/wiki/index.php/%E8%87%AA%E7%BC%96%E7%A0%81%E7%AE ...