题目链接

816B Karen and Coffee

题目分析

题意:有个人在学泡咖啡,因此看了很多关于泡咖啡温度的书,得到了n种推荐的泡咖啡温度范围[L1,R1] ,此人将有k种做法推荐的温度记为可用温度(个人翻译),然后给出q次询问,问区间[L2,R2]内的温度,有多少个温度是可用温度(每个整数代表一个温度)

思路:一开始用的是线段树写的,不过姿势不对,TLE了,然后改过来后,发现时间比较长,就考虑一下优化的方法。

比线段树某些功能更优的算法:差分思想,在对某一区间每个位置上的数加上一个值x,并求任意位置的数的时候,时间上比线段树 O(nlogn)的复杂度更低,为O(n),空间更小,代码更短,很合适。

那么如何运用差分思想来写这个题呢?首先用O(n)的时间求出每个温度有多少种做法推荐,然后维护一个前缀和sum[i],记录区间 [1, i ] 表示的温度中有多少个温度满足条件(即有多少个温度有k种以上做法推荐),用O(n)的时间求出sum数组,sum[i] = sum[i-1] + (val[i] >= k ? 1 : 0;) ,最后查询区间[ L2,R2]有多少适合的温度时,输出 sum[R2] - sum[L2-1] 。

(不了解什么是差分的话,可以看下我的这篇博客 差分+树上差分

代码区

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<string>
#include<fstream>
#include<vector>
#include<stack>
#include <map>
#include <iomanip>
#define bug cout << "**********" << endl
#define show(x,y) cout<<"["<<x<<","<<y<<"] "
//#define LOCAL = 1;
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll mod = 1e9 + ;
const int Max = 2e5 + ; int n, k, q;
int val[Max], sum[Max]; //val为差分数组,sum[i]记录区间[1,i]有多少个温度被确定为合适 int main()
{
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
while (scanf("%d%d%d", &n, &k, &q) != EOF)
{
memset(val, , sizeof(val));
memset(sum, , sizeof(sum)); for (int i = , l, r; i <= n;i++)
{
scanf("%d%d", &l, &r);
val[l]++;val[r + ]--;
}
for (int i = ;i <= ;i++)
{
val[i] += val[i - ]; //差分数组的前缀和即为该点的值
sum[i] = sum[i-] + (val[i] >= k ? : );
}
for(int i = ,l,r;i <= q; i ++)
{
scanf("%d%d", &l, &r);
printf("%d\n", sum[r] - sum[l-]);
}
}
return ;
}

codefoeces 618B Karen and Coffee

codeforces 816B Karen and Coffee (差分思想)的更多相关文章

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

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

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

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

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

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

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

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

  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

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

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

  8. codeforces round #419 B. Karen and Coffee

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

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

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

随机推荐

  1. Liblinear Visual studio 2013 Error C3057

    使用LibLinear时编译时出现Error C3057的错误: OpenMP报错,查询MSDN: #pragma omp threadprivate(var)//var在编译之前必须是确定值 所以修 ...

  2. Count the Buildings

    K - Count the Buildings 参考:Count the Buildings 思路可以借鉴,但是代码略有问题 写的时候 re 了 9 发,然后把变量定义的顺序换了一下居然 A 了,以为 ...

  3. Latex里面的\newtheorem*{xx}{yy}后面的*是干什么的?

    答案: 加了*表示不标号.例如: 同样使用命令: \begin{prb} xx\end{prb} 1. \newtheorem{prb}{Problem Formulation} → Problem ...

  4. Vue_(组件)自定义指令

    Vue.js自定义指令 传送门 自定义指令:除了内置指令,Vue也允许用户自定义指令 注册指令:通过全局API Vue.directive可以注册自定义指令 自定义指令的钩子函数参数:自定义指令的钩子 ...

  5. CodeForces 714E Sonya and Problem Wihtout a Legend(单调数列和DP的小研究)

    题意:给你n个数字,每个数字可以加减任何数字,付出变化差值的代价,求最后整个序列是严格单调递增的最小的代价. 首先我们要将这个题目进行转化,因为严格单调下是无法用下面这个dp的方法的,因此我们转化成非 ...

  6. Centos-Redhat下远程桌面的方法 & Redhat改Centos源

    折腾了好几天才搞定,Redhat下远程桌面的方法,首先保证本身已经装了桌面,并且可以ssh访问 由于系统中自带python2环境,装了anaconda以及它带的python3环境,这个必须存在(前提) ...

  7. Tree-based Model 如何处理categorical variable

    categorical variable 分为 order variale 和 non-order variable,其中order variable直接使用sklearn.preprocess.La ...

  8. 0.JQuery学习

    jQuery 教程 jQuery 是一个 JavaScript 库. jQuery 极大地简化了 JavaScript 编程. jQuery 简介 jQuery 库可以通过一行简单的标记被添加到网页中 ...

  9. leetcode题目19.删除链表的倒数第N个节点(中等)

    题目描述: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后 ...

  10. Groovy脚本基础全攻略

    1 背景 Groovy脚本基于Java且拓展了Java,所以从某种程度来说掌握Java是学习Groovy的前提,故本文适用于不熟悉Groovy却想快速得到Groovy核心基础干货的Java开发者(注意 ...