• Codeforces Round #419 (Div. 2) B. Karen and Coffee


    B. Karen and Coffee
    time limit per test
    2.5 seconds
    memory limit per test
    512 megabytes
    input
    standard input
    output
    standard output

    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.

    Examples
    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
    Note

    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 are3: 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.

    线段树写多了,做题都不用脑子,看到题第一眼就只会套线段树,我也是没救了!

    写到一半,看了下通过人数已经700+了,我就知道有更简单的写法,按理来说线段树不应该这么快,但这是在去想其他写法也已经来不及了,只好硬着头皮写完。

    赛后学习到了简单写法是前缀数组的写法。线段树的不能白写呀,我还是把线段树的代码贴出来吧!

    每个节点维护两个属性:该区间被提到了几次(c),该区间有多少个>=k的温度(ans)。

    首先进行n次对c的维护,注意只维护到最大子区间。(n*logmaxn)

    然后再进行一次对所有区间的c的维护,从上到下,子区间.c+=父区间.c。这样我们就知道了每个叶子节点的c值。然后判断是否>=k,如果大于,那么叶子节点的ans值便为1,然后

    再向上合并,我们便知道了所有区间的ans值。一次建树的复杂度。

     查询就简单了,查询到最大子区间的ans值,相加即可。(q*logmaxn)

    C题被hack了,赛后虽然过了,但是因为解法不是最简单的,不想写了...

    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<iostream>
    #include<queue>
    #include<map>
    #include<cmath>
    #include<set>
    #include<stack>
    #define ll long long
    #define max(x,y) ((x)>(y)?(x):(y))
    #define min(x,y) ((x)>(y)?(y):(x))
    #define cls(name,x) memset(name,x,sizeof(name))
    using namespace std;
    const int inf=1e9+10;
    const int maxn=200010;
    const int maxm=1e3+10;
    const int mod=1e9+7;
    const double pi=acos(-1.0);
    int n,k,q;
    struct node
    {
        int c,ans;
        node(){c=0;ans=0;}
    }tree[maxn*4];
    void update(int left,int right,int l,int r,int root)
    {
        if(left==l&&right==r)
        {
            tree[root].c++;
            return ;
        }
        int mid=(l+r)/2;
        if(right<=mid) update(left,right,l,mid,root*2);
        else if(left>=mid+1) update(left,right,mid+1,r,root*2+1);
        else
        {
            update(left,mid,l,mid,root*2);
            update(mid+1,right,mid+1,r,root*2+1);
        }
    }
    void refresh(int l,int r,int root)
    {
        if(l==r)
        {
            if(tree[root].c>=k)
                tree[root].ans=1;
            return ;
        }
        int mid=(l+r)/2;
        tree[root*2].c+=tree[root].c;
        refresh(l,mid,root*2);
        tree[root*2+1].c+=tree[root].c;
        refresh(mid+1,r,root*2+1);
        tree[root].ans=tree[root*2].ans+tree[root].ans+tree[root*2+1].ans;
    }
    int query(int left,int right,int l,int r,int root)
    {
        if(left==l&&right==r)
        {
            return tree[root].ans;
        }
        int ans=0;
        int mid=(l+r)/2;
        if(right<=mid) ans=query(left,right,l,mid,root*2);
        else if(left>=mid+1) ans=query(left,right,mid+1,r,root*2+1);
        else
        {
            ans=query(left,mid,l,mid,root*2)+query(mid+1,right,mid+1,r,root*2+1);
        }
        return ans;
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        while(~scanf("%d %d %d",&n,&k,&q))
        {
            for(int i=0;i<n;i++)
            {
                int a,b;
                scanf("%d %d",&a,&b);
                update(a,b,1,200000,1);
            }
            refresh(1,200000,1);
            while(q--)
            {
                int a,b;
                scanf("%d %d",&a,&b);
                printf("%d
    ",query(a,b,1,200000,1));
            }
        }
        return 0;
    }
  • 相关阅读:
    Wide & Deep Learning for Recommender Systems
    两个经典问题
    Vlog简介
    中文dumps显示
    用Python提取中文关键词
    【STL】算法 — partial_sort
    c字符串分割 strtok()
    JSP的声明(statement)
    layui之ajax巨坑
    jQuery 库中的 $() 是什么?(答案如下)
  • 原文地址:https://www.cnblogs.com/mgz-/p/7043541.html
Copyright © 2020-2023  润新知