CodeForces 816B Karen and Coffee(前缀和,大量查询)
CodeForces 816B Karen and Coffee(前缀和,大量查询)
Description
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.
Sample
input output input output
题意:
给出n个区间,q个查询区间,问每次查询时,该查询区间内有多少个点至少被k个区间覆盖。
样例1:第一行三个整数的意义是:3个区间 至少被2个区间覆盖 查询四次,第二三四行分别是三个区间。剩下四行为查询的区间,比如92 94就是在92到94中(包含92和94),多少个数被上面区间覆盖了2次(即k次)或者2次以上。在92到94的区间内92 93 94都被覆盖两次,故三个,输出3。
思路:
数据量是2*10^5,暴力的方法肯定会超时。
准备两个一维数组,cnt[] 和 sum[] 来处理每一个满足数 i (范围 [1, 200000] )
cnt[i] 数组:第 i 个数被区间包含的次数
sum[i] 数组: 前 i 个数被 k 个包含的前缀和。
第一组样例就是

所以对于 k 个提问,sum[b] - sum[a-1] 就是答案了(因为sum[b]是0到b中符合条件的总数,sum[a-1](包含两个端点)是0到a中符合条件的总数,两者相减就是a到b中符合条件的总数)。
这里最巧妙之处就是cnt[i] 要怎么统计出来。如果 [li, ri] 范围的数都遍历一次,绝对会超时, 处理两个点其实就可以统计出来了,分别是 cnt[li], cnt[ri+1]。 对于每一次询问,进行:cnt[li]++, cnt[ri+1]-- 处理。然后 q 个问题之后,再统一遍历多一次,利用前一个数 cnt[i-1] 就能统计出当前数 cnt[i] 了。
代码:
#include<stdio.h>
#include<iostream>
using namespace std;
int a[200010];
int c[200010];
int main()
{
int k,n,q;
int b,e;
cin>>k>>n>>q;
for(int i=0; i<k; i++)//输入查询
{
cin>>b>>e;
a[b]++;
a[e+1]--;
}
for(int i=1; i<200010; i++)//更新cnt数组
{
a[i]+=a[i-1];
if(a[i]>=n)
c[i]++;
}
for(int i=1; i<200010; ++i)//更新sum数组
c[i]+=c[i-1];
for(int i=0; i<q; i++)//输出结果
{
int l,r;
scanf("%d%d",&l,&r);
cout<<c[r]-c[l-1]<<endl;
}
}
CodeForces 816B Karen and Coffee(前缀和,大量查询)的更多相关文章
- codeforces 816B Karen and Coffee (差分思想)
题目链接 816B Karen and Coffee 题目分析 题意:有个人在学泡咖啡,因此看了很多关于泡咖啡温度的书,得到了n种推荐的泡咖啡温度范围[L1,R1] ,此人将有k种做法推荐的温度记为可 ...
- codeforces 816B.Karen and Coffee 解题报告
题目链接:http://codeforces.com/contest/816/problem/B 题目意思:给出 n 个recipes,第 i 个(1<= i <=n)recipes 表明 ...
- 816B. Karen and Coffee 前缀和思维 或 线段树
LINK 题意:给出n个[l,r],q个询问a,b,问被包含于[a,b]且这样的区间数大于k个的方案数有多少 思路:预处理所有的区间,对于一个区间我们标记其(左边界)++,(右边界+1)--这样就能通 ...
- CF 816B Karen and Coffee【前缀和/差分】
To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...
- Karen and Coffee CodeForces - 816B (差分数组+预处理前缀和)
To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...
- Codeforces Round #419 (Div. 2) B. Karen and Coffee(经典前缀和)
http://codeforces.com/contest/816/problem/B To stay woke and attentive during classes, Karen needs s ...
- Karen and Coffee CF 816B(前缀和)
Description To stay woke and attentive(专注的) during classes, Karen needs some coffee! Karen, a 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 816B 前缀和
To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...
随机推荐
- node.js如何制作命令行工具(一)
之前使用过一些全局安装的NPM包,安装完之后,可以通过其提供的命令,完成一些任务.比如Fis3,可以通过fis3 server start 开启fis的静态文件服务,通过fis3 release开启文 ...
- Blockly编程:用Scratch制作游戏愤怒的小牛(小鸟)
愤怒的小鸟曾经很热门,网上还说他是程序员最喜欢玩的游戏.最先我是WIKIOI的评测页面看到他的,后来在2014年全国信息学奥林匹克联赛第一天第三题飞扬的小鸟也看到了它.因此,突然想做一个类似愤怒的小鸟 ...
- Docker Machine 详解
笔者在<Docker Machine 简介>一文中简单介绍了 Docker Machine 及其基本用法,但是忽略的细节实在是太多了.比如 Docker 与 Docker Machine ...
- ZooKeeper源码分析-Jute-第二部分
数据类型和流 本部分描述支持Hadoop的基础数据类型以及复合类型.我们的目的是支持一系列的类型,可以用于在不同的编程语言中简化和有效表达一定范围的记录类型. 基础类型 大部分情况下,Hadoop的大 ...
- 大话Python正则表达式
python的正则表达式模块re import re match_object=re.compile(r"") result=re.match(match_object," ...
- MySQL left join操作中 on与where放置条件的区别
优先级 两者放置相同条件,之所以可能会导致结果集不同,就是因为优先级.on的优先级是高于where的. 1 1 首先明确两个概念: LEFT JOIN 关键字会从左表 (table_name1) 那里 ...
- 警惕System.Environment.CurrentDirectory 获取当前目录
最近工作中,要做个客户端提醒的小工具:winform程序自然少不了要读取和应用程序同一个目录的配置文件(不是exe.config文件): 要读取当前应用程序所在目录我立马想到了System.Envir ...
- 使用c#解析json库
写了个c#版的json解析库,提供了json到hashtable以及hashtable到json字符串的转换 受惠于c#的语法特性,hashtable到json的解析变得非常简单 先判断传入的obje ...
- 【Android Developers Training】 15. 启动一个Activity
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 从栈不平衡问题 理解 calling convention
最近在开发的过程中遇到了几个很诡异的问题,造成了栈不平衡从而导致程序崩溃. 经过几经排查发现是和调用规约(calling convention)相关的问题,特此分享出来. 首先,讲一下什么是调用规约. ...