implement 2d range tree c++

倖福魔咒の 提交于 2019-12-11 13:52:17

问题


I have been trying to understand range tree for some time, but i still can't understand it.

Can someone explain it to me with an implementation, because i want to use it to solve 2D RMQ, i know segment tree, and my teacher tell me range tree is similar to 2d segment tree, but i just can't think how the space complexity can be less than n^2 like 2d segment tree.

I'm not sure about this, but is it true that, it's like merge sort, so the memory will be less than n^2 using vector

void merge(vector<int> &res,vector<int> &a,vector<int> &b)
{
    int la = 0;
    int lb = 0;
    int sa = SIZE(a);
    int sb = SIZE(b);
    while(la < sa || lb < sb)
    {
        if (la >= sa) {res.pb(b[lb]);lb++;}
        else if (lb >= sb) {res.pb(a[la]);la++;}
        else
        {
            if (a[la] < b[lb]) {res.pb(a[la]);la++;}
            else {res.pb(b[lb]);lb++;}
        }
    }
}

void build(int n,int l,int r)
{
    if (l == r)
    {
        rtree[n].pb(ar[l]);
        return;
    }
    int m = (l+r)/2;
    build(2*n,l,m);
    build(2*n+1,m+1,r);
    merge(rtree[n],rtree[2*n],rtree[2*n+1]);
}

Thanks :)


回答1:


Have you checked the usual cookie jars?

Such as Wikipedia, a former lecture of several I found on Google and in addition to a Stack Question?

I did not have the pleasure of learning it at University but it appears to be an interesting data structure.



来源:https://stackoverflow.com/questions/15663914/implement-2d-range-tree-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!