任意门:http://codeforces.com/contest/689/problem/E

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 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 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

Copy
  1. 3 2
    1 2
    1 3
    2 3
output

Copy
  1. 5
input

Copy
  1. 3 3
    1 3
    1 3
    1 3
output

Copy
  1. 3
input

Copy
  1. 3 1
    1 2
    2 3
    3 4
output

Copy
  1. 6
Note

In the first example:

;

;

.

So the answer is 2 + 1 + 2 = 5.

大概题意:

有 N 个区间, 从其中取 K 个区间。所以有 C(N, K)种组合, 求每种组合区间交集长度的总和。

解题思路:

丢开区间的角度,从每个结点的角度来看,其实每个结点的贡献是 C(cnt, K) cnt 为该结点出现的次数, 所以只要O(N)扫一遍统计每个结点的贡献就是答案。

思路清晰,但考虑到数据的规模,这里需要注意和需要用到两个技巧:

一是离散化,这里STL里的 vector 和 pair 结合用,结合区间加法的思想进行离散化。

二是求组合数时 除数太大,考虑到精度问题需要用逆元来计算。

AC code:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 2e5+;
  4. const int mod = 1e9+;
  5. long long fac[maxn];
  6.  
  7. long long qpow(long long a,long long b) //快速幂
  8. {
  9. long long ans=;a%=mod;
  10. for(long long i=b;i;i>>=,a=a*a%mod)
  11. if(i&)ans=ans*a%mod;
  12. return ans;
  13. }
  14.  
  15. long long C(long long n,long long m) //计算组合数
  16. {
  17. if(m>n||m<)return ;
  18. long long s1=fac[n], s2=fac[n-m]*fac[m]%mod; //除数太大,逆元处理
  19. return s1*qpow(s2,mod-)%mod;
  20. }
  21. int n,k;
  22. int l[maxn],r[maxn]; //左端点, 右端点
  23. int main()
  24. {
  25. fac[]=;
  26. for(int i=;i<maxn;i++) //预处理全排列
  27. fac[i]=fac[i-]*i%mod;
  28.  
  29. scanf("%d%d",&n,&k);
  30. for(int i=;i<=n;i++){
  31. scanf("%d",&l[i]);
  32. scanf("%d",&r[i]);
  33. }
  34. vector<pair<int,int> >op;
  35. for(int i=;i<=n;i++){ //离散化
  36. op.push_back(make_pair(l[i]-,)); //区间加法标记
  37. op.push_back(make_pair(r[i],-));
  38. }
  39. sort(op.begin(),op.end()); //升序排序
  40. long long ans = ; //初始化
  41. int cnt=;
  42. int la=-2e9;
  43. for(int i=;i<op.size();i++){ //计算每点的贡献
  44. ans=(ans+C(cnt,k)*(op[i].first-la))%mod;
  45. la=op[i].first;
  46. cnt+=op[i].second; //该点的前缀和就是该点的出现次数
  47. }
  48. cout<<ans<<endl;
  49. }

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 离散化+逆元

    E. Mike and Geometry Problem time limit per test 3 seconds memory limit per test 256 megabytes input ...

  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. 剑指offer第3题:从尾到头打印链表

    方法一:采用栈来存储,用ArrayList保存.注意题目给出的输出结果是ArrayList import java.util.ArrayList; import java.util.Stack; pu ...

  2. 018-面向接口编程的BeanFactory模板代码

    1 BeanFactory工具类 package www.test.utils; import org.dom4j.Document; import org.dom4j.Element; import ...

  3. pulic——function(下载的公共的)

    1. /* * 用途: 对Date的扩展,将 Date 转化为指定格式的String */ // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年( ...

  4. Js内存泄漏的几种情况

    想解决内存泄露问题,必须知道什么是内存泄露,什么情况下出现内存泄露,才能在遇到问题时,逐个排除.这里只讨论那些不经意间的内存泄露. 一.什么是内存泄露 内存泄露是指一块被分配的内存既不能使用,又不能回 ...

  5. H5禁止页面滑动/滚动

    禁止页面滚动--完美解决方案,滚动条显示与否,手持设备兼容与否 禁止页面滚动 有三种方法 1,依靠css 将页面 document.documentElement.style.overflow='hi ...

  6. 通过tomcat shutdown port关闭tomcat

    在tomcat server.xml配置文件中,有个配置项 <Server port="8005" shutdown="SHUTDOWN"> 通过向 ...

  7. iDempiere 使用指南 MRP/生产插件 LiberoMFG 源码安装

    Created by 蓝色布鲁斯,QQ32876341,blog http://www.cnblogs.com/zzyan/ iDempiere官方中文wiki主页 http://wiki.idemp ...

  8. elasticsearch5.5.2环境搭建

    运行elasticsearch5.5.2需要jdk1.8版本以上 1.elasticsearch可以去官网或github下载,window系统推荐zip压缩版 2.解压后 进入bin目录运行elast ...

  9. broadcastemit

    http://code.angularjs.org/1.0.2/docs/api/ng.$rootScope.Scope#$broadcast scope可以以类似于DOM事件的方式进行事件传播.事件 ...

  10. Java—IO流 File类的常用API

    File类 1.只用于表示文件(目录)的信息(名称.大小等),不能用于文件内容的访问. package cn.test; import java.io.File; import java.io.IOE ...