Triplets of different numbers

Consider an array A[0..n−1]A[0..n-1]. Given two indices ℓ\ell and rr of the array, can you count the number of triplets of different numbers in A[ℓ..r]A[\ell..r], that is, the number of (i,j,k)(i, j, k) such that ℓ≤i<j<k≤r\ell \le i < j < k \le r, A[i]≠A[j]A[i] \ne A[j], A[j]≠A[k]A[j] \ne A[k], and A[i]≠A[k]A[i] \ne A[k]? You will have to efficiently answer nn such questions.

Input

Input consists of several cases. Each case starts with an nn between 5 and 10510^5. Follow the nn integer numbers A[0]A[0], …, A[n−1]A[n-1] of the array, all between 0 and 10910^9. Follow nn different queries, each with an ℓ\ell and an rr such that 0≤ℓ0 \le \ell, ℓ+2≤r\ell + 2 \le r, and r<nr < n.

Output

For every query of each case, print the required answer in a line (be aware that this answer may be large). Print a line with four dashes at the end of each case.

Observation

The expected solution solves three maximum cases in about two seconds.

Problem information

Author: Salvador Roura

Generation: 2026-01-25T10:26:27.807Z

© Jutge.org, 2006–2026.
https://jutge.org