yubaolai @ 2024-08-29 17:21:15
#include <bits/stdc++.h>
using namespace std;
int n,m,top=0,nx=0;
struct Info{
int x,y;
}a[2000000];
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
int t;
scanf("%d",&t);
if(t!=nx){
top++;
a[top].x=t,a[top].y=i;
nx=t;
}
}
for(int i=1;i<=m;i++){
int q;
scanf("%d",&q);
int L=1,R=top+1;
while(L+1!=R){
int M=(L+R)/2;
if(a[M].x<q)
L=M;
else
R=M;
}
if(a[R].x!=q)
printf("-1 ");
else
printf("%d ",a[R].y);
}
return 0;
}
by khfjg764 @ 2024-09-04 21:19:24
试试把右端点就初始化成最大长度试试,不要设成最大长度+1