Rewrote Triplets with Binary Indexed Tree. O(N*log(N))