题目描述
给出N个已经从小到大排好的有序整数序列(没有相同的数字),然后有M次询问,每次询问一个数字X, 表示需要找出等于整数x的数在整数序列中的位置。
输入描述
第一行两个整数N和M(1<=M<=N<=100000),分别表示有序整数序列数字的个数和询问的次数。 接下来一行一共有N个整数,表示整数序列上的数字,保证数字是有序的。 最后M行,每行一个整数表示要询问的数字x。 数字大小范围都在[0,200000]。
输出描述
对于每个询问都输出一个对应的结果,数据保证每个询问都有解。
样例输入
4 2 1 3 7 9 7 3
样例输出:
3 2
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b,c[100002],l=1,r,m,d;
cin>>a>>b;
r=a;
for(int i=1;i<=a;i++){
cin>>c[i];
}
while(b>0){
cin>>d;
l=1;
r=a;
while(1){
m=(l+r)/2;
if(c[m]==d){
cout<<m<<endl;
break;
}
if(c[m]<d){
l=m+1;
}
if(c[m]>d){
r=m-1;
}
}
b--;
}
return 0;
}