Reference
http://www.cnblogs.com/TenosDoIt/p/3453089.html Overview
is a balanced binary tree, where each node holds a segment (an array interval), which is mainly used to efficiently solve the dynamic query of continuous interval, and basically maintains the complexity of each operation O (Logn). Example
Problem Description: Finds the minimum value within a range from the array arr[0...n-1], where the array size is fixed, but the values of the elements in the array can be updated at any time.
The simplest solution: traversing the array interval to find the minimum value, time complexity O (n), additional space complexity O (1) stores the temporary min variable, but when the data is large and the query operation is frequent, it may time out.
Faster solution: Using a two-dimensional array to save the pre-calculated interval [I, j] The minimum value, the preprocessing time is O (n2) O (n^2), the query time-consuming O (1), but requires additional O (n2) O (n^2) space, when the volume of data is large, the space consumption is large, Also, updating the minimum value in a two-dimensional array is cumbersome when you change one of the values in the data.
The method of using line-segment tree: Preprocessing time O (n), query, update operation O (LOGN), need additional space O (n). According to this problem, construct the following two-fork tree: The leaf node is the original array in arr, the element of the non-leaf node represents the minimum value of all of its children's nodes.
For example, the array [2, 5, 1, 4, 9, 3] can construct the following two-tree (the black line is a leaf node, the blue lines are non-leaf nodes, the minimum values for the corresponding array interval) such as the root node represents the array interval arr[0 ... 5] The minimum value inside is 1:
For a balanced binary tree with n leaf nodes, there are n-1 non-leaf nodes, so there is a total of 2n-1 nodes, so the space complexity required to store segments is O (n).