差分 Karen and Coffee

发布时间:2024-01-21 14:19:17

题目来源CodeForces

评测方式RemoteJudge

难度普及/提高?

时间限制2.50s

内存限制500.00MB

08bf0ed1faf04ba385679c0c0427a137.webp

?

题目描述

To stay woke and attentive during classes, Karen needs some coffee!

84ca24fa194ecce1cb9c3e825b543dfb7809c444.pngKaren, 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?nn?coffee recipes. The?ii?-th recipe suggests that coffee should be brewed between?l_{i}li??and?r_{i}ri??degrees, inclusive, to achieve the optimal taste.

Karen thinks that a temperature is admissible if at least?kk?recipes recommend it.

Karen has a rather fickle mind, and so she asks?qq?questions. In each question, given that she only wants to prepare coffee with a temperature between?aa?and?bb?, inclusive, can you tell her how many admissible integer temperatures fall within the range?

输入格式

The first line of input contains three integers,?nn?,?kk?(?1<=k<=n<=2000001<=k<=n<=200000?), and?qq?(?1<=q<=2000001<=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?nn?lines describe the recipes. Specifically, the?ii?-th line among these contains two integers?l_{i}li??and?r_{i}ri??(?1<=l_{i}<=r_{i}<=2000001<=li?<=ri?<=200000?), describing that the?ii?-th recipe suggests that the coffee be brewed between?l_{i}li??and?r_{i}ri??degrees, inclusive.

The next?qq?lines describe the questions. Each of these lines contains?aa?and?bb?, (?1<=a<=b<=2000001<=a<=b<=200000?), describing that she wants to know the number of admissible integer temperatures

between?aa?and?bb?degrees, inclusive.

输出格式

For each question, output a single integer on a line by itself, the number of admissible integer temperatures between?aa?and?bb?degrees, inclusive.

?

题意翻译

Karen 喜欢咖啡。
她有?n?本食谱,第?i?本食谱包含两个数?li?,ri?,表示这本食谱推荐用?[li?,ri?]?之间的温度(包含?li?.ri?)来煮咖啡。
Karen 认为一个温度?a?是可接受的当且仅当有?≥k?本食谱推荐用?a?来煮咖啡。
Karen 有?q?个问题,每个问题用一对正整数?ai?,bi??来表示,表示她问?[ai?,bi?]?之间有多少个温度是可接受的

说明/提示

In the first test case, Karen knows?33?recipes.

  1. The first one recommends brewing the coffee between?9191?and?9494?degrees, inclusive.
  2. The second one recommends brewing the coffee between?9292?and?9797?degrees, inclusive.
  3. The third one recommends brewing the coffee between?9797?and?9999?degrees, inclusive.

A temperature is admissible if at least?22?recipes recommend it.

She asks?44?questions.

In her first question, she wants to know the number of admissible integer temperatures between?9292?and?9494?degrees, inclusive. There are?33?:?9292?,?9393?and?9494?degrees are all admissible.

In her second question, she wants to know the number of admissible integer temperatures between?9393?and?9797?degrees, inclusive. There are?33?:?9393?,?9494?and?9797?degrees are all admissible.

In her third question, she wants to know the number of admissible integer temperatures between?9595?and?9696?degrees, inclusive. There are none.

In her final question, she wants to know the number of admissible integer temperatures between?9090?and?100100?degrees, inclusive. There are?44?:?9292?,?9393?,?9494?and?9797?degrees are all admissible.

In the second test case, Karen knows?22?recipes.

  1. The first one, "wikiHow to make Cold Brew Coffee", recommends brewing the coffee at exactly?11?degree.
  2. The second one, "What good is coffee that isn't brewed at at least?36.330636.3306?times the temperature of the surface of the sun?", recommends brewing the coffee at exactly?200000200000?degrees.

A temperature is admissible if at least?11?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.

题解:

看一遍题目,就知道要用差分来做。

上AC代码:

5ce6b514fe2041cf8a64e943b50515b5.jpg

?

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+5;
ll ans[N],n,m,k,sum,a,b,l,r;
int main(){
	cin>>n>>m>>k;
	for(int i=1;i<=n;i++){
		cin>>a>>b;
		ans[a]+=1;
		ans[b+1]-=1;
		if(b>sum) sum=b;
	}
	for(int i=1;i<=sum;i++){
		ans[i]=ans[i-1]+ans[i];
	}
	for(int i=1;i<N;i++){
		if(ans[i]>=m) ans[i]=ans[i-1]+1;
		else ans[i]=ans[i-1];
	}
	for(int i=1;i<=k;i++){
		cin>>l>>r;
		cout<<ans[r]-ans[l-1]<<endl;
	}
	return 0;
}
文章来源:https://blog.csdn.net/2301_78892795/article/details/135723383
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。