Question

I was learning segment tree from this page : http://letuskode.blogspot.com/2013/01/segtrees.html

I am finding trouble to understand various fragments of code.I will ask them one by one.Any help will be appreciated.

Node declaration:

struct node{
    int val;
    void split(node& l, node& r){}
    void merge(node& a, node& b)
    {
        val = min( a.val, b.val );
    }
}tree[1<<(n+1)];

1.What will the split function do here ?

2.This code is used for RMQ . So I think that val will be the minimum of two segments and store it in some other segment.Where the value will be saved?

Range Query Function:

node range_query(int root, int left_most_leaf, int right_most_leaf, int u, int v)
{
    //query the interval [u,v), ie, {x:u<=x<v}
    //the interval [left_most_leaf,right_most_leaf) is 
    //the set of all leaves descending from "root"
    if(u<=left_most_leaf && right_most_leaf<=v)
        return tree[root];
    int mid = (left_most_leaf+right_most_leaf)/2, 
        left_child = root*2, 
        right_child = left_child+1;
    tree[root].split(tree[left_child], tree[right_child]);
    //node l=identity, r=identity;
    //identity is an element such that merge(x,identity) = merge(identity,x) = x for all x
    if(u < mid) l = range_query(left_child, left_most_leaf, mid, u, v);
    if(v > mid) r = range_query(right_child, mid, right_most_leaf, u, v);
    tree[root].merge(tree[left_child],tree[right_child]);
    node n;
    n.merge(l,r);
    return n;
}

1.What is the use of the array tree and what values will be kept there ?

2.What will this statement : tree[root].split(tree[left_child], tree[right_child]); do ?

3.What will those statements do ? :

node n;
n.merge(l,r);
return n;

Update and Merge Up functions: I am not understanding those two functions properly:

void mergeup(int postn)
{
    postn >>=1;
    while(postn>0)
    {
        tree[postn].merge(tree[postn*2],tree[postn*2+1]);
        postn >>=1;
    }
}
void update(int pos, node new_val)
{
    pos+=(1<<n);
    tree[pos]=new_val;
    mergeup(pos);
}

Also what should I write inside the main function to make this thing work? Suppose I have an array A={2,3,2,4,20394,21,-132,2832} , How I can use this code to find RMQ(1,4)?

Was it helpful?

Solution

1.What will the split function do here ?

Nothing: the function body is empty. There may be some other implementation where an action is required. (See Example 3) And see answer to 2b

2.... Where the value will be saved?

In the "val" field of the class/struct for which "merge" is called.

1b.What is the use of the array tree and what values will be kept there ?

Array "node tree[...]" stores all the nodes of the tree. Its element type is "struct node".

2b.What will this statement : tree[root].split(tree[left_child], tree[right_child]); do ?

It calls split for the struct node that's stored at index root, passing the nodes of the split node's children to it. What it actually does to tree[root] depends on the implementation of "split".

3b.What will those statements do ? :

node n;         // declare a new node
n.merge(l,r);   // call merge - store the minimum of l.val, r.val into n.val
return n;       // terminate the function and return n

I'll have to figure out the answers to your last Qs in the context of that code. Will take a little time.

Later This should build a tree and do a range query. I'm not sure that the code on that page is correct. Anyway, the interface for range_query is not what you'd expect for easy usage.

int main(){
  int a[] = { -132, 1, 2, 3, 4, 21, 2832, 20394};
  for( int i = 0; i < 8; i++ ){
    node x;
    x.val = a[i];
    update( i, x);
  }

  node y = range_query(0, 8, 15, 8 + 1, 8 + 4 );
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top