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?

Input

The first line of input contains three integers, nk (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.

Output

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(前缀和,大量查询)的更多相关文章

  1. codeforces 816B Karen and Coffee (差分思想)

    题目链接 816B Karen and Coffee 题目分析 题意:有个人在学泡咖啡,因此看了很多关于泡咖啡温度的书,得到了n种推荐的泡咖啡温度范围[L1,R1] ,此人将有k种做法推荐的温度记为可 ...

  2. codeforces 816B.Karen and Coffee 解题报告

    题目链接:http://codeforces.com/contest/816/problem/B 题目意思:给出 n 个recipes,第 i 个(1<= i <=n)recipes 表明 ...

  3. 816B. Karen and Coffee 前缀和思维 或 线段树

    LINK 题意:给出n个[l,r],q个询问a,b,问被包含于[a,b]且这样的区间数大于k个的方案数有多少 思路:预处理所有的区间,对于一个区间我们标记其(左边界)++,(右边界+1)--这样就能通 ...

  4. CF 816B Karen and Coffee【前缀和/差分】

    To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...

  5. Karen and Coffee CodeForces - 816B (差分数组+预处理前缀和)

    To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...

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

  7. Karen and Coffee CF 816B(前缀和)

    Description To stay woke and attentive(专注的) during classes, Karen needs some coffee! Karen, a coffee ...

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

  9. CodeForces 816B 前缀和

    To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...

随机推荐

  1. Linux 最新SO_REUSEPORT特性

    1.前言 昨天总结了一下Linux下网络编程“惊群”现象,给出Nginx处理惊群的方法,使用互斥锁.为例发挥多核的优势,目前常见的网络编程模型就是多进程或多线程,根据accpet的位置,分为如下场景: ...

  2. 理解容器之间的连通性 - 每天5分钟玩转 Docker 容器技术(34)

    通过前面小节的实践,当前 docker host 的网络拓扑结构如下图所示,今天我们将讨论这几个容器之间的连通性. 两个 busybox 容器都挂在 my_net2 上,应该能够互通,我们验证一下: ...

  3. HTML行内元素、块状元素、行内块状元素的区别

    HTML可以将元素分类方式分为行内元素.块状元素和行内块状元素三种.首先需要说明的是,这三者是可以互相转换的,使用display属性能够将三者任意转换: (1)display:inline;转换为行内 ...

  4. 不让bat文件运行命令结束后cmd窗口自动关闭

    方法1假设你的bat名字叫aaa.bat你可以新开一个bat,内容是start aaa.bat然后这个新的bat是不会自动关闭的 方法2要执行bat后不退出,可以在bat里的最后添加pause命令,暂 ...

  5. Java中使用 Long 表示枚举类

    Java中使用 Long 表示枚举类 在日常的开发过程中,很多时候我们需要枚举类(enum)来表示对象的各种状态,并且每个状态往往会关联到指定的数字,如: private enum Color { R ...

  6. 从Java虚拟机的内存区域、垃圾收集器及内存分配原则谈Java的内存回收机制

    一.引言: 在Java中我们只需要轻轻地new一下,就可以为实例化一个类,并分配对应的内存空间,而后似乎我们也可以不用去管它,Java自带垃圾回收器,到了对象死亡的时候垃圾回收器就会将死亡对象的内存回 ...

  7. C语言精要总结-指针系列(二)

    此文为指针系列第二篇: C语言精要总结-指针系列(一) C语言精要总结-指针系列(二) 指针运算 前面提到过指针的解引用运算,除此之外,指针还能进行部分算数运算.关系运算 指针能进行的有意义的算术运算 ...

  8. 【LeetCode】217. Contains Duplicate

    题目: Given an array of integers, find if the array contains any duplicates. Your function should retu ...

  9. 6.vue如何上传到svn

    node_module是不需要上传的,先删掉,然后用tortoiseSvn的TortoiseSVN Repository Browser,ADD folder,选择工程文件,就行,checkout下来 ...

  10. jap页面获取struts2中action中变量的值

    在jsp页面中可以通过ONGL表达式获取struts2中action处理后的变量的值,这是因为每一个action在初始化后都会放到strackcontext中,可以通过ONGL表达式取到值. 注意要在 ...