codeforces 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 (差分思想)的更多相关文章
- CodeForces 816B Karen and Coffee(前缀和,大量查询)
CodeForces 816B Karen and Coffee(前缀和,大量查询) Description Karen, a coffee aficionado, wants to know the ...
- codeforces 816B.Karen and Coffee 解题报告
题目链接:http://codeforces.com/contest/816/problem/B 题目意思:给出 n 个recipes,第 i 个(1<= i <=n)recipes 表明 ...
- CF 816B Karen and Coffee【前缀和/差分】
To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...
- 816B. Karen and Coffee 前缀和思维 或 线段树
LINK 题意:给出n个[l,r],q个询问a,b,问被包含于[a,b]且这样的区间数大于k个的方案数有多少 思路:预处理所有的区间,对于一个区间我们标记其(左边界)++,(右边界+1)--这样就能通 ...
- 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
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 ...
- codeforces round #419 B. Karen and Coffee
To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...
- Karen and Coffee CF 816B(前缀和)
Description To stay woke and attentive(专注的) during classes, Karen needs some coffee! Karen, a coffee ...
随机推荐
- element-ui多重下拉列表的使用
template <div class="block"> <span class="demonstration">默认 click 触发 ...
- Luogu P5652 基础博弈练习题 (博弈论、图论)
题目链接 https://www.luogu.org/problem/P5652 题解 好题,想了四小时-- 首先考虑如何判断胜负: 首先假设只有一个柱子,那就是奇败偶胜.不难发现最后一个奇数后面的偶 ...
- AcWing:149. 荷马史诗(哈夫曼编码 + k叉哈夫曼树)
追逐影子的人,自己就是影子. ——荷马 达达最近迷上了文学. 她喜欢在一个慵懒的午后,细细地品上一杯卡布奇诺,静静地阅读她爱不释手的<荷马史诗>. 但是由<奥德赛>和<伊 ...
- HDU 5831 Rikka with Parenthesis II ——(括号匹配问题)
用一个temp变量,每次出现左括号,+1,右括号,-1:用ans来记录出现的最小的值,很显然最终temp不等于0或者ans比-2小都是不可以的.-2是可以的,因为:“))((”可以把最左边的和最右边的 ...
- Nginx数据结构之红黑树ngx_rbtree_t
1. 什么是红黑树? 1.1 概述 红黑树实际上是一种自平衡二叉查找树. 二叉树是什么?二叉树是每个节点最多有两个子树的树结构,每个节点都可以用于存储数据,可以由任 1 个节点访问它的左右 子树或父节 ...
- Lasso回归总结
Ridge回归 由于直接套用线性回归可能产生过拟合,我们需要加入正则化项,如果加入的是L2正则化项,就是Ridge回归,有时也翻译为岭回归.它和一般线性回归的区别是在损失函数上增加了一个L2正则化的项 ...
- docker启动常见报错
Docker启动时的报错汇总 22017.11.10 16:30:29字数 575阅读 27184 八个Docker常见故障 https://mp.weixin.qq.com/s/2GNKmRJtBG ...
- python3 格式化输出,字符串操作,模块,列表,元组
初识python,在网上看了老男孩的视频,建立一个博客将自己所学的python知识记录下来. input加密,用于输入密码的阶段. import getpass user = input(" ...
- mysql 创建相同的表结构
前言: 项目中用到分表存储,需要创建100张表,每个表的结构相同,原始操作,一个个复制粘贴,修改名字.今天DBA给了意见 create table a like b 将b的表结构和索引都复制 cre ...
- leetcode 1278 分割回文串
time O(n^2*k) space O(n^2) class Solution { public: int palindromePartition(string s, int K) { //分成 ...