zoukankan      html  css  js  c++  java
  • CodeForces-816B:Karen and Coffee (简单线段树)

    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.

    Example

    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

    题意:N,K,Q;给出N个区间;Q次询问,对每次询问,问[X,Y]区间里有多少个数在N个区间出现次数大于等于K。

    思路:对N个区间,前缀和处理,大于等于K的插入线段树。

    。。:主要是图美,所以想做一下。

    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int maxn=200000;
    int a[maxn+10],sum[(maxn+10)<<2];
    void read(int &res){
        char c=getchar();
        for(;c>'9'||c<'0';c=getchar());
        for(res=0;c>='0'&&c<='9';c=getchar()) res=(res<<3)+(res<<1)+c-'0';
    }
    struct Segment_Tree
    {
        void add(int Now,int L,int R,int l,int r)
        {
            if(L==R){
                sum[Now]++;
                return ;
            }
            int Mid=(L+R)>>1;
            if(l<=Mid) add(Now<<1,L,Mid,l,r);
            if(r>Mid) add(Now<<1|1,Mid+1,R,l,r);
            sum[Now]=sum[Now<<1]+sum[Now<<1|1];
        }
        int query(int Now,int L,int R,int l,int r)
        {
            if(l<=L&&r>=R)
                return sum[Now];
            int Mid=(L+R)>>1,tmp=0;
            if(l<=Mid) tmp+=query(Now<<1,L,Mid,l,r);
            if(r>Mid) tmp+=query(Now<<1|1,Mid+1,R,l,r);
            return tmp;
        }
    }Tree;
    int main()
    {
        int N,M,K,Q,x,y;
        read(N); read(K); read(Q);
        for(int i=1;i<=N;i++){
            read(x); read(y);
            a[x]++; a[y+1]--;
        }
        for(int i=1;i<=maxn;i++) a[i]+=a[i-1];
        for(int i=1;i<=maxn;i++)
         if(a[i]>=K)
          Tree.add(1,1,maxn,i,i);
        while(Q--){
            read(x); read(y);
            printf("%d
    ",Tree.query(1,1,maxn,x,y));
        }
        return 0;
    }
  • 相关阅读:
    TCP/IP协议栈与数据包封装+TCP与UDP区别
    MySQL数据库优化总结
    MySQL存储引擎,锁,优化简述
    java实现常见查找算法
    Java的几种常见排序算法
    UML类图学习
    高性能的RTC服务器OpenFire
    常用的设计模式
    Swing JInternalFrame的使用
    Qt 模态与非模态
  • 原文地址:https://www.cnblogs.com/hua-dong/p/8551028.html
Copyright © 2011-2022 走看看