AcWing 789. 数的范围 简单两次二分查找即可
原题链接
简单
作者:
皓首不倦
,
2020-08-06 19:17:39
,
所有人可见
,
阅读 434
'''
简单两次二分即可
'''
n, q = map(int, input().split())
arr = list(map(int, input().split()))
for _ in range(q):
val = int(input())
l, r = 0, len(arr)-1
ans1 = -1
while l <= r:
mid = l + (r-l) // 2
if arr[mid] == val:
ans1 = mid
r = mid - 1
elif arr[mid] < val:
l = mid + 1
else:
r = mid - 1
if ans1 == -1:
print(-1, -1)
else:
ans2 = -1
l, r = ans1, len(arr)-1
while l <= r:
mid = l + (r - l) // 2
if arr[mid] == val:
ans2 = mid
l = mid + 1
elif arr[mid] < val:
l = mid + 1
else:
r = mid - 1
print(ans1, ans2)