In an unordered set containing n elements, the time complexity of searching for the I-th sequence statistic is O (n ). By establishing a specific structure, any sequence statistic can be found in O (lgn) time. This is the sequence statistics tree based on the red and black trees mentioned below.
Compared with the basic data structure, the Ordered Statistics tree adds a Domain Size [X]. This field contains the number of nodes (including X itself) of the subtree rooted in X ). The size field satisfies the equation:
Size [x] = size [left [x] + size [right [x] + 1
Then, based on the sorting feature of the red and black trees, we can complete the following operations in O (lgn) time.
Shows the sequence statistics tree:
Search for elements smaller than I
Implement OS-select (X, I) to return the pointer to the node containing the I small keyword in the subtree with X as the root. Based on the nature of the sorting tree, we know that the key value of the Left subtree is smaller than the key value of the root node, and the key value of the right node is greater than the root node, this is equivalent to the partition of the static sequence statistic. At the same time, we know the size of the left and right subtree, And we can determine which branch to perform the next search.
The entire OS-select (x, I) process is as follows:
OS-select (X, I) <br/> r align size [left [x] + 1 <br/> If I = r <br/> then return x <br/> elseif I <r <br /> then return OS-select (left [X], i) <br/> else return OS-select (left [X], I-r) <br/>
Each call is bound to decrease by one layer, so the time complexity of OS-select is O (lgn)
Determine the rank of an element
The rank indicates the position of node X in the linear sequence. Based on the nature of the sorting tree, that is, the position of node X in the middle-order traversal. We can use the features of sequential traversal.
The OS-rank (t, x) process is as follows:
OS-rank (t, x) <br/> r limit size [left [x] + 1 <br/> Y limit x <br/> while y =root [T] <br/> do if y = right [p [y] <br/> then r weight R + size [left [y] + 1 <br/> Y weight P [y] <br/>
The time complexity of OS-rank is O (lgn)
When the time for establishing the sequence statistics tree is O (nlgn), isn't it more complicated than general static search O (n? In fact, the applications of these two methods are different. If you only search once or several times, static search is faster. If you search multiple times (the number of searches is comparable to N ), the Ordered Statistics tree shows its advantages. In addition, the sequence statistics tree can also facilitate the insertion and deletion of supported elements in the O (lgn) time. These two aspects are incomparable to the static sequence statistics method.
Think about the problem. How can we use the sequence statistics tree to solve some problems?
14.1-5Given the order of n elements, how does one determine the I-th successor of an element x and a natural number I in the O (nlgn) Time?
Analysis and answer:
First, use element x to obtain its rank R, and then find the element smaller than I + R.
The whole process of OS-rank (t, x, I) is as follows:
OS-rank (t, x, I) <br/> r sort OS-rank (t, x) <br/> return OS-select (X, I + r)
14.1-7This section describes how to use the sequence statistics tree to count the reverse order pairs in an array of N in the O (nlgn) time)
Analysis and answer:
If the array of n elements is marked as A1, A2, A3 ,..., AI ,..., then, we can find the number VI of <AJ, AI> and j <I in the descending order ending with the AI element I.
Then the total number of Reverse Order pairs is v.
V = V1 + V2 +... + VN
This can be considered as a dynamic set of A1, A2 ,..., in AI, we can find the rank of AI (that is, the position of Ai In the sorted sequence). If the rank is R, the number of Reverse Order Pairs
Vi = I-r
So that we can calculate it iteratively.
Inversion (a) <br/> T has failed <br/> V has 0 <br/> for I have 1 to n <br/> do OS-insert (t, A [I]) <br/> r sort OS-rank (t, a [I]) <br/> V sort v + (I-r) <br/> return v <br/>
14-2Joseph's arrangement
The definition of the Joseph's problem is as follows: Suppose n people are arranged in a ring and there is a positive integer m <= n. Starting from a specified person and reporting along the ring, the nth person will be listed and the number of reports will continue. This process continues until everyone is listed. The order of columns for each person defines the (n, m)-Joseph arrangement of integers 1, 2,..., and N. For example, <3, 6, 2, 7, 5, 1, 4> is arranged in the format of (7, 3)-Joseph.
A) suppose M is a constant. Describe an O (n) Time Algorithm to make the given integer N, output (n, m)-Joseph Arrangement
B) suppose M is not a constant. Describe an O (nlgn) time algorithm so that the given integers n and m are output (n, m)-Joseph's arrangement.
Analysis and answer:
A) Each person corresponds to one element with n elements in total and the key value is numbered. The n elements constitute a cyclic double-stranded table, so the time complexity for each person to column is O (M), and the total time complexity is O (Nm). Since M is a constant, it is the time complexity of O (n.
B) If M is not a constant, the sequence statistics tree can be used for dynamic processing. Select an element from the column each time, delete it in the sequence statistics tree, search again, and iterate. If you previously deleted the element at position J of the current set, the next time you deleted the element at position J of the remaining set + m, modulo the number of elements in the remaining set. The entire process is as follows:
Joseph PHUs (n, m) <br/> T have been deleted <br/> for I have 1 to n <br/> do create a node X with key [x] = I <br/> OS-insert (T, x) <br/> K then n <br/> J then M <br/> while K> 2 <br/> do X then OS-select (root [T], j) <br/> Print key [x] <br/> OS-delete (t, x) <br/> K keys K −1 <br/> J keys (J + M −2) mod k) + 1