Description
给出一个长度为 N的不下降序列, m次询问数字x在序列中的哪个位置
如果有多个解输出位置最靠左边的,无解输出-1
Format
Input
第一行给出数字N,M
第二行N个数字
第三行M个数字,其值<=1e9
N<=2e6
M<=1e5
Output
一行输出M个数字,代表结果
每个数字后面有个空格
Samples
输入数据 1
11 3
1 3 3 3 5 7 9 11 13 15 15
1 3 6
Copy
输出数据 1
1 2 -1
#include<bits/stdc++.h>
using namespace std;
int n,m,a[3333333];
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=m;i++){
int x,an=0;
cin>>x;
for(int j=20;j>=0;j--){
if(a[an+(1<<j)]<x&&an+(1<<j)<=n){
an+=(1<<j);
}
}
if(a[an+1]!=x){
cout<<"-1 ";
}
else{
cout<<an+1<<" ";
}
}
return 0;
}