CodeForces 816B Karen and Coffee

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

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;
}
### Codeforces Coffee Break 问题解析 #### 题目描述 在一个场景中,某人计划在一段时间内喝 `n` 杯咖啡,每杯咖啡有一个特定的饮用时间点(以分钟为单位)。为了健康考虑,在同一天内任意两杯咖啡之间的最小间隔应不少于 `d` 分钟。目标是计算完成所有咖啡饮用所需的最少天数,并指定每一天具体饮用了哪些咖啡。 #### 解决方案概述 通过分析不同时间段内的可用间隙来安排每日的最大可能饮品数量。此过程可以通过多种方式实现,其中一种高效的方法涉及使用二分查找配合数据结构如集合(set)来进行优化处理[^1]。 对于每一个给定的时间戳 t_i ,算法试图找到最早能够容纳该次休息而不违反 d 分钟约束条件的日子 r_j 。一旦找到了合适的位置,则更新对应的日期并继续下一个请求直到全部分配完毕。 ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, m, d; cin >> n >> m >> d; // 输入参数 vector<int> times(n); for(auto& time : times){ cin>>time; } set<pair<int,int>> days; for(int i=0;i<n;++i){ auto it = days.lower_bound({times[i]-d+1,-1}); if(it!=days.begin()){ --it; cout<<(++(*it).second)<<' '; days.erase(it); }else{ cout<<1<<' '; } days.insert({times[i],*days.rbegin().second+1}); } } ``` 上述代码实现了基于输入时间和间隔限制的有效调度策略。它利用了 STL 中的 `set` 容器及其成员函数 `lower_bound()` 方法来快速定位满足条件的最佳位置[^2]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值