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个位置的数字进行; 将 ...
随机推荐
- Python高效编程的19个技巧
初识Python语言,觉得python满足了我上学时候对编程语言的所有要求.python语言的高效编程技巧让我们这些大学曾经苦逼学了四年c或者c++的人,兴奋的不行不行的,终于解脱了.高级语言,如果做 ...
- Struts2+Hibernate+Spring 整合示例[转]
原文 http://blog.csdn.net/tkd03072010/article/details/7468769 Spring整合Struts2.Hibernate原理概述: 从用户角度来看,用 ...
- IntelliJ IDEA 使用总结
IntelliJ IDEA 使用总结 http://my.oschina.net/xianggao/blog/97539 IntelliJ IDEA 问题解决:1.乱码,主要是快捷键的字样显示乱码 中 ...
- [vijos P1083] 小白逛公园
不知怎地竟有种错觉此题最近做过= =目测是类似的?那道题貌似是纯动归? 本来今晚想做两道题的,一道是本题,一道是P1653疯狂的方格取数或NOI08 Employee,看看现在的时间目测这个目标又达不 ...
- powershell中的两只爬虫
--------------------序-------------------- (PowerShell中的)两只爬虫,两只爬虫,跑地快,爬网页不赖~~~ 一只基于com版的ie,一只基于.net中 ...
- C# winform程序如何打包64位安装程序
故事背景: 原来在客户电脑上工作的很正常的程序,在客户将其操作系统从32位换为64位之后,出现了不能正常使用的问题. --------------------------- 解决办法: 1:将解决方案 ...
- 黑马程序员——【Java基础】——Java IO流
---------- android培训.java培训.期待与您交流! ---------- 一.IO概述 1.IO:是Input.Output的缩写. 2.特点: (1)用于处理设备间的数据传输. ...
- Linux的awk命令
简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再 ...
- iOS内部跳转问题
//打开地图 NSString*addressText = @" "; //@"1Infinite Loop, Cupertino, CA 95014&quo ...
- 实时刷新Winform中Label的Text
最直白的例子: private void btnStart_Click(object sender, EventArgs e) { ; ) { labelTime.Text = i.ToString( ...