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

题目意思:给出 n 个recipes,第 i 个(1<= i <=n)recipes 表明 coffee 调制的推荐温度范围是 [li, ri] 之间。现在有 q 个问题,每个问题需要回答 coffee 在范围 [a, b] 之间,共有多少个数满足至少有 k 个推荐。

题目解析:这题主要是考我们对于大范围(最大200000),如何处理数据。方法是很容易想到的,但要考虑优化,即离线处理。20w * 20w,直接做是很容易超时啦~~~(我就交了好几个 TLE 了,以为不会超过2.5 秒的,看来时间评估也很重要哇,囧 )

  需要准备两个一维数组,cnt[] 和 sum[] 来处理每一个满足数 i (范围 [1, 200000]  )

  cnt[i] 数组:第 i 个数获得的推荐数

  sum[i] 数组: 前 i 个数满足 k 个推荐的前缀和。

  拿 test1 的测试数据来分析,请看下面的图

  

所以对于 k 个提问,sum[b] - sum[a-1] 就是答案了。

  这里最巧妙之处就是cnt[i] 要怎么统计出来。如果 [li, ri] 范围的数都遍历一次,绝对会TLE的!!! 处理两个点其实就可以统计出来了,分别是 cnt[li], cnt[ri+1]。 对于每一次询问,进行:cnt[li]++, cnt[ri+1]-- 处理。然后 q 个问题之后,再统一遍历多一次,利用前一个数 cnt[i-1] 就能统计出当前数 cnt[i] 了。(这个方法我也是第一次见,厉害厉害~~~以后就会敏感点了 ^___^)

  

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = 2e5 + ;
int cnt[maxn];
int sum[maxn]; int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE
int n, k, q; while (cin >> n >> k >> q) {
int l, r;
memset(cnt, , sizeof(cnt));
for (int i = ; i < n; i++) {
scanf("%d%d", &l, &r);
cnt[l]++;
cnt[r+]--;
}
memset(sum, , sizeof(sum));
for (int i = ; i <= ; i++) {
cnt[i] += cnt[i-];
sum[i] = (cnt[i]>=k ? sum[i-]+: sum[i-]);
}
int a, b;
while (q--) {
scanf("%d%d", &a, &b);
printf("%d\n", sum[b]-sum[a-]);
}
}
return ;
}

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 (差分思想)

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

  3. Codeforces Educational Round 92 赛后解题报告(A-G)

    Codeforces Educational Round 92 赛后解题报告 惨 huayucaiji 惨 A. LCM Problem 赛前:A题嘛,总归简单的咯 赛后:A题这种**题居然想了20m ...

  4. codeforces 476C.Dreamoon and Sums 解题报告

    题目链接:http://codeforces.com/problemset/problem/476/C 题目意思:给出两个数:a 和 b,要求算出 (x/b) / (x%b) == k,其中 k 的取 ...

  5. Codeforces Round #382 (Div. 2) 解题报告

    CF一如既往在深夜举行,我也一如既往在周三上午的C++课上进行了virtual participation.这次div2的题目除了E题都水的一塌糊涂,参赛时的E题最后也没有几个参赛者AC,排名又成为了 ...

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

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

  7. codeforces 507B. Amr and Pins 解题报告

    题目链接:http://codeforces.com/problemset/problem/507/B 题目意思:给出圆的半径,以及圆心坐标和最终圆心要到达的坐标位置.问最少步数是多少.移动见下图.( ...

  8. codeforces 500B.New Year Permutation 解题报告

    题目链接:http://codeforces.com/problemset/problem/500/B 题目意思:给出一个含有 n 个数的排列:p1, p2, ..., pn-1, pn.紧接着是一个 ...

  9. codeforces B. Xenia and Ringroad 解题报告

    题目链接:http://codeforces.com/problemset/problem/339/B 题目理解不难,这句是解题的关键 In order to complete the i-th ta ...

随机推荐

  1. php获取本地IP

    function get_local_ip() { $preg = "/\A((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\.){3 ...

  2. shell输出颜色

    #!/bin/bash # #下面是字体输出颜色及终端格式控制 #字体色范围:- echo -e "\033[30m 黑色字 \033[0m" echo -e "\033 ...

  3. 微信支付 超时 mysql.event

    $wtime 使用具体timestamp //rand 防推测 $wev = 'ev_gbuy_create_' . trim($winsert_id) . rand(100, 999); $sql ...

  4. Redis配置文件的使用

    Redis基本配置 常规配置 进到配置文件下 vi /etc/redis.conf 写入配置项 port 1111 # 配置端口号 daemonize yes # 是否后台运行 daemonize y ...

  5. MongoDB-2:MongoDB添加、删除、修改

    一.简介 MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSQL数据库产品中最热门的一种.数据被分组存储在数据集中,被称为一个集合(Collenction)和对于存储在MongoDB ...

  6. Java集合的遍历方式

    Map的遍历 1.通过map.entrySet遍历Key和Value Map<Integer,Integer> map = new HashMap<>(); map.put(1 ...

  7. (4.17)sql server中的uuid获取与使用

    sql server中的uuid  建表: 1.自增长 studentno int primary key identity(1,1)——bigint也是可以的 2.创建uuidcustomerid  ...

  8. Winio.dll的使用

    Winio.dll的使用 WinIO程序库允许在32位的Windows应用程序中直接对I/O端口和物理内存进行存取操作.通过使用一种内核模式的设备驱动器和其它几种底层编程技巧,它绕过了Windows系 ...

  9. 转:css中!important的作用

    转:http://www.cnblogs.com/guoguo-15/archive/2011/08/24/2151859.html {*rule !important}这个css规则当今在网页制作的 ...

  10. 5. Longest Palindromic Substring(最长回文子串 manacher 算法/ DP动态规划)

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...