Is it possible to findT kth largest element in the tree. With update and query operation ?
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
Is it possible to findT kth largest element in the tree. With update and query operation ?
Название |
---|
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.