解决刺伤查询中的分段树

时间:2013-08-08 10:11:49

标签: data-structures binary-tree segment-tree

在解决刺伤问题时,哪里可以找到段树实现的参考?刺穿问题要求返回包含给定点q的所有间隔(来自形式[a,b],1< = a< = b< = n,其中给出n)的预定义m间隔集合。“ / p>

转角案例:仅一个点的间隔[a,a]。

到目前为止,我设法获得了以下代码,但它确实无效,我无法真正找到错误:

    int n, m, x[100005], y[100005], q;
    list< int > segm[200005];
    //Search for q in the segment tree
    void segmQuery(int node, int left, int right)
    {
        for (list<int>::iterator i = segm[node].begin(); i != segm[node].end(); i++)
        {
            //Process the segment
        }
        if (left < right)
        {
            int middle = left + (right - left) / 2;
            if (q <= middle)
                segmQuery(2 * node, left, middle);
            if (q > middle)
                segmQuery(2 * node + 1, middle + 1, right);
        }
    }
    //Add the q-th segment into the tree
    //Segment is denoted by x[q], y[q]
    void segmAdd(int node, int left, int right)
    {
        if ((x[q] <= left) && (right <= y[q]))
            segm[node].push_back(q);
        else
        {
            int middle = left + (right - left) / 2;
            if (x[q] <= middle)
                segmAdd(2 * node, left, middle);
            if (y[q] > middle)
                segmAdd(2 * node + 1, middle + 1, right);
        }
    }

0 个答案:

没有答案