https://www.luogu.com.cn/problem/P2249
输入 n(n\le10^6)n(n≤106) 个不超过 10^9109 的单调不减的(就是后面的数字不小于前面的数字)非负整数 a_1,a_2,\dots,a_{n}a1,a2,…,an,然后进行 m(m\le10^5)m(m≤105) 次询问。对于每次询问,给出一个整数 q(q\le10^9)q(q≤109),要求输出这个数字在序列中的编号,如果没有找到的话输出 -1 。
输入格式
第一行 2 个整数 n 和 m,表示数字个数和询问次数。
第二行 n 个整数,表示这些待查询的数字。
第三行 m 个整数,表示询问这些数字的编号,从 1 开始编号。
输出格式
m 个整数表示答案。
输入输出样例
输入 #1复制
11 3 1 3 3 3 5 7 9 11 13 15 15 1 3 6
输出 #1复制
1 2 -1
这题意义在于让我发现了我之前总结的板子边界又有问题。重新更正了。https://blog.csdn.net/zstuyyyyccccbbbb/article/details/107106402
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e6+10;
typedef long long LL;
LL a[maxn];
LL bsearch(LL l,LL r,LL q)
{
while(l<r)
{
LL mid=(l+r)>>1;
if(a[mid]>=q) r=mid;
else l=mid+1;
}
return l;
}
int main(void)
{
LL n,m;cin>>n>>m;
for(LL i=1;i<=n;i++) cin>>a[i];
while(m--)
{
LL q;cin>>q;
LL t=bsearch(1,n+1,q);
// cout<<"t=="<<t<<endl;
// cout<<"a[t]=="<<a[t]<<endl;
if(t==n+1||a[t]!=q) cout<<"-1"<<' ';
else cout<<t<<' ';
}
return 0;
}