CodeForces 816B Karen and Coffee

Karen,一位咖啡爱好者,在寻找完美咖啡冲泡温度的秘密。通过研究多种食谱,她了解到不同配方推荐的最佳温度范围。本篇探讨如何高效计算在特定温度区间内,符合至少k个食谱推荐的适宜温度数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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.

题意

有n个制作咖啡的食谱,分别要求在一定的温度间。给你q个温度区间,问你在这个温度区间内有多少个温度满足至少k个制作咖啡的方法。

思路

这个题暴力是不可能的,每次询问就要n*(r-l+1)次,然后是q次询问。
预先处理n个区间,求出满足条件的区间(不好实现)。再对每次询问,进行讨论,端点相加减。

仔细分析,这题类似于区间更新。后来百度了一下题解,原来还有差分这个思想。

首先建立一个哈希表a[] (初始为0),下标代表温度,值代表该温度出现了几次。对于某个食谱,已知温度区间的左右端点分别为l,r。使a[l]++,a[r+1]–,再对a数组求其前缀和,所得的sum数组就为a[]数组应该更新的状态。为什么不直接更新a数组了? 因为该方法可以离线区间修改,即将所有修改操作全部读入,然后一起处理,跑一遍sum数组就可以得到最终需要修改n次的结果。将一个二重循环变成了一个一重循环。

接下来是q次询问。如果每次循环都要遍历一遍sum数组那就太麻烦了。 这时候就要用到前缀和。 因为是统计某个区间的个数,先将sum数组中大于k次的温度置为1,否则置为0。再对sum数组求一遍前缀和,得到sum2数组,sum2[右端点+1]-sum2[左端点]就为所求答案。

代码

#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cstdio>
#include <vector>
#include <utility>
#include <cstring>
#include <iostream>
#include <algorithm>
#define eps 1e-8
#define PI acos(-1)
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3f
#define N 200005
#define LIM 10000000
#define newmax(a,b) a>b?a:b
#define newmin(a,b) a>b?b:a
#define Lowbit(x) (x&-x)
using namespace std;
typedef long long int LL;
const int dir[4][2]= { {1,0},{0,1},{-1,0},{0,-1} };

LL c[N];

int main()
{
    LL p[N]={0},sum[N]={0};
    LL n,k,q,g=0,maxs=-INF,mins=INF;
    scanf("%lld%lld%lld",&n,&k,&q);
    for(LL i=0;i<n;i++)
    {
        LL a,b;
        scanf("%lld%lld",&a,&b);
        p[a]+=1;
        p[b+1]+=-1;
    }
    for(LL i=1;i<N;i++)
    {
        c[i]=c[i-1]+p[i];
    }
    for(LL i=0;i<N;i++)
    {
        if(c[i]>=k)
            c[i]=1;
        else
            c[i]=0;
    }
    for(LL i=1;i<N;i++)
    {
        sum[i]=sum[i-1]+c[i];
    }
    for(LL i=0;i<q;i++)
    {
        LL a,b;
        scanf("%lld%lld",&a,&b);
        printf("%lld\n",sum[b]-sum[a-1]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值