Sunday, July 28, 2013

Segment Tree


This is an amazing tree, simple but yet extremely elegant for find min/max in of a range $[a, b]$.


Problem
Given an array of number $A[1...n]$ (unsorted), what is the fastest way to answer the following query:

1) min_query(i, j):  return the minimum value between indexes $(i, j)$ where $0 \leq i \leq j \leq N$
2) max_query(i, j): return the minimum value between indexes $(i, j)$ where $0 \leq i \leq j \leq N$

The naive approach to this problem would be: loop through all element from a to b and update the max (or min):

No comments:

Post a Comment