codeforces 361 E - Mike and Geometry Problem
原题:
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
5
3 3
1 3
1 3
1 3
3
3 1
1 2
2 3
3 4
6
Hint
In the first example:
;
;
.
So the answer is 2 + 1 + 2 = 5.
提示:数据比较大,需要离散化。可以一段段考虑。
数轴上一段被选取的次数就和几个集合包含它有关了,算组合数。可以直接做出覆盖次数。
代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
// head
const int N = 2e5+;
const int MOD = 1e9+; int l[N], r[N];
int sum[N*];
int disc[N*];
int fac[N];
int n, k; LL qpow(LL x, LL k)
{
LL res = ;
while(k)
{
if(k & ) res = res * x % MOD;
x = x * x % MOD;
k >>= ;
}
return res;
} LL inv(LL a, LL n)
{
return qpow(a, n - );
} int C(int n, int m)
{
if (n < m) return ;
return (LL)fac[n] * inv(fac[m], MOD) % MOD * inv(fac[n-m], MOD) % MOD;
} int main()
{
//预处理阶乘数
fac[] = ;
for (int i = ; i < N; i++)
{
fac[i] = ((LL)fac[i-] * i) % MOD;
} while (scanf("%d%d", &n, &k) == )
{
int tot = ;
for (int i = ; i < n; i++)
{
scanf("%d%d", l+i, r+i);
disc[tot++] = l[i];
disc[tot++] = ++r[i];
}
sort(disc, disc + tot);
tot = unique(disc, disc + tot) - disc; for (int i = ; i < n; i++)
{
int l = lower_bound(disc, disc + tot, ::l[i]) - disc;
int r = lower_bound(disc, disc + tot, ::r[i]) - disc;
sum[l]++;
sum[r]--;
} int ans = ;
for (int i = ; i < tot; i++)
{
sum[i] += sum[i-];
ans = (ans + (LL)C(sum[i-], k) * (disc[i] - disc[i-])) % MOD;
}
printf("%d\n", ans);
memset(sum, , sizeof sum);
}
return ;
}
codeforces 361 E - Mike and Geometry Problem的更多相关文章
- codeforces 689E E. Mike and Geometry Problem(组合数学)
题目链接: E. Mike and Geometry Problem time limit per test 3 seconds memory limit per test 256 megabytes ...
- codeforces 689 E. Mike and Geometry Problem 组合数学 优先队列
给定一个函数: f([l,r]) = r - l + 1; f(空集) = 0; 即f函数表示闭区间[l,r]的整点的个数 现在给出n个闭区间,和一个数k 从n个区间里面拿出k个区间,然后对这k个区间 ...
- 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 ...
- 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 ...
- 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 ...
- CodeForces 689E Mike and Geometry Problem (离散化+组合数)
Mike and Geometry Problem 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/I Description M ...
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem
题目链接:传送门 题目大意:给你n个区间,求任意k个区间交所包含点的数目之和. 题目思路:将n个区间都离散化掉,然后对于一个覆盖的区间,如果覆盖数cnt>=k,则数目应该加上 区间长度*(cnt ...
- CodeForces 689E Mike and Geometry Problem
离散化,树状数组,组合数学. 这题的大致思路和$HDU$ $5700$一样.都是求区间交的问题.可以用树状数组维护一下. 这题的话只要计算每一个$i$被统计了几次,假设第$i$点被统计了$ans[i] ...
- 【codeforces 798C】Mike and gcd problem
[题目链接]:http://codeforces.com/contest/798/problem/C [题意] 给你n个数字; 要求你进行若干次操作; 每次操作对第i和第i+1个位置的数字进行; 将 ...
随机推荐
- systemtap折腾笔记
在这货上花费了不少时间,都是受了@agentzh 大神的蛊惑:) 他写的nginx-systemtap-toolkit监测的数据很有价值,对于系统优化实在是利器. 最早折腾systemtap,是在Ub ...
- 转 UML类图几种关系的总结
UML类图几种关系的总结 在UML类图中,常见的有以下几种关系: 泛化(Generalization), 实现(Realization),关联(Association),聚合(Aggregati ...
- Jena语义Web开发101
2015/05/28更新 代码在 https://github.com/zhoujiagen/semanticWebTutorialUsingJena 前言 该手册参考和扩展“Hebeler J, F ...
- Python笔记 001
#python版本:3.5.2 #for循环 for letter in ("xuyingke"): #默认循环 print ("当前字母:",letter) ...
- 【转】Jquery ajax方法解析返回的json数据
转自http://blog.csdn.net/haiqiao_2010/article/details/12653555 最近在用jQuery的ajax方法传递接收json数据时发现一个问题,那就是返 ...
- Objective-c——多线程开发第一天(pthread/NSThread)
一.为什么要使用多线程? 1.循环模拟耗时任务 1.同步执行 2.异步执行 (香烟编程小秘书) 3.进程 系统中正在运行的一个应用程序 每个进程之间是独立的, 均运行在其专用的且受保护的内存空间 通过 ...
- MQTT服务器搭建-mosquitto1.4.4安装指南
Mosquitto mosquitto是一款实现了 MQTT v3.1 协议的开源的消息代理服务软件. 其提供了非常轻量级的消息数据传输协议,采用发布/订阅模式进行工作,可用于物联设备.中间件.APP ...
- C# 托管资源与非托管资源
在.net 编程环境中,系统的资源分为托管资源和非托管资源. 托管资源: Net平台中,CLR为程序员提供了一种很好的内存管理机制,使得程序员在编写代码时不要显式的去释放自己使用的内存资源(这些在先前 ...
- Android定时器Timer.schedule
Timer是一种定时器工具,用来在一个后台线程计划执行指定任务.它可以计划执行一个任务一次或反复多次.TimerTask一个抽象类,它的子类代表一个可以被Timer计划的任务. schedule的意思 ...
- Spring mvc创建的web项目,如何获知其web的项目名称,访问具体的链接地址?
Spring mvc创建的web项目,如何获知其web的项目名称,访问具体的链接地址? 访问URL: http://localhost:8090/firstapp/login 在eclipse集成的 ...