B. 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?
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 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
In the first test case, Karen knows 3 recipes.
- The first one recommends brewing the coffee between 91 and 94 degrees, inclusive.
- The second one recommends brewing the coffee between 92 and 97 degrees, inclusive.
- The third one recommends brewing the coffee between 97 and 99 degrees, inclusive.
A temperature is admissible if at least 2 recipes recommend it.
She asks 4 questions.
In her first question, she wants to know the number of admissible integer temperatures between 92 and 94 degrees, inclusive. There are 3: 92, 93 and 94 degrees are all admissible.
In her second question, she wants to know the number of admissible integer temperatures between 93 and 97 degrees, inclusive. There are 3: 93, 94 and 97 degrees are all admissible.
In her third question, she wants to know the number of admissible integer temperatures between 95 and 96 degrees, inclusive. There are none.
In her final question, she wants to know the number of admissible integer temperatures between 90 and 100 degrees, inclusive. There are 4: 92, 93, 94 and 97 degrees are all admissible.
In the second test case, Karen knows 2 recipes.
- The first one, "wikiHow to make Cold Brew Coffee", recommends brewing the coffee at exactly 1 degree.
- The second one, "What good is coffee that isn't brewed at at least 36.3306 times the temperature of the surface of the sun?", recommends brewing the coffee at exactly 200000 degrees.
A temperature is admissible if at least 1 recipe recommends it.
In her first and only question, she wants to know the number of admissible integer temperatures that are actually reasonable. There are none.
题解:
首先预处理出c[i]表示第i位的次数。
然后维护一个线段树,c[i]>=m时就加到线段树里面去。
线段树可以保证这道题目不超时。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
int a[],mmax,n,m,l;
int sgm[],root,lazy[];
int ll(int x){return x<<;}
int rr(int x){return x<<|;}
void update(int root,int left,int right,int l,int r,int v)
{
if(l<=left&&right<=r)
{
lazy[root]+=v;
return;
}
sgm[root]=sgm[root]+v*(min(r,right)-max(left,l)+);
int m=(left+right)>>;
if(l<=m)update(ll(root),left,m,l,r,v);
if(r>m)update(rr(root),m+,right,l,r,v);
return;
}
int query(int root,int left,int right,int l,int r)
{
if(l<=left&&right<=r)return sgm[root]+lazy[root]*(right-left+);
if(right<l||left>r)return ;
int m=(left+right)>>;
return query(ll(root),left,m,l,r)+query(rr(root),m+,right,l,r)+lazy[root]*(min(r,right)-max(left,l)+);
}
int main()
{
int i,j;
scanf("%d%d%d",&n,&m,&l);
for(i=;i<=n;i++)
{
int c,d;
scanf("%d%d",&c,&d);
mmax=max(mmax,d);
a[c]++;a[d+]--;
}
for(i=;i<=mmax;i++)
{
a[i]=a[i]+a[i-];
if(a[i]>=m)update(,,mmax,i,i,);
}
for(i=;i<=l;i++)
{
int c,d;
scanf("%d%d",&c,&d);
printf("%d\n",query(,,mmax,c,d));
}
return ;
}
B. Karen and Coffee的更多相关文章
- CodeForces 816B Karen and Coffee(前缀和,大量查询)
CodeForces 816B Karen and Coffee(前缀和,大量查询) Description Karen, a coffee aficionado, wants to know the ...
- 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 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 ...
- Codeforces816B Karen and Coffee 2017-06-27 15:18 39人阅读 评论(0) 收藏
B. Karen and Coffee time limit per test 2.5 seconds memory limit per test 512 megabytes input standa ...
- 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 ...
- Karen and Coffee CF 816B(前缀和)
Description To stay woke and attentive(专注的) during classes, Karen needs some coffee! Karen, a coffee ...
- CF 816B Karen and Coffee【前缀和/差分】
To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...
- CodeForces-816B:Karen and Coffee (简单线段树)
To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...
随机推荐
- Transform java future into completable future 【将 future 转成 completable future】
Future is introduced in JDK 1.5 by Doug Lea to represent "the result of an asynchronous computa ...
- 最大流算法之EK(最短路径增广算法)
这是网络流最基础的部分--求出源点到汇点的最大流(Max-Flow). 最大流的算法有比较多,本次介绍的是其中复杂度较高,但是比较好写的EK算法.(不涉及分层,纯粹靠BFS找汇点及回溯找最小流量得到最 ...
- Android NDK开发之C调用Java及原生代码断点调试(二)
上一篇中,我们主要学习了Java调用本地方法,并列举了两大特殊实例来例证我们的论据,还没学习的伙伴必须先去阅读下,本次的学习是直接在上一篇的基础上进行了.点击:Android NDK开发之从Java与 ...
- hdu2089 不要62 我的第一个数位DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 数位DP的入门题,我是根据kuangbin的博客写出来的 思路: dp[i][0],表示长度为i ...
- iis7 安装laravel5.4环境
laravel版本: Laravel5.4IIS版本:IIS7站点配置就不详细说啦,大家网上可以搜一坨很多的配置方法啦哈直接上图: 由于IIS没有像Apache.htaccess文件,创建一个Web. ...
- [刷题]算法竞赛入门经典(第2版) 4-6/UVa508 - Morse Mismatches
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,10 ms) //UVa508 - Morse Mismatches #include< ...
- Java IO详解(一)------File 类
File 类:文件和目录路径名的抽象表示. 注意:File 类只能操作文件的属性,文件的内容是不能操作的. 1.File 类的字段 我们知道,各个平台之间的路径分隔符是不一样的. ①.对于UNIX平台 ...
- Jdk1.6 JUC源码解析(6)-locks-AbstractQueuedSynchronizer
功能简介: AbstractQueuedSynchronizer(以下简称AQS)是Java并发包提供的一个同步基础机制,是并发包中实现Lock和其他同步机制(如:Semaphore.CountDow ...
- linux三剑客之sed命令
一.前言 我们都知道,在Linux中一切皆文件,比如配置文件,日志文件,启动文件等等.如果我们相对这些文件进行一些编辑查询等操作时,我们可能会想到一些vi,vim,cat,more等命令.但是这些命令 ...
- dubbo高级配置学习
启动时检查 可以通过check="false"关闭检查,比如,测试时,有些服务不关心,或者出现了循环依赖,必须有一方先启动. 关闭某个服务的启动时检查:(没有提供者时报错) < ...