zoukankan      html  css  js  c++  java
  • Karen and Coffee CF 816B(前缀和)

    Description

    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, nk (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.

    Sample Input

    Input
    3 2 4
    91 94
    92 97
    97 99
    92 94
    93 97
    95 96
    90 100
    Output
    3
    3
    0
    4
    Input
    2 1 1
    1 1
    200000 200000
    90 100
    Output
    0

    Hint

    In the first test case, Karen knows 3 recipes.

    1. The first one recommends brewing the coffee between 91 and 94 degrees, inclusive.
    2. The second one recommends brewing the coffee between 92 and 97 degrees, inclusive.
    3. 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.

    1. The first one, "wikiHow to make Cold Brew Coffee", recommends brewing the coffee at exactly 1 degree.
    2. 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.

    题目意思:女主为了得到最好的泡咖啡的温度,看了n本书,n本书推荐了n种的适合范围,她想要查询q次,a~b温度范围内有多少个温度是可以允许泡咖啡的时间(这个时间要求至少有k本数推荐过)。

    解题思路:当时没有做到这一道题。。。。总的来说这一道题使用前缀和来做,这道题是不是有点像给气球刷颜色的那一类题。

    N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗?
     
    当时数据在做这一类题的时候,限于数据量的因素,使用了树状数字或者线段树这样的数据结构,这里就可以不去使用了。
     
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #define maxs 200010
     5 using namespace std;
     6 int n,k,q;
     7 int a[maxs];
     8 int sum[maxs];
     9 int main()
    10 {
    11     int i,r,l;
    12     scanf("%d%d%d",&n,&k,&q);
    13     for(i=0;i<n;i++)
    14     {
    15       scanf("%d%d",&l,&r);
    16       a[l]++;
    17       a[r+1]--;
    18     }
    19     for(i=1;i<maxs;i++)
    20     {
    21         a[i]+=a[i-1];
    22         if(a[i]>=k)///满足至少有k本书推荐
    23         {
    24             sum[i]++;
    25         }
    26     }
    27     for(i=1;i<maxs;i++)
    28     {
    29         sum[i]+=sum[i-1];
    30     }
    31     for(i=0;i<q;i++)
    32     {
    33         scanf("%d%d",&l,&r);
    34         printf("%d
    ",sum[r]-sum[l-1]);
    35     }
    36     return 0;
    37 }
  • 相关阅读:
    navicat 连接Oracle 报错 ORA-12514 TNS:listener does not currently know of service requested in connect descriptor
    centos 安装配置 redis
    ubuntu安装keras
    VB类似的InputBox为MFC
    WPF颜色选择器
    窗口样式
    IP地址、端口号、子网掩码提交表单库
    添加自定义对话框,您的应用程序
    在c#中使用异步等待构建响应式UI
    一个通用对话框
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/9762574.html
Copyright © 2011-2022 走看看