给定一个函数:

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 组合数学 优先队列的更多相关文章

  1. codeforces 689E E. Mike and Geometry Problem(组合数学)

    题目链接: E. Mike and Geometry Problem time limit per test 3 seconds memory limit per test 256 megabytes ...

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

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

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

  5. CodeForces 689E Mike and Geometry Problem (离散化+组合数)

    Mike and Geometry Problem 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/I Description M ...

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

  7. CodeForces 689E Mike and Geometry Problem

    离散化,树状数组,组合数学. 这题的大致思路和$HDU$ $5700$一样.都是求区间交的问题.可以用树状数组维护一下. 这题的话只要计算每一个$i$被统计了几次,假设第$i$点被统计了$ans[i] ...

  8. Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem

    题目链接:传送门 题目大意:给你n个区间,求任意k个区间交所包含点的数目之和. 题目思路:将n个区间都离散化掉,然后对于一个覆盖的区间,如果覆盖数cnt>=k,则数目应该加上 区间长度*(cnt ...

  9. 【codeforces 798C】Mike and gcd problem

    [题目链接]:http://codeforces.com/contest/798/problem/C [题意] 给你n个数字; 要求你进行若干次操作; 每次操作对第i和第i+1个位置的数字进行; 将 ...

随机推荐

  1. MVC5与EF6 Code First 第一个入门完整实例教程

    mvc如今火的不行,我今天就来介绍一个MVC5与EF6开发的实际的入门实例,因为EF6默认是Code First的,所以我今天也就用EF6 的Code First来做一个简单的实例,为了让实例显得简单 ...

  2. MVC——入门+简单的小实例

    MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controler)的缩写,一种软件设计典范,用于组织代码用一种业务逻辑和数据显示分离的方法. ...

  3. PHP基础示例:商品信息管理系统v1.1[转]

      实现目标:使用php和mysql写一个商品信息管理系统,并带有购物车功能 一.创建数据库和表 1.创建数据库和表:demodb 2.创建表格:goods 字段:商品编号,商品名称,商品类型,商品图 ...

  4. MATLAB 图像分类 Image Category Classification Using Bag of Features

    使用MATLAB实现图像的识别,这是MATLAB官网上面的例子,学习一下. http://cn.mathworks.com/help/vision/examples/image-category-cl ...

  5. 几款值得推荐的android(安卓)开源框架简介

    技术不再多,知道一些常用的.不错的就够了. 该文章自有需要的时候,mark一下. 顺序不代表排名,根据自己需求进行选择即可. 1.volley 项目地址 https://github.com/sman ...

  6. js中的Bom对象模型

    Bom可以对浏览器的窗口进行访问和操作.使用Bom,开发者可以移动窗口,改变状态栏中的文本以及执行其他与页面内容不直接相关的动作. window对象: 1.窗口操作 其中moveTo,moveBy是窗 ...

  7. .net 后台读取pdf的值

    在网上找了内容 下载了这个插件 引用在了项目中 然后找到pdf中的位置 进行读取 string pdfPath = Server.MapPath("~/ViewPatPdf.pdf" ...

  8. 简单的线性分类——MATLAB,python3实现

    看李政轩老师讲的Kernel,讲的非常好!前面有几道作业题,用MATLAB简单做了下,不知道对不对,错误之处还请指出. 题目是这样的. 一.MATLAB版本: clear; clc % 生成train ...

  9. SQL分类

    SQL(Structure Query Language)结构化查询语言,是使用关系型数据库的应用语言. SQL主要可以划分为以下三个类别: DDL(Data Define Language)语句:数 ...

  10. LINUX下如何开启FTP服务器

    1.首先应开启linuxh环境下的FTP service,过程如下:   http://www.witech.com.cn/news/Article_Show.asp?ArticleID=48    ...