左闭右闭 [ left,right ] [1,1]可以
while( left <= right )
if( a[mid] > target )
right = mid - 1
else if( a[mid] < target )
left = mid + 1
左闭右开 [ left,right ) [1,1)不可以while( left < right )
if( a[mid] > target ) 已经>target,开区间right = mid
right = mid
else if( a[mid] < target )
left = mid + 1
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int l, r;
int[] a = new int[n];
for(int i = 0; i < n; i ++)
a[i] = in.nextInt();
while(m -- > 0) {
int first = -1, last = -1;
int x = in.nextInt();
l = 0;
r = n - 1;
while(l <= r) {
int mid = (l + r) / 2;
if(a[mid] > x) {
r = mid - 1;
} else if(a[mid] < x) {
l = mid + 1;
} else {
first = mid;
r = mid - 1;
}
}
l = 0;
r = n - 1;
while(l <= r) {
int mid = (l + r) / 2;
if(a[mid] > x) {
r = mid - 1;
} else if(a[mid] < x) {
l = mid + 1;
} else {
last = mid;
l = mid + 1;
}
}
System.out.println(first + " " + last);
}
}
}