include [HTML_REMOVED]
// 二分查找函数,用于找到元素k在数组arr中的起始位置
int binarySearchFirst(int arr[], int size, int key) {
int left = 0, right = size - 1, mid;
while (left <= right) {
mid = left + (right - left) / 2;
if (arr[mid] == key) {
if (mid == 0 || arr[mid - 1] < key) {
return mid;
} else {
right = mid - 1;
}
} else if (arr[mid] < key) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
// 二分查找函数,用于找到元素k在数组arr中的终止位置
int binarySearchLast(int arr[], int size, int key) {
int left = 0, right = size - 1, mid;
while (left <= right) {
mid = left + (right - left) / 2;
if (arr[mid] == key) {
if (mid == size - 1 || arr[mid + 1] > key) {
return mid;
} else {
left = mid + 1;
}
} else if (arr[mid] < key) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
int main() {
int n, q;
scanf(“%d %d”, &n, &q);
int arr[n];
for (int i = 0; i < n; i++) {
scanf(“%d”, &arr[i]);
}
for (int i = 0; i < q; i++) {
int key;
scanf("%d", &key);
int first = binarySearchFirst(arr, n, key);
int last = binarySearchLast(arr, n, key);
if (first == -1) {
printf("-1 -1\n");
} else {
printf("%d %d\n", first, last);
}
}
return 0;
}