ABC 248 D - Range Count Query(思维)

https://atcoder.jp/contests/abc248/tasks/abc248_d

题目大意:

给定一个长度为n的数组a,再给出q次询问;

每次询问都问我们区间a[l]~a[r]中k的出现次数是多少?
Sample Input 1 
5
3 1 4 1 5
4
1 5 1
2 4 3
1 5 2
1 3 3
Sample Output 1  
2
0
0
1

这道题目虽然我思路完全正确,但是在代码实现的时候有小细节导致我没有得出答案,就是vector遇到lower_bound和upper_bound的写法上不对劲
写成这样就行啦!

LL be=lower_bound(v[k].begin(),v[k].end(),l)-v[k].begin();

LL ed=upper_bound(v[k].begin(),v[k].end(),r)-v[k].begin();

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=200200,M=2002;
LL a[N];
vector<LL> v[N];
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        LL n;
        cin>>n;
        for(LL i=1;i<=n;i++)
        {
            cin>>a[i];
            v[a[i]].push_back(i);
        }
        LL q;
        cin>>q;
        while(q--)
        {
            LL l,r,k;
            cin>>l>>r>>k;
            LL be=lower_bound(v[k].begin(),v[k].end(),l)-v[k].begin();
            LL ed=upper_bound(v[k].begin(),v[k].end(),r)-v[k].begin();
            cout<<max((LL)0,ed-be)<<endl;
        }
    }
    return 0;
}
posted @ 2022-10-04 11:04  高尔赛凡尔娟  阅读(22)  评论(0编辑  收藏  举报