我一直试图获得数组的和范围查询索引。假设我有一个数组,如:
{1, 3, 5, 7, 9, 11}
With a segment tree like that
{36, 9, 27, 4, 5, 16, 11, 1, 3, DUMMY, DUMMY, 7, 9, DUMMY, DUMMY}这是一张它的图片。

如何获得段树数组中查询0-2的索引(例如,0-2索引为1,3-5索引为2)?
发布于 2020-11-19 19:12:23
import math
#get a size of list and return list of lists
#where every list is the indexes that should be on that place in the tree
def list_divid(size):
index_list = list(range(size))
out = []
out.append([index_list[:]])
divide = True
while divide:
divide = False
for i in out[-1]:
if len(i)>1:
divide = True
if not divide:
break
add_to_out = []
for i in range(len(out[-1])):
if len(out[-1][i])==1:
add_to_out.append([])
if len(out[-1][i])%2==0:
middle_index = int((len(out[-1][i]))/2)
add_to_out.append(out[-1][i][:middle_index])
add_to_out.append(out[-1][i][middle_index:])
if len(out[-1][i])%2!=0:
middle_index = math.ceil((len(out[-1][i]))/2)
add_to_out.append(out[-1][i][:middle_index])
add_to_out.append(out[-1][i][middle_index:])
out.append(add_to_out)
rv = []
for i in out:
rv+=i
return rv
#getting first and last index, and list size
#create the tree using list_divid
#then find the index of the list in the list of lists
def get_index1(first,last, list_len):
tree = list_divid(list_len)
for i in range(len(tree)):
if tree[i][0] == first and tree[i][-1] == last:
return i我喜欢这个问题
https://stackoverflow.com/questions/64896532
复制相似问题