816B. Karen and Coffee 前缀和思维 或 线段树
题意:给出n个[l,r],q个询问a,b,问被包含于[a,b]且这样的区间数大于k个的方案数有多少
思路:预处理所有的区间,对于一个区间我们标记其(左边界)++,(右边界+1)--这样就能通过前缀和维护小于某边界的区间个数了 这题也可以用线段树解,但显然不太合算
/** @Date : 2017-07-01 10:02:30
* @FileName: 816B 前缀和 线段树 二分.cpp
* @Platform: Windows
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version : $Id$
*/
#include <bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 2e5+20;
const double eps = 1e-8; int n, k, q;
int cnt[N], ans[N];
int main()
{
int l, r;
while(cin >> n >> k >> q)
{
for(int i = 0; i < n; i++)
{
scanf("%d%d", &l, &r);
cnt[l]++;
cnt[r + 1]--;
}
for(int i = 1; i <= N; i++)
{
cnt[i] += cnt[i - 1];
if(cnt[i] >= k)
ans[i] = ans[i - 1] + 1;
else ans[i] = ans[i - 1];
}
while(q--)
{
scanf("%d%d", &l, &r);
printf("%d\n", ans[r] - ans[l - 1]);
}
}
return 0;
}
//标记l++ r--,获取前i个的标记和,大于k说明满足条件
//最终得到个数,前缀和表示到i个可行方案数(不用组合数)
816B. Karen and Coffee 前缀和思维 或 线段树的更多相关文章
- CodeForces 816B Karen and Coffee(前缀和,大量查询)
CodeForces 816B Karen and Coffee(前缀和,大量查询) Description Karen, a coffee aficionado, wants to know the ...
- codeforces 816B Karen and Coffee (差分思想)
题目链接 816B Karen and Coffee 题目分析 题意:有个人在学泡咖啡,因此看了很多关于泡咖啡温度的书,得到了n种推荐的泡咖啡温度范围[L1,R1] ,此人将有k种做法推荐的温度记为可 ...
- CF 816B Karen and Coffee【前缀和/差分】
To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...
- BZOJ4627 前缀和 + 权值线段树
https://www.lydsy.com/JudgeOnline/problem.php?id=4627 题意:求序列中和在L到R之间的字串种数. 要求的是和的范围,我们可以考虑先求一个前缀和pre ...
- hdu 5475(打破固定思维OR线段树)
An easy problem Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- codeforces 816B.Karen and Coffee 解题报告
题目链接:http://codeforces.com/contest/816/problem/B 题目意思:给出 n 个recipes,第 i 个(1<= i <=n)recipes 表明 ...
- 【思维题 线段树】cf446C. DZY Loves Fibonacci Numbers
我这种maintain写法好zz.考试时获得了40pts的RE好成绩 In mathematical terms, the sequence Fn of Fibonacci numbers is de ...
- Karen and Coffee CodeForces - 816B (差分数组+预处理前缀和)
To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...
- Karen and Coffee CF 816B(前缀和)
Description To stay woke and attentive(专注的) during classes, Karen needs some coffee! Karen, a coffee ...
随机推荐
- mysql 多查询临时表的运用
SELECT * from (select count(*) imgCount1 from imagetable where SeriesID = '1201061992020630292018092 ...
- Python:内建函数zip
1.语法 zip([iterable,...]) [说明]:iterable——一个或多个迭代器 2.功能 zip()函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个的元组,然后返回由这 ...
- 5月5号周二课堂练习:简评cnblogs.com的用户体验
一.用户类型 在博客园上写博客,提问题,浏览感兴趣的博客帖子的活跃用户. 二.对cnblogs的期望 在博客园上写博客更流畅,制作手机版的APP可以随时随地在线浏览大牛们写的博客,提出的问题能更好的更 ...
- 安装cocoa pods
1.移除现有Ruby默认源 $gem sources --remove https://rubygems.org/ 2.使用新的源 $gem sources -a https://ruby.taoba ...
- ASP.NET MVC5 学习系列之表单和HTML辅助方法
一.表单 (一)Action和Method特性 Action特性用以告知浏览器信息发往何处,因此,Action特性后面需要包含一个Url地址.这里的Url地址可以是相对的,也可以是绝对的.如下Form ...
- tensorflow训练线性回归模型
tensorflow安装 tensorflow安装过程不是很顺利,在这里记录一下 环境:Ubuntu 安装 sudo pip install tensorflow 如果出现错误 Could not f ...
- HDU 1754 I Hate It 线段树(单点更新,成段查询)
题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=1754 题解: 单点更新,成段查询. 代码: #include<iostream> ...
- Scrum 项目准备3.0
SCRUM 流程的步骤2: Spring 计划 1. 确保product backlog井然有序.(参考示例图1) 2. Sprint周期,一个冲刺周期,长度定为两周,本学期还有三个冲刺周期. Spr ...
- week1词频统计
使用java完成对txt格式的英文短片进行字符提取及统计. package nenu.softWareProject; import java.io.*;import java.util.*; pub ...
- excel表中判断A列与B列内容是否相同,相同的话在C列按条件输出!
判断两列数据是否相同,有以下几个函数判断(做笔记于此,方便以后查找): 1.=IF(AND(A4=B4),"相同","") 在C列输出相同字符 2.=IF(A1 ...