首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获取分段树查询的索引?

如何获取分段树查询的索引?
EN

Stack Overflow用户
提问于 2020-11-18 15:49:50
回答 1查看 401关注 0票数 2

我一直试图获得数组的和范围查询索引。假设我有一个数组,如:

代码语言:javascript
复制
{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)?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-19 19:12:23

代码语言:javascript
复制
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

我喜欢这个问题

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64896532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档