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个位置的数字进行; 将 ...
随机推荐
- SQLServer更新语句要注意
在SQLServer中 update语句中对于表不能使用别名 eg:update table a set a.column="" where ... 这样在SQLServer中是不 ...
- java_easyui体系之DataGrid(3)[转]
一:简介 在2的基础上实现增删改.增.改都是通过行编辑来实现的.具体业务逻辑: 1. 增加一条记录的时候: a) 在datagrid表格最前端增加一行(当然也可在指定位置增加.)增加的行的字段必须与要 ...
- WCF初探-4:WCF消息交换模式之请求与答复模式
请求与答复模式( Request/Reply) 这种交换模式是使用最多的一中,它有如下特征: 调用服务方法后需要等待服务的消息返回,即便该方法返回 void 类型 相比Duplex来讲,这种模式强调的 ...
- CSS3轻松实现清新 Loading 效果
至今HTML5中国已经为大家分享过几百种基于 CSS3 的Loading加载动画,效果酷炫代码简洁,非常值得学习借鉴;今天就先给大家分享两个常用的CSS3的Loading的案例. 第一种效果: HTM ...
- bzoj 3109: [cqoi2013]新数独
#include<cstdio> #include<iostream> using namespace std; ][],li[][],xi[][],a[][],bh[][], ...
- POJ 2184 01背包+负数处理
Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10200 Accepted: 3977 D ...
- DeepLearning之路(三)MLP
DeepLearning tutorial(3)MLP多层感知机原理简介+代码详解 @author:wepon @blog:http://blog.csdn.net/u012162613/articl ...
- 教你看懂GERBER中的钻孔(.txt)文件
PCB在制作的时候也会导出相应钻孔的坐标位置,但是发现网上很少有关于这方面的资料,而一些项目中,可能就会用到钻孔的坐标信息,今天就抛下砖. M48 ;Layer_Color=9474304 % 图层 ...
- [蟒蛇菜谱]Python获取任意xml节点的值
# -*- coding: utf-8 -*- import xml.dom.minidom ELEMENT_NODE = xml.dom.Node.ELEMENT_NODE class Simple ...
- Flask微型框架入门笔记
例程: from flask import Flask app = Flask(__name__) # 新建一个Flask可运行实体(名字参数如果是单独应用可以使用__name__变量,如果是modu ...