Codeforces Round #419 (Div. 2)_Karen and Coffee
来源:互联网 发布:诸神黄昏神翼进阶数据 编辑:程序博客网 时间:2024/06/05 14:54
惯例,先上题:
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?
The first line of input contains three integers, n, k (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.
For each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive.
3 2 491 9492 9797 9992 9493 9795 9690 100
3304
2 1 11 1200000 20000090 100
0
翻译成中文意思就是:给很多区间,只有区间的重叠次数大于等于K被询问时才能被被选择,答案输出询问的区间中可以有多少个数可以被选择。
这个题目给了2.5s,然后看一下数据量,也就知道这2.5s也不算是特别长,所以传统暴力当然不可以啦!
解决问题:
①区间重叠次数的简单计算(如果用循环++肯定超时)
②被询问时重叠数量的简单计算(当然也不能用for暴力)
对于①,我们可以只把给定区间的左端点++,右端点加1处--,然后dp一遍,就可以知道区间重复多少次了。
对于②,我们可以把大于等于K的区域做dp,然后询问时右端点减去左端点就可以了。
上代码:
#include<cstdio>#include<iostream>using namespace std;const int N = 2e5+5;int a[N];int main(){int n,k,q;int l,r;scanf("%d %d %d",&n,&k,&q);for(int i = 1;i<= n;i++){scanf("%d %d",&l,&r);a[l]++;//起点++a[r+1]--;//重点后一个--,可以截止区间}for(int i = 1;i<= N;i++)//这里为了简便,直接从1到N,其实可以从区间最小到区间最大+1a[i]+= a[i-1];//计算每个点的重叠次数for(int i = 1;i<= N;i++){a[i]= a[i-1]+(a[i]>= k);//把大于等于K的点做一个累加}while(q--){scanf("%d %d",&l,&r);printf("%d\n",a[r]-a[l-1]);//前面累加了,只需末减初就可以了}return 0;}
如有不清楚,欢迎指教。
- Codeforces Round #419 (Div. 2)_Karen and Coffee
- Codeforces Round #419 (Div. 2)Karen and Coffee
- Codeforces Round #419 (Div. 2) B. Karen and Coffee
- Codeforces Round #419 (Div. 2)Karen and Coffee
- Codeforces Round #419 (Div. 2) B. Karen and Coffee 预处理+技巧
- [前缀和] Codeforces Round #419 (Div. 2)B. Karen and Coffee
- Codeforces Round #419 (Div. 2) B. Karen and Coffee (差分标记)
- Codeforces Round #419 (Div. 2) B. Karen and Coffee【前缀和求区间覆盖次数】
- CodeForces #419 Div.2 B Karen and Coffee 差分 巧妙
- Codeforces#419 Karen and Coffee
- Karen and Coffee codeforces
- Codeforces Round #419 (Div. 2) C.Karen and Game 思维
- Codeforces Round #419 (Div. 2)Karen and Game
- Codeforces Round #419 (Div. 2) C. Karen and Game 题解
- Codeforces Round #419 (Div. 2) A. Karen and Morning
- Codeforces Round #419 (Div. 2) C. Karen and Game【贪心】
- Codeforces Round #419 (Div. 2)
- Codeforces Round #419 (Div. 2)
- HDU 5517 Triple (线段树)
- 搜索插入位置
- 【Gradle】eclipse导入Gradle项目
- 机器学习实战-使用朴素贝叶斯分类器来做垃圾邮件分类
- 我的网络编程学习之路——getsockname 和 getpeername 函数
- Codeforces Round #419 (Div. 2)_Karen and Coffee
- windows的grep命令Select-String
- SQL server中设置自增字段
- storm示例之DRPC
- 欢迎使用CSDN-markdown编辑器
- Android JNI HelloWorld
- 设计模式——工厂模式
- c++远征之模板篇——标准模板库(STL)
- 51nod1479 小Y的数论题