Description
输入 �n 个不超过 109109 的单调不减的(就是后面的数字不小于前面的数字)非负整数 �1,�2,…,��a1,a2,…,an,然后进行 �m 次询问。对于每次询问,给出一个整数 �q,要求输出这个数字在序列中第一次出现的编号,如果没有找到的话输出 −1−1 。
Input
第一行 22 个整数 �n 和 �m,表示数字个数和询问次数。
第二行 �n 个整数,表示这些待查询的数字。
第三行 �m 个整数,表示询问这些数字的编号,从 11 开始编号。
Output
输出一行,�m 个整数,以空格隔开,表示答案。
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≤�≤1061≤n≤106,0≤��,�≤1090≤ai,q≤109,1≤�≤1051≤m≤105
本题输入输出量较大,请使用较快的 IO 方式。
代码:
#include<iostream>
#include<algorithm>
#define endl '\n'
#define ll long long
//#define int ll
using namespace std;
const int N=1e6+7;
int a[N],b;
int n;
int solve(int q)
{
int l=1,r=n;
while(l<r)
{
int mid=(l+r)/2;
if(a[mid]>=q) r=mid;
else l=mid+1;
}
if(a[r]==q) return l;
else return -1;
}
signed main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int m;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
for(int i=1;i<=m;i++)
{
cin>>b;
cout<<solve(b)<<' ';
}
return 0;
}