题目描述
输入样例
6 3
1 2 2 3 3 4
3
4
5
输出样例
3 4
5 5
-1 -1
分析
代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
static final int N = 100010;
public static int[] q = new int[N];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] s = br.readLine().split(" ");
int[] a = new int[2];
a[0] = Integer.parseInt(s[0]);//数组长度
a[1] = Integer.parseInt(s[1]);//查询次数
//读取数据
String[] data = br.readLine().split(" ");
for(int i = 0; i < a[0];i++) {
q[i] = Integer.parseInt(data[i]);
}
while(a[1] != 0) {
int x = Integer.parseInt(br.readLine());
int l = 0;
int r = a[0] - 1;
//找左边界,相同的数最左边的那一个
while(l < r) {
int mid = l+r>>1;
if(q[mid] >= x) r = mid;
else l = mid + 1;
}
if(q[l] != x) System.out.println("-1 -1");
else {
System.out.print(l+" ");
//找右边界,相同的数最右边的那一个
l = 0;
r = a[0] - 1;
while(l < r) {
int mid = l+r+1>>1;
if(q[mid] <= x) l = mid;
else r = mid - 1;
}
System.out.println(l);
}
a[1]--;
}
}
}