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\n", cal(r + 1 , k) - cal(l , k));
    }
}
    return 0;
}

  

转载于:https://www.cnblogs.com/zero-begin/p/4656680.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值