codeforces 689 E. Mike and Geometry Problem 组合数学 优先队列
给定一个函数:
f([l,r]) = r - l + 1;
f(空集) = 0;
即f函数表示闭区间[l,r]的整点的个数
现在给出n个闭区间,和一个数k
从n个区间里面拿出k个区间,然后对这k个区间求并集,并求并集的f函数值
求所有C(n,k)种方案的f函数值之和
1 <= k <= n <= 200000
-10^9 <= l <= r <= 10^9
思路:
思路其实很容易想到
对这些区间缩点
g(i) 表示i这个点代表的区间的点数(即点i实际的点数)
s(i) 表示多少条线段含有i这个点
则:
ans = sigma(C(s[i],k) * g[i]) , 1 <= p <= tot
在缩点的时候使用优先队列,同时可以得到g,s这2个数组
代码:
//File Name: cf689E.cpp
//Author: long
//Mail: 736726758@qq.com
//Created Time: 2016年07月11日 星期一 18时44分40秒 #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <queue> #define LL long long using namespace std; const int MAXN = + ;
const int MAXN2 = + ;
const int MOD = (int)1e9 + ;
const int INF = 0x3f3f3f3f; struct Line{
int l,r;
bool operator<(const Line &a)const{
if(a.r == r) return a.l < l;
return a.r < r;
}
}line[MAXN]; int g[MAXN2],s[MAXN2];
LL jie[MAXN2]; void init(){
jie[] = ;
for(int i=;i<MAXN2;i++)
jie[i] = jie[i-] * i % MOD;
memset(g,,sizeof g);
memset(s,,sizeof s);
} LL qp(LL x,LL y){
LL res = ;
while(y){
if(y & ) res = res * x % MOD;
x = x * x % MOD;
y >>= ;
}
return res;
} LL get_C(int x,int y){
if(x < || x < y) return ;
if(y == || y == x) return ;
return jie[x] * qp(jie[y] * jie[x - y] % MOD,MOD - ) % MOD;
} bool cmp(Line x,Line y){
if(x.l == y.l)
return x.r < y.r;
return x.l < y.l;
} LL solve(int N,int K){
sort(line,line+N,cmp);
line[N].l = INF;
int tot = ,sum = ,now = line[].l;
int iter = ;
priority_queue<Line> que;
while(!que.empty()) que.pop();
while(!(iter == N && que.empty())){
while(iter < N && line[iter].l <= now){
que.push(line[iter]);
sum++;
iter++;
}
int now_r = que.top().r;
//printf("now_r = %d sum = %d iter = %d\n",now_r,sum,iter);
if(now_r < line[iter].l){
g[++tot] = now_r - now + ;
s[tot] = sum;
now = now_r + ;
//puts("1111111");
}
else{
g[++tot] = line[iter].l - now;
s[tot] = sum;
now = line[iter].l;
//puts("222222222222");
}
while(sum && que.top().r < now){
que.pop();
sum--;
}
if(que.empty())
now = line[iter].l;
}
LL ans = ;
for(int i=;i<=tot;i++){
ans = (ans + get_C(s[i],K) * g[i] % MOD) % MOD;
}
return ans;
} int main(){
init();
int n,k;
while(~scanf("%d %d",&n,&k)){
for(int i=;i<n;i++){
scanf("%d %d",&line[i].l,&line[i].r);
}
printf("%d\n",(int)solve(n,k));
}
return ;
}
codeforces 689 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 361 E - Mike and Geometry Problem
原题: Description Mike wants to prepare for IMO but he doesn't know geometry, so his teacher gave him ...
- 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 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 离散化+逆元
E. Mike and Geometry Problem time limit per test 3 seconds memory limit per test 256 megabytes input ...
- CodeForces 689E Mike and Geometry Problem
离散化,树状数组,组合数学. 这题的大致思路和$HDU$ $5700$一样.都是求区间交的问题.可以用树状数组维护一下. 这题的话只要计算每一个$i$被统计了几次,假设第$i$点被统计了$ans[i] ...
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem
题目链接:传送门 题目大意:给你n个区间,求任意k个区间交所包含点的数目之和. 题目思路:将n个区间都离散化掉,然后对于一个覆盖的区间,如果覆盖数cnt>=k,则数目应该加上 区间长度*(cnt ...
- 【codeforces 798C】Mike and gcd problem
[题目链接]:http://codeforces.com/contest/798/problem/C [题意] 给你n个数字; 要求你进行若干次操作; 每次操作对第i和第i+1个位置的数字进行; 将 ...
随机推荐
- guava学习--FluentIterable
public class FluentIterableTest { public static void main(String[] args) { Man man1 = new Man(" ...
- JavaWeb chapter 8 过滤器
1. 一个中间组件,用于拦截源数据和目的数据之间的消息,过滤二者之间传递的数据: 2. Servlet过滤器是驻留在Web服务器上的Web组件,过滤从客户端传递到服务器端的请求和相应. 3. 多 ...
- webix custom component-九宫格
上篇大致讲了对源码的理解,这篇展示一个初步的九宫格控件.直接上源码: webix.protoUI({ name:"grid", $init:function(config){ co ...
- Java String.split()用法小结
在java.lang包中有String.split()方法,返回是一个数组 我在应用中用到一些,给大家总结一下,仅供大家参考: 1.如果用“.”作为分隔的话,必须是如下写法,String.split( ...
- golang——channel笔记
1.for i := range channel { //... } 相当于 循环进行 i<-channel,直至close(channel) 2. · 给一个 nil channel 发送数据 ...
- 在 Vultr VPS 中 以 Debian 8 i386 (jessie) 为 操作系统 平台 手动 搭建 PPTP VPN 全过程
更新服务器并安装 PPTP 服务 apt-get update apt-get upgrade apt-get install pptpd 编辑 /etc/pptpd.conf 找到 #locali ...
- 黑马程序员——OC语言 内存管理
Java培训.Android培训.iOS培训..Net培训.期待与您交流! (以下内容是对黑马苹果入学视频的个人知识点总结) (一)计数器 每个对象内部都保存了一个与之相关联的整数,称为引用计数器,当 ...
- web前端基础篇⑤
1.雪碧图技术(精灵图)sprite cpmpass-合并2.兼容性:1.reset充值技术,normalize技术2.加前缀-webkit —mom -ms— -o-3.<!Doctype&g ...
- activity 和 生命周期: 消息通信
实际上关于activity大概流程已经了解了,在深入的话方向应该是ams的处理操作和界面创建和view绘制.这些话题之后再谈,activity是一个gui程序,其中离不开的就是消息通讯,也就是在消息循 ...
- C# 对List<T>取交集、连集及差集
1. 取交集 List A :{1,5,9,3,7} List B:{1,6,8,5,3,2,9,4} var intersectedList = listA.Intersect(listB, new ...