Thursday, 15 April 2010

c - Query code unexpectedly modifies array instead of returning the index -



c - Query code unexpectedly modifies array instead of returning the index -

in this tutorial, why query() function modify content of m in lastly 4 lines of code, instead of returning node index?

i think there's problem approach: modified m array won't able handle future rmq [range minimum queries], since m[node] stores index of recent query.

here's code i'm talking about:

int query(int node, int b, int e, int m[maxind], int a[maxn], int i, int j) { int p1, p2; // if current interval doesn't intersect // query interval homecoming -1 if (i > e || j < b) homecoming -1; // if current interval included in // query interval homecoming m[node] if (b >= && e <= j) homecoming m[node]; // compute minimum position in // left , right part of interval 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); // homecoming position overall // minimum if (p1 == -1) homecoming m[node] = p2; if (p2 == -1) homecoming m[node] = p1; if (a[p1] <= a[p2]) homecoming m[node] = p1; homecoming m[node] = p2; }

it bug. you're right, should homecoming index without updating array.

c data-structures segment-tree

No comments:

Post a Comment