Pashmak and Parmida's problem
HardRating 1800CodeforcesSegment Tree & Fenwick Tree
Approach hintTry it first, then revealHide
Precompute pre[i], the occurrences of a[i] up to i, and suf[j], the occurrences of a[j] from j onward. Then count pairs i < j with pre[i] > suf[j] using a Fenwick tree indexed by count.