I am learning segment trees (on the example of range minimum query) and have some problem understanding the reason behind the part of the algorithm for querying a segment tree.
I have no problem understanding the part of construction the segment tree, I can not understand why in this function:
int RMQUtil(int *st, int ss, int se, int qs, int qe, int index)
{
// If segment of this node is a part of given range, then return the min of the segment
if (qs <= ss && qe >= se)
return st[index]; // this is the part I am struggling with
// If segment of this node is outside the given range
if (se < qs || ss > qe)
return INT_MAX;
// If a part of this segment overlaps with the given range
int mid = getMid(ss, se);
return minVal(RMQUtil(st, ss, mid, qs, qe, 2*index+1), RMQUtil(st, mid+1, se, qs, qe, 2*index+2));
}
If segment of this node is a part of given range, then return the min of the segment.