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;
    }
    

      

  • 相关阅读:
    大文件上传
    zabbix接口
    Vue 在不同的环境使用不同的接口地址
    Vue发布流程
    RabbitMQ集群一些使用细节
    Watcher 系统整体流程图
    监控系统各个模块部署
    deepin安装node和npm最新
    google安装json插件
    数据库访问性能优化 Oracle
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4656680.html
Copyright © 2011-2022 走看看