I was solving the question PSHTRG. Basically it is a segment tree problem. I stored 45 greatest elements of each range as the node in segment tree and then for each query I used these elements to find the answer. But my code is giving TLE. I think there is nothing wrong with the logic but I am not able to optimize the code. Please can someone help. Thanks in advance.
Your update function works in O(N).You must add this "if(l > x || r < x) return;" to make it O(logN). And then your code should be accepted.
Thanks a lot friend. I just can't believe I missed that and wasted a lot of time.