zoukankan      html  css  js  c++  java
  • FZU2072——二分——Count

    Given an array of positive integers and m queries.Each query contains i, j, x, output the number of occurrences of x into the subarray Ai,Ai+1...,Aj.

    Input

    There are several cases. The first line of each case contains tow integers n, q(1<=n,q<=100000), indicating the array length and the number of queries.The second line contains n positive integers ai(1 <= ai <= 100000).Next q lines contain three positive integers i,j,x(1<=i<=j<=n).

    Output

    For each query output one line, the number of occurrences of x.

    Sample Input

    3 2 1 2 1 1 2 1 1 3 1

    Sample Output

    1 2
    /*
    lower_bound(  ,  , x)
    是用二分从begin 到 end 找序号大于等于 x 的第一个值为y的迭代器下标
    大意:在l到r之间询问k出现的次数
    */
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    using namespace std;
    
    const int MAX = 1000100;
    vector<int> G[MAX];
    int a[MAX];
    
    int  cal(int x, int y)
    {   
        return lower_bound( G[y].begin(), G[y].end(), x) - G[y].begin();
    }
    int main()
    {
    int n, m;
    int l, r, k;
    while(~scanf("%d%d", &n, &m)){
        for(int i = 1; i <= n; i++)
            G[a[i]].clear();
        for(int i = 1; i <= n; i++){
            scanf("%d", &a[i]);
            G[a[i]].push_back(i);
        }
        for(int i = 1; i <= m; i++){
            scanf("%d%d%d", &l, &r, &k);
            printf("%d
    ", cal(r + 1 , k) - cal(l , k));
        }
    }
        return 0;
    }
    

      

  • 相关阅读:
    LNMP 部署
    zabbix3.2安装graphtree3.0.4
    升级java8---from centos
    mysql5.6-5.7性能调优
    samba server install
    centos7 zabbix3 install done
    实验四总结
    第五周学习小结
    个人的一些html、css笔记
    为什么wait,notify,notifyAll定义在Object中?
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4656680.html
Copyright © 2011-2022 走看看