1613. For Fans of Statistics(STL)

1613

高端的东西

lower_bounder

函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置,且last的位置是越界的

然后用map 把数映射成容器 可以简单查询到每个数出现的最前和最后位置 再与给出的L,R相比较

 1 #include <iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<stdlib.h>
 6 #include<vector>
 7 #include<map>
 8 using namespace std;
 9 #define N 70010
10 map<int,vector<int> >t;
11 vector<int>::iterator it;
12 int main()
13 {
14     int i,k,n,a;
15     scanf("%d",&n);
16     for(i = 1; i <= n ; i++)
17     {
18         scanf("%d",&a);
19         t[a].push_back(i);
20     }
21     scanf("%d",&k);
22     for(i = 1; i <= k ;i++)
23     {
24         int l,r,x;
25         scanf("%d%d%d",&l,&r,&x);
26         it = lower_bound(t[x].begin(),t[x].end(),l);
27         if(it==t[x].end())
28         printf("0");
29         else if((*it)<=r)
30         printf("1");
31         else
32         printf("0");
33     }
34     puts("");
35     return 0;
36 }
View Code
原文地址:https://www.cnblogs.com/shangyu/p/3337031.html