I have tried to use the Sparse Table Algorithm in order to compute the RMQ in this problem. However, there must be some error in either the algorithm or the binary search as seem to be overcounting the number of ordered pairs. If anyone can find the error, I would greatly appreciate your assistance. Thanks!
Here is my submission: http://codeforces.me/contest/689/submission/18959583