# """# 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:left,right=0,reader.length()-1whileleft<right:t1,t2,t3=(left,left+(right-left)//3,left+((right-left)//3)*2+1,)cmp=reader.compareSub(t1,t2,t2+1,t3)ifcmp==0:left=t3+1elifcmp==1:right=t2else:left,right=t2+1,t3returnleft
/** * // This is ArrayReader's API interface. * // You should not implement it, or speculate about its implementation * interface ArrayReader { * // 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]) * public int compareSub(int l, int r, int x, int y) {} * * // Returns the length of the array * public int length() {} * } */classSolution{publicintgetIndex(ArrayReaderreader){intleft=0,right=reader.length()-1;while(left<right){intt1=left,t2=left+(right-left)/3,t3=left+(right-left)/3*2+1;intcmp=reader.compareSub(t1,t2,t2+1,t3);if(cmp==0){left=t3+1;}elseif(cmp==1){right=t2;}else{left=t2+1;right=t3;}}returnleft;}}
/** * // This is the ArrayReader's API interface. * // You should not implement it, or speculate about its implementation * class ArrayReader { * public: * // 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]) * int compareSub(int l, int r, int x, int y); * * // Returns the length of the array * int length(); * }; */classSolution{public:intgetIndex(ArrayReader&reader){intleft=0,right=reader.length()-1;while(left<right){intt1=left,t2=left+(right-left)/3,t3=left+(right-left)/3*2+1;intcmp=reader.compareSub(t1,t2,t2+1,t3);if(cmp==0){left=t3+1;}elseif(cmp==1){right=t2;}else{left=t2+1;right=t3;}}returnleft;}};
/** * // This is the ArrayReader's API interface. * // You should not implement it, or speculate about its implementation * type ArrayReader struct { * } * // 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]) * func (this *ArrayReader) compareSub(l, r, x, y int) int {} * * // Returns the length of the array * func (this *ArrayReader) length() int {} */funcgetIndex(reader*ArrayReader)int{left,right:=0,reader.length()-1forleft<right{t1,t2,t3:=left,left+(right-left)/3,left+(right-left)/3*2+1cmp:=reader.compareSub(t1,t2,t2+1,t3)ifcmp==0{left=t3+1}elseifcmp==1{right=t2}else{left,right=t2+1,t3}}returnleft}