Is it possible to findT kth largest element in the tree. With update and query operation ?
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 160 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | Dominater069 | 154 |
8 | awoo | 154 |
10 | luogu_official | 151 |
Is it possible to findT kth largest element in the tree. With update and query operation ?
Name |
---|
Yes it is. (If I understood the problem right)
In nodes you should keep how many nodes are there already in the subtree. Lets call it size of the subtree. (size of parent is size of left tree + size of right tree)
Recursive Query goes like:
When you see your left subtree's size is > k, you check the left subtree, else you look in the right subtree for a tree of size k — size of left subtree
Suppose you are given numbers from 0 to n — 1, now you've to find the kTh element. (If the numbers are enough large, I'd map them with relative small numbers.)
In the interval tree, every node will contain how many elements are there in it's range. Now you can use binary search to find the answer. Here is the sample code:
UPD: There is a simpler version of this interval tree. I know it by the name of 'misof tree'. Here is a sample code. I've changed the original code though but the basic idea is same.
Would you please elaborate briefly how does this tree work? Or give a link to a paper or article if possible?
Thank you
I just have the code of misof tree, nothing else. So I'm going to explain as much as I can. It works in the same way as in the interval tree.
A node contains the count of the numbers that are in it's range. Now when we've to insert a number, we start from the leaf, increment the count of the leaf and then climb up to its parent node. We continue this process until we reach to the root. Delete operation works in same way.
A problem is that, it only works for numbers from 0 to n-1. So we may have to map the input numbers. Also the tree has to be perfect binary tree to make the searching work. So we've to choose a suitable value for the
leaf
such that leaf >= n and leaf = 2^x form.Suppose we've to find the kTh number. From a node, we should decide should we go to left or to right. If the left node contains equal to or more than k numbers, then left sub-tree contains the result, else the right sub-tree has it.
I hope this explains.
Aha thank you
thanks
I am confused. In your code, the 4th line and 6th line both have same if condition. Is it a typo?
Fixed everything :|
I have a N*sqrt(N)*log(sqrt(N)) solution. And it got accepted. How is this possible?
It has a precomputation of O(N*sqrt(N)*log(sqrt(N))) and query time O(sqrt(N)*log(sqrt(N))). I was pretty sure while I was writing the code for this that this code would get a tle.