二分找數

BiankaShenhen發表於2024-11-14

//https://www.luogu.com.cn/problem/P2249

include

using namespace std;

int n,m,q,a[1000005];

int find(int x)
{
int l=1,r=n;
while (l<r)
{
int mid=l+(r-l)/2;
if (a[mid]>=x) r=mid;
else l=mid+1;
}

if (a[l]==x) return l; 
else return -1;

}

int main()
{
scanf("%d %d",&n,&m);
for (int i=1 ; i<=n ; i++)
scanf("%d",&a[i]);

for (int i=1 ; i<=m ; i++)
{
	scanf("%d",&q);
	int ans=find(q); 
	printf("%d ",ans); 
}

return 0;

}

相關文章