zoukankan      html  css  js  c++  java
  • CodeForces 816B 前缀和

    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个区间,然后q次查询,每次查询 问在查询区间中有多个数 在开始给给定的n个区间中出现次数大于等于k(题意大致是这样)。

      前缀和。

    对于开始给定的区间,利用括号匹配的思想,区间的左端点看作是左括号,右端点看作是右括号。然后从前向后遍历一遍,左括号每遇到一个右括号,就同时消去这一对括号,判断该数带有几个左括号

    即为在几个区间出现过。前缀和完毕。

      

      

     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<cstring>
     4 #include<string>
     5 #include<cmath>
     6 #include<algorithm>
     7 #include<queue>
     8 #include<stack>
     9 #include<deque>
    10 #include<map>
    11 #include<iostream>
    12 using namespace std;
    13 typedef long long  LL;
    14 const double pi=acos(-1.0);
    15 const double e=exp(1);
    16 const int N = 100009;
    17 
    18 struct con{
    19     int x;
    20     int left,right;
    21 }con[2*N];
    22 
    23 int num[2*N];
    24 
    25 int main()
    26 {
    27     int n,k,q;
    28     int i,p,j;
    29     int a,b;
    30     scanf("%d%d%d",&n,&k,&q);
    31     for(i=1;i<=n;i++)
    32     {
    33         scanf("%d%d",&a,&b);
    34         con[a].left++;
    35         con[b+1].right++;
    36     }
    37     a=b=0;
    38     for(i=1;i<=2*N;i++)
    39     {
    40         a+=con[i].left;
    41         a-=con[i].right;
    42         if(a>=k)
    43             num[i]=1;
    44         num[i]+=num[i-1];
    45     }
    46 //    for(i=1;i<=100;i++)
    47 //    {
    48 //        printf("%d ",num[i]);
    49 //        if(i%10==0)
    50 //            putchar('
    ');
    51 //    }
    52     for(i=1;i<=q;i++)
    53     {
    54         scanf("%d%d",&a,&b);
    55         printf("%d
    ",num[b]-num[a-1]);
    56     }
    57 
    58     return 0;
    59 }
    View Code
  • 相关阅读:
    PHP7 开启Zend Opcache
    swoole笔记之 主服务器swoole_websocket_server, 监听 tcp端口 ,任务投递, http请求
    Navicat 远程连接docker容器中的mysql 报错1251
    nginx配置后访问不了问题
    解决每次git pull需要输入用户名密码的问题
    论文阅记 EfficientDet: Scalable and Efficient Object Detection
    Tensorflow bug(一) ValueError The passed save_path is not a valid checkpoint
    论文阅记 MobileNetV3:Searching for MobileNetV3
    论文阅记 MnasNet: Platform-Aware Neural Architecture Search for Mobile
    论文阅记 MobileNetV2:Inverted Residuals and Linear Bottlenecks
  • 原文地址:https://www.cnblogs.com/daybreaking/p/9746682.html
Copyright © 2011-2022 走看看