publicstaticvoidmain(String[] args)throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); int[] line1 = Arrays.asList(input.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray(); int n = line1[0]; int q = line1[1]; int[] line2 = Arrays.asList(input.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();
while (q-- != 0) { int target = Integer.parseInt(input.readLine()); // 查找左边界 用第一个模板 int index_l = bsearch_1(line2, 0, n - 1, target); if (line2[index_l] != target) { System.out.println("-1 -1"); } else { System.out.print(index_l + " "); // 查找右边界 用第二个模板 int index_r = bsearch_2(line2, 0, n - 1, target); System.out.print(index_r + "\n"); } } }
publicstaticintbsearch_1(int[] arr, int l, int r, int target){ while (l < r) { int mid = l + r >> 1; if (arr[mid] >= target) { r = mid; } else { l = mid + 1; } } return l; }
publicstaticintbsearch_2(int[] arr, int l, int r, int target){ while (l < r) { int mid = l + r + 1 >> 1; if (arr[mid] <= target) { l = mid; } else { r = mid - 1; } } return l; }