AcWing 789. 数的范围(java版)
原题链接
简单
作者:
Elegant
,
2021-03-09 19:38:32
,
所有人可见
,
阅读 369
import java.io.*;
import java.util.*;
public class Main {
static Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
static int[] a=new int[100010];
static int t;
static int n;
static int m;
public static void main(String[] args)throws IOException
{
n=in.nextInt();
t=in.nextInt();
for(int i=1;i<=n;i++)
a[i]=in.nextInt();
while(t-->0)
{
m=in.nextInt();
int l=1,r=n;
while(l<r)
{
int mid=l+r>>1;
if(a[mid]>=m)
r=mid;
else
l=mid+1;
}
if(a[l]!=m)
{
System.out.println("-1 -1");
continue;
}
out.write(l-1+" ");
l=1;
r=n;
while(l<r)
{
int mid=l+r+1>>1;
if(a[mid]<=m)
l=mid;
else
r=mid-1;
}
out.write(r-1+"\n");
out.flush();
}
}
}