题目链接:http://codeforces.com/contest/816/problem/B

题意:给出n个范围,q个查询问查询区间出现多少点在给出的n个范围中至少占了k次

题解:很显然的一道题目,可能会想到用莫队,或者分块来写,但是这样会超时。这题有个技巧,可以考虑用前缀和来求。

首先在n个范围中给出了l,r,用一个数组a[],a[l]++,a[r+1]--,然后求前缀表示前i个有多少个超过k的。然后就简单了,具体看一下代码。

总而言之想法很重要。

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int M = 2e5 + 10;
int a[M] , have[M];
int main() {
int n , k , q;
scanf("%d%d%d" , &n , &k , &q);
for(int i = 0 ; i < n ; i++) {
int l , r;
scanf("%d%d" , &l , &r);
a[l]++;
a[r + 1]--;
}
have[0] = 0;
int pre = 0;
for(int i = 0 ; i < M ; i++) {
pre += a[i];
if(i) have[i] = have[i - 1];
if(pre >= k) {
have[i]++;
}
}
for(int i = 0 ; i < q ; i++) {
int l , r;
scanf("%d%d" , &l , &r);
printf("%d\n" , have[r] - have[l - 1]);
}
return 0;
}

codeforces 816 B. Karen and Coffee(思维)的更多相关文章

  1. codeforces 816 D. Karen and Test(逆元+思维+组合数)

    题目链接:http://codeforces.com/contest/816/problem/D 题解:显然一看到这题应该会想到是有什么规律的于是多写几项就会发现偶数列之间是有关系的. 满足a[i][ ...

  2. codeforces 816 C. Karen and Game(模拟+思维)

    题目链接:http://codeforces.com/contest/816/problem/C 题意:给出一个矩阵,问能否从都是0的情况下只将一整行+1或者一整列+1变形过来,如果可以输出需要步数最 ...

  3. 【codeforces 816B】Karen and Coffee

    [题目链接]:http://codeforces.com/contest/816/problem/B [题意] 给你很多个区间[l,r]; 1<=l<=r<=2e5 一个数字如果被k ...

  4. codeforces 816 E. Karen and Supermarket(树形dp)

    题目链接:http://codeforces.com/contest/816/problem/E 题意:有n件商品,每件有价格ci,优惠券di,对于i>=2,使用di的条件为:xi的优惠券需要被 ...

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

  6. CodeForces 816B Karen and Coffee(前缀和,大量查询)

    CodeForces 816B Karen and Coffee(前缀和,大量查询) Description Karen, a coffee aficionado, wants to know the ...

  7. codeforces round #419 B. Karen and Coffee

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

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

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

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

随机推荐

  1. Asp.Net MVC HttpPost用法

    一个Action只能用一个http 特性,例如:HttpPost 不能与HttpGet 或者多个HttpPost重复使用,否则会出错 也可以用 [AcceptVerbs("put" ...

  2. 一道看似简单的go程序的深入分析

    先上代码: func main() { var a [10]int for i := 0; i < 10; i++ { go func(i int) { for { a[i]++ } }(i) ...

  3. java基础知识总结,绝对经典

    写代码: 1,明确需求.我要做什么? 2,分析思路.我要怎么做?1,2,3. 3,确定步骤.每一个思路部分用到哪些语句,方法,和对象. 4,代码实现.用具体的java语言代码把思路体现出来. 学习新技 ...

  4. Unity实现放大缩小以及相机位置平移实现拖拽效果

    放大缩小功能是游戏开发中用到的功能,今天就来讲一下Unity中放大缩小怎么实现. 1.IDragHandler, IBeginDragHandler, IEndDragHandler这三个接口是Uni ...

  5. Elasticsearch索引增量统计及定时邮件实现

    0.需求 随着ELKStack在应用系统中的数据规模的急剧增长,每天千万级别数据量(存储大小:10000000*10k/1024/1024=95.37GB,假设单条数据10kB,实际远大于10KB)的 ...

  6. LeetCode——409. Longest Palindrome

    题目: Given a string which consists of lowercase or uppercase letters, find the length of the longest ...

  7. LeetCode——540. Single Element in a Sorted Array

    题目:Given a sorted array consisting of only integers where every element appears twice except for one ...

  8. 通过Blazor使用C#开发SPA单页面应用程序(1)

    2019年9月23——25日 .NET Core 3.0即将在.NET Conf上发布! .NET Core的发布及成熟重燃了.net程序员的热情和希望,一些.net大咖也在积极的为推动.NET Co ...

  9. Linux安装nfs共享文件

    简介nfs nfs网络文件系统常用于共享音视频,图片等静态资源.将需要共享的资源放到NFS里的共享目录,通过服务器挂载实现访问. 服务端安装: yum install -y nfs-utils rpc ...

  10. 100天搞定机器学习|day39 Tensorflow Keras手写数字识别

    提示:建议先看day36-38的内容 TensorFlow™ 是一个采用数据流图(data flow graphs),用于数值计算的开源软件库.节点(Nodes)在图中表示数学操作,图中的线(edge ...