For a given sequence A={a0,a1,...,an−1} which is sorted by ascending order, find a specific value k given as a query.
Input
The input is given in the following format.
n
a0a1,...,an−1
q
k1
k2
:
kq
The number of elements n and each element ai are given in the first line and the second line respectively. In the third line, the number of queries q is given and the following q lines, q integers ki are given as queries.
Output
For each query, print 1 if any element in A is equivalent to k, and 0 otherwise.
Constraints
1≤n≤100,000
1≤q≤200,000
0≤a0≤a1≤...≤an−1≤1,000,000,000
0≤ki≤1,000,000,000
Sample Input 1
4
1 2 2 4
3
2
3
5
Sample Output 1
1
0
0
题意:
从长度为n的非递减序列里查找q次,每次查1个数,如果找到输出1,否则输出0
#include<bits/stdc++.h>
using namespace std;
int a[100005];
bool f(int a[],int n,int x){
int l=0,r=n-1,m;
while(r>l){
m=(l+r)>>1;
if(a[m]==x) return true;
else if(a[m]>x) r=m-1;
else l=m+1;
}
if(a[l]==x) return true;
else return false;
}
int main(){
int n,m,x;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
scanf("%d",&m);
while(m--){
scanf("%d",&x);
if(f(a,n,x)) printf("1\n");
else printf("0\n");
}
return 0;
}