Problem 2072 Count

来源:互联网 发布:淘宝联盟 百度百科 编辑:程序博客网 时间:2024/06/05 23:52
Problem 2072 Count

Accept: 167    Submit: 514
Time Limit: 1000 mSec    Memory Limit : 65536 KB

 Problem Description

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 21 2 11 2 11 3 1

 Sample Output

1
2
代码如下:
#include <cstdio>#include <algorithm>#include <vector>#define Max 100000+5using namespace std;int a[Max];vector< int > c[Max];void solve(int i, int j, int x){    if(c[x].size()==0){ printf("0\n"); return; }    int d = upper_bound(c[x].begin(),c[x].end(),j)-lower_bound(c[x].begin(),c[x].end(),i);// upper_bound返回地址,区间为[0,j]-[0,i)    printf("%d\n",d);    return;}int main(){    int n, q, sum;    int i, j, k, x;    while( ~scanf("%d%d",&n,&q) ){        for(i=0; i<n; i++) scanf("%d",&a[i]);        for(i=0; i<n; i++) c[a[i]].clear();        for(i=0; i<n; i++) c[a[i]].push_back(i);        for(k=0; k<q; k++){            scanf("%d%d%d",&i, &j, &x);            solve(i-1, j-1, x);        }    }}

0 0
原创粉丝点击