Prerequisites : Segment Trees Target Problem : Given an array of N elements and you have to answer Q queries of the form L R K , To Count the numbers smaller than K in range L to R. Key Idea : The key idea is to build a Segment Tree with a vector at every node and the vector contains all the elements of the subrange in a sorted order . And if you observe this segment tree structure this is some what similar to the tree formed during the merge sort algorithm ( Yes , thats why they are called Merge Sort Trees ) . Building the tree :
Querying the tree :
Build function Analysis : Build a merge sort tree takes O(NlogN) time which is same as Merge Sort Algorithm . It will take O(NlogN) memory because each number Ai will be present in at most LogN vectors (Height of the tree ) . Query function Analysis : A range L to R can divided into at most Log(N) parts, where we will perform binary search on each part . So this gives us complexity of O(LogN * LogN) per query . Handling Point Updates : The only reason that we cant handle updates on MST in this code is because its merge function takes too much time, so even if theres a point update it will lead to O(N). So the major issue is of vectors and rearranging them on updations, but why do we need vectors ? Just to find the elements smaller than K in that complete vector, right ? Lets forget about vectors and keep policy based data structure at each node which handles three queries (insertion , deletion and elements smaller than K in the set) in O(LogN) time . So now no need to rearrange vectors and we can use insertion  deletion to handle point queries . This is just an idea , we can discuss this in comments again if anyone has a doubt . Bonus : How to use this to solve the query of type Kth smallest number in range L to R ? So we can binary search over the solution and find the value which has exactly K numbers smaller than it in the given range . Complexity : O(LogN * LogN * LogAi ) per query . Why to use MST: Apart from the code simplicity, they answer queries online . We could have used some offline algorithms like MOs or even Segment tree but come on, Online Querying is great because it can be used in Dp Optimisations and stuff like that . Peace Out . asked 27 Mar '17, 20:07

You can do Kth smallest number query in range L to R, in $O(n * {\log}^{2}n)$ by building the merge sort tree on indices instead of the values. This solution doesn't depend on the values of $A[i]$, however large they may be. For implementation details, one may refer to this code for MKTHNUM problem on Spoj. answered 28 Mar '17, 00:33
1
@likecs I have written the exactly same solution as you did, but in java. I am getting TLE. Can you please help me? Note : I am using the fast I/O also. Link to my code : http://ideone.com/pHN7Z2
(14 Jun '17, 22:30)
@mayank12559, I tried to optimise your solution by making the binary serach iterative as well, but no sucess. May be the time limits of the problem are strict for java for passing with O(log^2n) approach. Also, only 4 solutions in java for this problem till date. May be you can try with C++.
(15 Jun '17, 00:39)
Thanks for the reply. I will try to write the same in c++. Java solution should also get accepted though, as c++ solutions are getting accepted with same complexity. There should not be any discrimination among languages.
(15 Jun '17, 08:22)
@likecs I have written the code in c++. On submission I am getting Wrong Answer now. Can you please help me once again? Link to my code : http://ideone.com/qC2tVy
(15 Jun '17, 16:54)
Getting same issue with JAVA. Getting TLE after 1516 cases
(09 Mar, 09:00)

Can anyone help me in the spoj problem MKTHNUM and tell me why I am getting wrong answer? MY SOLUTION answered 07 Jan '18, 18:33
