# """# This is ArrayReader's API interface.# You should not implement it, or speculate about its implementation# """#class ArrayReader(object):# # Compares the sum of arr[l..r] with the sum of arr[x..y]# # return 1 if sum(arr[l..r]) > sum(arr[x..y])# # return 0 if sum(arr[l..r]) == sum(arr[x..y])# # return -1 if sum(arr[l..r]) < sum(arr[x..y])# def compareSub(self, l: int, r: int, x: int, y: int) -> int:## # Returns the length of the array# def length(self) -> int:#classSolution:defgetIndex(self, reader:'ArrayReader')->int:
n = reader.length()
i, j =0, n -1while i < j -1:
mid =(i + j)//2if(j - i)%2==1:
l, r, x, y = i, mid, mid +1, j
else:
l, r, x, y = i, mid, mid, j
res = reader.compareSub(l, r, x, y)if res ==1:
i, j = l, r
elif res ==-1:
i, j = x, y
else:return mid # 相等必定mid搞事if reader.compareSub(i, i, j, j)==1:return i
return j