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 ...
随机推荐
- border、margin、padding三者的区别
当你写前端的时候,肯定会遇到border,margin和padding这几个单词. 如: padding: 16px 0; margin: 0 10px; 在CSS中,他们是表示距离的东西,很多人刚开 ...
- Python:生成器的简单理解
一.什么是生成器 在Python中,由于受到内存的限制,列表容量肯定是有限的.例如我们创建一个包含一亿个元素的列表,Python首先会在内存中开辟足够的空间来存储这个包含一亿个元素的列表,然后才允许用 ...
- 【线段树求区间第一个不大于val的值】Lpl and Energy-saving Lamps
https://nanti.jisuanke.com/t/30996 线段树维护区间最小值,查询的时候优先向左走,如果左边已经找到了,就不用再往右了. 一个房间装满则把权值标记为INF,模拟一遍,注意 ...
- ubuntu16.04卸载火狐,Amazon
一.卸载火狐: . dpkg --get-selections |grep firefox .sudo apt-get purge firefox unity-scope-firefoxbookmar ...
- 利用Docker安装Web前端性能测试工具Sitespeed.io
目录结构 一.Sitespeed.io概述 1.Sitespeed.io简介 2.Sitespeed.io使用场景 二.Sitespeed.io的安装和使用 1.安装Sitespeed.io 2.连接 ...
- C 语言疑难杂症 [转:http://blog.chinaunix.net/uid-20688544-id-1894880.html]
无聊在网上找了些C语言的东东练一下手,竟然发现其实还有好多细节之前,没注意到,该好好复习一下先. 解决掉的问题先不发出来,把疑问的先做个笔记,过几天解决了就回来修改补上. #include < ...
- solr4.2增量索引之同步(修改,删除,新增)--转载
原文地址:http://www.jiancool.com/article/12743229775/;jsessionid=14E9B3F1BB33399799884B5C8F15DDE1 solr增 ...
- presence_of_element_located与visibility_of_element_located区别
selenium 问题:加了显性等待后,操作元素依然出错 背景: 用WebDriverWait时,一开始用的是presence_of_element_located,我对它的想法就是他就是用来等待 ...
- CF605E Intergalaxy Trips 贪心 概率期望
(当时写这篇题解的时候,,,不知道为什么,,,写的非常冗杂,,,不想改了...) 题意:一张有n个点的图,其中每天第i个点到第j个点的边都有$P_{i, j}$的概率开放,每天可以选择走一步或者留在原 ...
- Codeforces711
A ZS the Coder and Chris the Baboon are travelling to Udayland! To get there, they have to get on th ...