P10288 [GESP样题 八级] 区间

原题链接

题解

本题的优化真的很重要!!
把所有元素出现的下标用map套vector存起来,然后二分查找

code

#include<bits/stdc++.h>
using namespace std;
map<int,vector<int> > mp;
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);//缺一不可
    int t;
    cin>>t;
    while(t--)
    {
        mp.clear();
        int n,x;
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>x;
            mp[x].emplace_back(i);
        }

        int q;
        cin>>q;
        while(q--)
        {
            int l,r;
            cin>>l>>r>>x;
            int len=upper_bound(mp[x].begin(),mp[x].end(),r)-lower_bound(mp[x].begin(),mp[x].end(),l);
            cout<<len<<"\n";//必须要\n而不能endl
        }
    }
    return 0;
}
posted @ 2024-04-16 12:49  纯粹的  阅读(106)  评论(0)    收藏  举报