The given array is a permutation of first n integers.We are given two ranges [l1,r1] &[l2,r2] . We have to find, for each element a[i] in the first range; the number of elements in the second range which are greater than that. (Length of both the ranges are equal)
I first sorted both the ranges. Then iterating over each element in the first range ,counted the number of elements greater than that in the second range.Kept an account of the previous count..So my iteration was just O(l) [l=length of each range]
My overall time complexity is O(l*log(l))..but since I am computing this for many queries of ranges,its taking much time.
I think this question is similar to counting the number of inversions in an array..but since two ranges and many queries are involved,I am unable to go about it. I am not sure but I think that some fenwick tree structure can be of some help.Could anybody pour in some significant suggestions.