implement 2d range tree c++

2.1k Views Asked by At

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

There are 1 best solutions below

4
Mushy On

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.