220514 T1 查询 (二分查找+vector)

用vector记录每个数出现的位置,对于要查询的X,要找他落在L~R的个数有几个,用lower_bound和upper_bound查找,相减就是答案。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int read(){
 4     int x=0,f=1;
 5     char c=getchar();
 6     while(c<'0'||c>'9'){if(c=='-') f=-1;c=getchar();}
 7     while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
 8     return x*f;
 9 }
10 const int N=2e5+10;
11 int n,q,a;
12 vector<int> m[N];//存一个数他所在的位置 
13 int main(){
14     //freopen("query.in","r",stdin);
15     //freopen("query.out","w",stdout);
16     n=read();
17     for(int i=1;i<=n;i++){
18         a=read();
19         m[a].push_back(i); 
20     } 
21     q=read();
22     for(int i=1;i<=q;i++){
23         int L,R,X;
24         L=read(),R=read(),X=read();
25         int p1=lower_bound(m[X].begin(),m[X].end(),L)-m[X].begin();
26         int p2=upper_bound(m[X].begin(),m[X].end(),R)-m[X].begin();
27         printf("%d\n",p2-p1);//个数 
28     }
29 }

 

posted @ 2022-05-14 15:00  YHXo  阅读(53)  评论(0)    收藏  举报