Myh_XXI @ 2023-07-10 18:45:42
#include<iostream>
using namespace std;
const int MAXN = 1e6 + 10;
int read()
{
int x = 0, p = 1;
char c = getchar();
while (c < '0' || c>'9')
{
if (c == '-')p = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
{
x = x * 10 + c - '0';
c = getchar();
}
return x * p;
}
int towfind(int* a, int i, int o)
{
int s = 1;
while (s <= o)
{
int k = (s + o) / 2;
if (a[k] == i)
{
if (a[k - 1] == i)
{
o = k - 1;
continue;
}
return k;
}
else if (a[k] > i)
{
o = k - 1;
}
else if (a[k] < i)
{
s = k + 1;
}
}
return -1;
}
int main()
{
int n = read(), m = read(), a[MAXN], c;
for (int i = 1; i <= n; i++)
{
a[i] = read();
}
while (m--)
{
c = read();
cout << towfind(a, c, n) << " ";
}
return 0;
}
by MC00101 @ 2023-07-12 15:02:01
#include <bits/stdc++.h>
using namespace std;
long long n,m,ks;
long long a[10010000];
long long rf(long long ks){
long long l=1,r=n,k;
while(l<=r){
k=l+(r-l)/2;
if(a[k]==ks){
r=k-1;
}
else if(a[k]>ks){
r=k-1;
}
else{
l=k+1;
}
}
if(a[l]==ks){
return l;
}
else{
return -1;
}
}
int main(){
cin>>n>>m;
for(long long i=1;i<=n;i++)
cin>>a[i];
for(long long i=1;i<=m;i++){
cin>>ks;
cout<<rf(ks)<<" ";
}
return 0;
}