Hi !
I am trying to answer problem 155 at ACM.SGU.RU site .
My algortihm is sort by a[i] elements , then create a BST by k[i] elements.
I know my algorithm in worse case is O(n^2) and n can be 50000 ! So my answer get TLE .
What can I do for this problem ? Is my algorithm wrong or can be optimize ?
Cartesian tree is a well-known data structure, that can be created in O(N). You can read about it here.
Just to note, O(N) cartesian-tree building algorithm requires points to be sorted by x-coordinate beforehand. If that's not the case, this task is clearly no easier than general sorting problem, so the best time we can achieve is O(N log N).
i accept this problem via segment tree(its so hard to code for me) but one of my friend have a very very creative solution and i think his solution is near to your solution but you must change your approach to optimize your algorithm! sorry for my bad english!