For a given sequence A={a0,a1,...,an−1}A = \{a_0, a_1, ..., a_{n-1}\}A={a0,a1,...,an−1} which is sorted by ascending order, find a specific value kkk given as a query.
Input
The input is given in the following format.
nnn
a0  a1  ,...,  an−1a_0 \; a_1 \; ,..., \; a_{n-1}a0a1,...,an−1
qqq
k1k_1k1
k2k_2k2
:
kqk_qkq
The number of elements nnn and each element aia_iai are given in the first line and the second line respectively. In the third line, the number of queries qqq is given and the following qqq lines, qqq integers kik_iki are given as queries.
Output
For each query, print 1 if any element in AAA is equivalent to kkk, and 0 otherwise.
Constraints
1≤n≤100,0001 \leq n \leq 100,0001≤n≤100,000
1≤q≤200,0001 \leq q \leq 200,0001≤q≤200,000
0≤a0≤a1≤...≤an−1≤1,000,000,0000 \leq a_0 \leq a_1 \leq ... \leq a_{n-1} \leq 1,000,000,0000≤a0≤a1≤...≤an−1≤1,000,000,000
0≤ki≤1,000,000,0000 \leq k_i \leq 1,000,000,0000≤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;
}