Hey Guys,
You should all be familiar with the segment tree algorithm tutorial from TopCoder.
I have a small doubt in the same.
The code snippet for the “Query” part of the segment tree algorithm is as follows,
int query(int node, int b, int e, int M[MAXIND], int A[MAXN], int i, int j)
{
int p1, p2;
if (i > e || j < b)
return -1;
if (b >= i && e <= j)
return M[node];
p1 = query(2 * node, b, (b + e) / 2, M, A, i, j);
p2 = query(2 * node + 1, (b + e) / 2 + 1, e, M, A, i, j);
if (p1 == -1)
return M[node] = p2;
if (p2 == -1)
return M[node] = p1;
if (A[p1] <= A[p2])
return M[node] = p1;
return M[node] = p2;
}
Now What I’d like to know is, why are the results returned this way ‘M[node] = position’
Will that not alter the pre-initialized minimum positions ?
Thanks in Advance.