Codeforces Round #419 (Div. 2) B. Karen and Coffee(经典前缀和)

Karen, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. Indeed, she has spent some time reading several recipe books, including the universally acclaimed "The Art of the Covfefe".
She knows n coffee recipes. The i-th recipe suggests that coffee should be brewed between li and ri degrees, inclusive, to achieve the optimal taste.
Karen thinks that a temperature is admissible if at least k recipes recommend it.
Karen has a rather fickle mind, and so she asks q questions. In each question, given that she only wants to prepare coffee with a temperature between a and b, inclusive, can you tell her how many admissible integer temperatures fall within the range?
The first line of input contains three integers, n, k (1 ≤ k ≤ n ≤ 200000), and q (1 ≤ q ≤ 200000), the number of recipes, the minimum number of recipes a certain temperature must be recommended by to be admissible, and the number of questions Karen has, respectively.
The next n lines describe the recipes. Specifically, the i-th line among these contains two integers li and ri (1 ≤ li ≤ ri ≤ 200000), describing that the i-th recipe suggests that the coffee be brewed between li and ri degrees, inclusive.
The next q lines describe the questions. Each of these lines contains a and b, (1 ≤ a ≤ b ≤ 200000), describing that she wants to know the number of admissible integer temperatures between a and b degrees, inclusive.
For each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive.
3 2 4
91 94
92 97
97 99
92 94
93 97
95 96
90 100
3
3
0
4
2 1 1
1 1
200000 200000
90 100
0
给出n个区间,然后进行q次区间查询,问每次查询区间中的数至少在k个区间中出现的个数。
思路:
前缀和的经典应用。
也就是++cnt【l】和--cnt【r+1】,很妙。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn = +; int n, k, q;
int cnt[maxn];
int sum[maxn]; int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d%d%d",&n, &k, &q))
{
memset(cnt,,sizeof(cnt));
for(int i=;i<n;i++)
{
int l, r;
scanf("%d%d",&l,&r);
++cnt[l];
--cnt[r+];
} memset(sum,,sizeof(sum));
for(int i=;i<=maxn;i++)
{
cnt[i]+=cnt[i-];
if(cnt[i]>=k) sum[i]=;
sum[i]+=sum[i-];
} while(q--)
{
int l, r;
scanf("%d%d",&l, &r);
printf("%d\n",sum[r]-sum[l-]);
}
}
return ;
}
Codeforces Round #419 (Div. 2) B. Karen and Coffee(经典前缀和)的更多相关文章
- Codeforces Round #419 (Div. 2) B. Karen and Coffee
To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...
- Codeforces Round #419 (Div. 2) C. Karen and Game
C. Karen and Game time limit per test 2 seconds memory limit per test 512 megabytes input standard i ...
- Codeforces Round #419 (Div. 2) E. Karen and Supermarket(树形dp)
http://codeforces.com/contest/816/problem/E 题意: 去超市买东西,共有m块钱,每件商品有优惠卷可用,前提是xi商品的优惠券被用.问最多能买多少件商品? 思路 ...
- Codeforces Round #419 (Div. 2) A. Karen and Morning(模拟)
http://codeforces.com/contest/816/problem/A 题意: 给出一个时间,问最少过多少时间后是回文串. 思路: 模拟,先把小时的逆串计算出来: ① 如果逆串=分钟, ...
- Codeforces Round #419 (Div. 1) C. Karen and Supermarket 树形DP
C. Karen and Supermarket On the way home, Karen decided to stop by the supermarket to buy some g ...
- 【找规律】【递推】【二项式定理】Codeforces Round #419 (Div. 1) B. Karen and Test
打个表出来看看,其实很明显. 推荐打这俩组 11 1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000 1000000000 ...
- 【贪心】 Codeforces Round #419 (Div. 1) A. Karen and Game
容易发现,删除的顺序不影响答案. 所以可以随便删. 如果行数大于列数,就先删列:否则先删行. #include<cstdio> #include<algorithm> usin ...
- Codeforces Round #419 (Div. 2) A-E
上紫啦! E题1:59压哨提交成功翻盘 (1:00就做完了调了一个小时,还好意思说出来? (逃)) 题面太长就不复制了,但是配图很可爱所以要贴过来 九条可怜酱好可爱呀 A - Karen and Mo ...
- Codeforces Round #419 (Div. 2)
1.题目A:Karen and Morning 题意: 给出hh:mm格式的时间,问至少经过多少分钟后,该时刻为回文字符串? 思路: 简单模拟,从当前时刻开始,如果hh的回文rh等于mm则停止累计.否 ...
随机推荐
- Java初学者笔记二:关于类的常见知识点汇总
一.Java的类: Java的类是Java的基本概念了,基本的定义语法我就不提了,自己也不会忘了的,下面分成几个模块介绍: 1.Java的类定义时候的修饰符 2.Java的类的继承与派生 二.Java ...
- 正则表达式—RegEx(RegularExpressio)(一)
今日随笔,想和大家分享一下正则表达式的相关知识. 先不说概念性的东西,举一个例子再说. 验证你输入的邮政编码 ,你输入的邮政编码必须是六位的数字. while (true) { Console.Wri ...
- 使用Eclipse(以及intellij IDEA)配合JDWP对服务器上部署的代码进行调试
今天遇到了一个问题:同样的代码,在服务器上跑的时候会报空指针异常,但是在本地是没有问题的,看服务器上打印的日志只能看到异常信息,不能准确地定位到出问题的代码,于是就搜索了一下远程调试.结果还真的可以在 ...
- 常用的sass编译库
@charset "UTF-8"; /*引进图片合并给一个变量(后面会用到这个变量)*/ $sprites:sprite-map("pwd/*.png",$sp ...
- JavaScript获取地址栏的参数!
第一种方式:手动解析 “location”对象 console.log(window.location); 使用这个对象做跳转: window.location.href=""; ...
- Hadoop集群搭建文档
环境: Win7系统装虚拟机虚拟机VMware-workstation-full-9.0.0-812388.exe Linux系统Ubuntu12.0.4 JDK j ...
- 【转载】国外程序员整理的Java资源大全
以下转载自: 推荐!国外程序员整理的Java资源大全中文版 https://github.com/akullpp/awesome-java英文版 Java 几乎是许多程序员们的入门语言,并且也是 ...
- react路由守卫
react没有vue那样的路由钩子beforeEach,实现登陆验证. 实现效果:如果没有登陆,就跳转到登陆界面,如果登陆过浏览器存有登陆信息就跳转到所输入的路由界面,如果该路由不存在则跳到404页面 ...
- 170704、springboot编程之CommandLineRunner
实际应用中,我们会有在项目服务启动的时候就去加载一些数据或做一些事情这样的需求. 为了解决这样的问题,Spring Boot 为我们提供了一个方法,通过实现接口 CommandLineRunner 来 ...
- 热词统计以及Quartz.net的简单使用
一.热词统计 方案一: 设计一个表:ID KeyWord Count 当用户再输入框中查询的时候,我们就往表中插入数据,在插入之前首先判断是否已经存在keyword,存在的话,让C ...