CodeForces-816B:Karen and Coffee (简单线段树)
To stay woke and attentive during classes, Karen needs some coffee!
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?
Input
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.
Output
For each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive.
Example
3 2 4
91 94
92 97
97 99
92 94
93 97
95 96
90 100
3
3
0
4
2 1 1
1 1
200000 200000
90 100
0
题意:N,K,Q;给出N个区间;Q次询问,对每次询问,问[X,Y]区间里有多少个数在N个区间出现次数大于等于K。
思路:对N个区间,前缀和处理,大于等于K的插入线段树。
。。:主要是图美,所以想做一下。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int a[maxn+],sum[(maxn+)<<];
void read(int &res){
char c=getchar();
for(;c>''||c<'';c=getchar());
for(res=;c>=''&&c<='';c=getchar()) res=(res<<)+(res<<)+c-'';
}
struct Segment_Tree
{
void add(int Now,int L,int R,int l,int r)
{
if(L==R){
sum[Now]++;
return ;
}
int Mid=(L+R)>>;
if(l<=Mid) add(Now<<,L,Mid,l,r);
if(r>Mid) add(Now<<|,Mid+,R,l,r);
sum[Now]=sum[Now<<]+sum[Now<<|];
}
int query(int Now,int L,int R,int l,int r)
{
if(l<=L&&r>=R)
return sum[Now];
int Mid=(L+R)>>,tmp=;
if(l<=Mid) tmp+=query(Now<<,L,Mid,l,r);
if(r>Mid) tmp+=query(Now<<|,Mid+,R,l,r);
return tmp;
}
}Tree;
int main()
{
int N,M,K,Q,x,y;
read(N); read(K); read(Q);
for(int i=;i<=N;i++){
read(x); read(y);
a[x]++; a[y+]--;
}
for(int i=;i<=maxn;i++) a[i]+=a[i-];
for(int i=;i<=maxn;i++)
if(a[i]>=K)
Tree.add(,,maxn,i,i);
while(Q--){
read(x); read(y);
printf("%d\n",Tree.query(,,maxn,x,y));
}
return ;
}
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 (差分思想)
题目链接 816B Karen and Coffee 题目分析 题意:有个人在学泡咖啡,因此看了很多关于泡咖啡温度的书,得到了n种推荐的泡咖啡温度范围[L1,R1] ,此人将有k种做法推荐的温度记为可 ...
- codeforces 816B.Karen and Coffee 解题报告
题目链接:http://codeforces.com/contest/816/problem/B 题目意思:给出 n 个recipes,第 i 个(1<= i <=n)recipes 表明 ...
- [Codeforces 280D]k-Maximum Subsequence Sum(线段树)
[Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...
- [Codeforces 266E]More Queries to Array...(线段树+二项式定理)
[Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...
- codeforces 1217E E. Sum Queries? (线段树
codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...
- 816B. Karen and Coffee 前缀和思维 或 线段树
LINK 题意:给出n个[l,r],q个询问a,b,问被包含于[a,b]且这样的区间数大于k个的方案数有多少 思路:预处理所有的区间,对于一个区间我们标记其(左边界)++,(右边界+1)--这样就能通 ...
- Codeforces 1276F - Asterisk Substrings(SAM+线段树合并+虚树)
Codeforces 题面传送门 & 洛谷题面传送门 SAM hot tea %%%%%%% 首先我们显然可以将所有能够得到的字符串分成六类:\(\varnothing,\text{*},s, ...
- Codeforces 1270H - Number of Components(线段树)
Codeforces 题目传送门 & 洛谷题目传送门 首先需发现一个性质,那就是每一个连通块所对应的是一个区间.换句话说 \(\forall l<r\),若 \(l,r\) 在同一连通块 ...
随机推荐
- Educational Codeforces Round 36 (Rated for Div. 2) G. Coprime Arrays
求a_i 在 [1,k]范围内,gcd(a_1,a_2...,a_n) = 1的a的数组个数. F(x)表示gcd(a_1,a_2,...,a_n) = i的a的个数 f(x)表示gcd(a_1,a_ ...
- JavaScript插件
Spket ide Spket ide是强大的工具包为了JavaScript和XML的开发,这个强大的编辑器对JavaScript, XUL/XBLand Yahoo! Widget的开发都有全面的支 ...
- Hadoop 连接mysql
1 mysql数据导入到hdfs数据 hadoop提供了org.apache.hadoop.io.Writable接口来实现简单的高效的可序列化的协议,该类基于DataInput和DataOutput ...
- keras常见问题解答
https://keras.io/zh/getting-started/faq/ https://keras.io/zh/ https://github.com/keras-team/keras/tr ...
- 安卓执行机制JNI、Dalvik、ART之间的比較 。android L 改动执行机制。
Android L默认採用ART执行环境.全然兼容64位移动处理器.Google称这将比此前的Dalvik模式性能提高两倍,可是会占用很多其它的内存空间.Android有三种执行模式:JNI.Dalv ...
- PS 如何把大嘴变小嘴
Photoshop整容教程:让MM美唇大嘴变小嘴 2009-06-17 14:15作者:佚名出处:天极网软件频道责任编辑:王健 下面就开始实际操作了. 1.首先从Photosh ...
- Linux C++的多线程编程(转)
1. 引言 线程(thread)技术早在60年代就被提出,但真正应用多线程到操作系统中去,是在80年代中期,solaris是这方面的佼佼者.传统的Unix也支持线程的概念,但是在一个进程(proces ...
- HDU2084_数塔【简单题】【数塔】
数塔 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...
- 最小公倍数(Least Common Multiple)
最小公倍数=两个数的乘积/两个数的最大公约数. 接上篇求最大公约数方法,最小公倍数的代码例如以下: public class LCM { //最小公倍数=两数乘积/最大公约数 public stati ...
- 删除moduleCache下文件解决预编译头文件相关的编译错误
之前有在代码全部正确的情况下,遇到过下面的编译错误: fatal error: file '.....h' has been modified since the precompiled header ...