algo. number_range

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), q = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
while (q-- > 0) {
int target = sc.nextInt();
int l = 0, r = n - 1;
while (l < r) {
int mid = l + r >> 1;
if (arr[mid] >= target) r = mid;
else l = mid + 1;
}
if (arr[l] != target) {
System.out.println("-1 -1");
continue;
}
int start = l;
l = 0; r = n - 1;
while (l < r) {
int mid = l + r + 1 >> 1;
if (arr[mid] <= target) l = mid;
else r = mid - 1;
}
int end = l;
System.out.println(start + " " + end);
}
}
}

algo. number_range
http://elwinliu.com/2024/09/12/algo. number_range/
Author
Elwin
Posted on
September 12, 2024
Licensed under