In an order statistic tree, each node x is labeled with the number of nodes contained in the subtree rooted in x . Implementing both operations involves keeping track of how many nodes lie to the left of our path. In the algorithms we have studied so far, correctness tended to be easier than efficiency. In designing algorithms for optimization problem - we must prove that the algorithm in fact gives the best possible solution.
Dynamic programming is a technique for efficiently computing recurrences by storing partial results.
Dynamic programming is best understood by looking at a bunch of different examples. We would like to avoid big intermediate matrices, and since matrix multiplication is associative , we can parenthesise however we want. Matrix multiplication is not communitive , so we cannot permute the order of the matrices without changing the result. A direct recursive implementation of this will be exponential, since there is a lot of duplicated work as in the Fibonacci recurrence. There are only substrings between 1 and n .