题目描述
主要是练习两种二分法的模板
def divid_sort(target):
global num
def left_sort(target):
l,r=0,len(num)-1
while l<r:
mid = (l+r)>>1
if num[mid]>=target:
r = mid#模板一
else:
l = mid+1
return l if num[l]==target else -1
def right_sort(target):
l,r = 0, len(num)-1
while l<r:
mid = (l+r+1)>>1
if num[mid]<=target:
l = mid#模板二
else:
r = mid -1
return l if num[l]==target else -1
return str(left_sort(target))+' '+str(right_sort(target))
n, q = [int(i) for i in input().split()]
num = [int(i) for i in input().split()]
ans = []
for _ in range(q):
ans.append(divid_sort(int(input())))
print('\n'.join(map(str,ans)))