Description
输入 nn 个不超过 109109 的单调不减的(就是后面的数字不小于前面的数字)非负整数 a1,a2,…,ana1,a2,…,an,然后进行 mm 次询问。对于每次询问,给出一个整数 qq,要求输出这个数字在序列中第一次出现的编号,如果没有找到的话输出 −1−1 。
Input
第一行 22 个整数 nn 和 mm,表示数字个数和询问次数。
第二行 nn 个整数,表示这些待查询的数字。
第三行 mm 个整数,表示询问这些数字的编号,从 11 开始编号。
Output
输出一行,mm 个整数,以空格隔开,表示答案。
Sample 1
Inputcopy | Outputcopy |
---|---|
11 3 1 3 3 3 5 7 9 11 13 15 15 1 3 6 | 1 2 -1 |
Hint
数据保证,1≤n≤1061≤n≤106,0≤ai,q≤1090≤ai,q≤109,1≤m≤1051≤m≤105
本题输入输出量较大,请使用较快的 IO 方式。
#include <bits/stdc++.h>
using namespace std;
int main (void) {
int n,m; cin>>n>>m;
int num[n+5]={0};
for(int i=0;i<n;i++){
cin>>num[i];
}
while(m--){
int f; cin>>f;
int index=lower_bound(num,num+n,f)-num;
if(f==num[index]){
cout<<index+1<<" ";
}else{
cout<<"-1"<<" ";
}
}
return 0;
}