整数二分
注意一下l和r的范围即可
import java.util.*;
public class Main {
static final int N = 100010;
static int[] a = new int[N];
static int n, q;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt(); q = sc.nextInt();
sc.nextLine();
String[] s = sc.nextLine().split(" ");
for (int i = 0; i < n; i++) a[i] = Integer.parseInt(s[i]);
while (q -- > 0) {
int k = sc.nextInt();
int l = 0, r = n - 1;
while (l < r) {
int mid = l + r >> 1;
if (a[mid] >= k) r = mid;
else l = mid + 1;
}
if (a[l] != k) {
System.out.println("-1 -1");
continue;
}
System.out.print(l + " ");
l = 0; r = n - 1;
while (l < r) {
int mid = l + r + 1 >> 1;
if (a[mid] <= k) l = mid;
else r = mid - 1;
}
System.out.println(l);
}
}
}