Partition tree Summary

Source: Internet
Author: User

I recently learned about the tree division, and I will summarize it below.

We can generally use the line segment tree to solve the problem when finding the maximum value of the range. However, if the k-th or k-th interval is required, the line segment tree is a little inadequate, this is something we can do with the partition tree. The Division tree uses the idea of fast sorting. First, we set the median of the current interval to mid (in order to quickly find the median of the interval, we usually sort the original sequence first.) Then we place the smaller part of the interval than the mid value into the left subtree, and the larger part of the interval than the mid value into the right subtree, if the number is equal to the value of mid, we should discuss that some of them need to be placed in the left subtree, while others need to be placed in the right subtree. Note that the relative sequence of numbers remains unchanged when we put them in the subtree. In this way, the space consumption is O (nlogn) when the interval is halved ). The following is an example.

 


Suppose the sequence length is 9, which is 3 5 7 3 4 9 4 2 5 in sequence. How can we see what it looks like after the build is complete.

Sort [] [2 3 3 4 4 5 5 7 9]


Tree [0] [3 5 7 3 4 9 4 2 5]

Tree [1] [3 3 4 4 2] [5 7 9 5]

Tree [2] [3 3 2] [4 4] [5 5] [7 9]

Tree [3] [3 2] [3] [4] [4] [5] [5] [7] [9]

Tree [4] [2] [3] [3] [4] [4] [5] [5] [7] [9]

 


Well, after the tree is created, the most critical query is now. We set the function query (p, l, r, s, t, k) indicates the k-th small value in the interval [s, t] of the subtree of the p-th layer in the subtree of [l, r. We set sum [I] in each subtree to indicate the number of numbers in the range [l, I] to be placed in the left subtree, which is easy to get, sum [t]-sum [s-1] indicates how many trees in the interval [s, t] are put into the left subtree. We may set this value to num, if k <= num, we can know that the number we are looking for must be in the left subtree; otherwise, it must be in the right subtree. Next we just need to continue traversing down, when l = r, we can determine the number we are looking for. It is easy to know that the complexity of this step is O (log n ), now the key point is how to determine the value of ls and t when it becomes more convenient. In fact, it is easy to draw a picture by yourself. The following is only a conclusion. Here we first set snum numbers in the left subtree in the interval [l, s-1, the midpoint of the current interval is mid,

If k <= num, we return query (p + 1, l, mid, l + snum, l + sum [t]-1, k ), (the number to be searched is in the left subtree)


Otherwise, we return query (p + 1, mid + 1, r, mid + 1-l + s-snum, mid + 1-l + t-sum [t], k-num ); (the number to be searched is on the right subtree)

Here is another example. The number is the same as above.

Now we need to find the decimal number in the range [3rd.

 

Sort [] [2 3 3 4 4 5 5 7 9]


Tree [0] [3 5 7 3 4 9 4 2 5]

Tree [1] [3 3 4 4 2] [5 7 9 5]

Tree [2] [3 3 2] [4 4] [5 5] [7 9]

Tree [3] [3 2] [3] [4] [4] [5] [5] [7] [9]

Tree [4] [2] [3] [3] [4] [4] [5] [5] [7] [9]

The numbers on the orange background are the required range.


We first look for the first layer of the tree, that is, query (,). We found that three numbers in the [] range are placed in the left subtree, satisfying k <= num, therefore, we can find it in the left subtree and call query (, 2 ).

In the second layer tree, we found that the interval [2, 4] has only one number placed in the left subtree. Therefore, we must find the number in the right subtree and call query (2, 4, 5, 4, 5, 2 ).

In the layer-3 tree, we found that the interval [] has only one number placed in the left subtree. Similarly, we should look for it in the right subtree and call query ). Now we can find that l = r, then we can determine that we have found the number to be searched, and then return 4. We can see that the number of 3rd small values on the range [2, 7] is 4.

The basic tree is here, and my code is given below:

[Cpp]
# Include <iostream>
# Include <string. h>
# Include <stdio. h>
# Include <algorithm>
# Define maxn100010
# Define mid (l + r)> 1)
Using namespace std;
Int t [20] [maxn], sum [20] [maxn];
Int a [maxn], as [maxn];
// Find the k-th Small Partition tree in the interval
Void build (int p, int l, int r)
{
Int lm = 0, I, ls = l, rs = mid + 1; // lm indicates the number of digits that should be placed in the left subtree and equal to the median, ls indicates the start position of the Left subtree, and rs indicates the start position of the right subtree.
For (I = mid; I> = l; I --) // evaluate lm
{
If (as [I] = as [mid])
Lm ++;
Else
Break;
}
For (I = l; I <= r; I ++)
{
If (I = l) // special discussion here
Sum [p] [I] = 0;
Else
Sum [p] [I] = sum [p] [I-1];
If (t [p] [I] = as [mid]) // if it is equal to the median, determine whether it should be placed in the left subtree.
{
If (lm)
{
Lm --;
Sum [p] [I] ++;
T [p + 1] [ls ++] = t [p] [I];
}
Else
T [p + 1] [rs ++] = t [p] [I];
}
Else if (t [p] [I] <as [mid]) // the K-th largest search interval is>
{
Sum [p] [I] ++;
T [p + 1] [ls ++] = t [p] [I];
}
Else
T [p + 1] [rs ++] = t [p] [I];
}
If (l = r)
Return;
Build (p + 1, l, mid );
Build (p + 1, mid + 1, r );
}
Int query (int p, int l, int r, int ql, int qr, int k)
{
Int s, ss; // s indicates the number of left subtree placed in the range from l to ql-1, ss indicates the number of left subtree placed in the range [ql, qr]
If (l = r) // find the desired number
Return t [p] [l];
If (ql = l)
S = 0, ss = sum [p] [qr];
Else
S = sum [p] [ql-1], ss = sum [p] [qr]-s;
If (k <= ss) // the number to be searched is in the left subtree.
Return query (p + 1, l, mid, l + s, l + sum [p] [qr]-1, k );
Else // the number to be searched is in the right subtree
Return query (p + 1, mid + 1, r, mid + 1-l + ql-s, mid + 1-l + qr-sum [p] [qr], k-ss );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.