Consider an array . Given two indices and of the array, can you count the number of triplets of different numbers in , that is, the number of such that , , , and ? You will have to efficiently answer such questions.
Input consists of several cases. Each case starts with an between 5 and . Follow the integer numbers , …, of the array, all between 0 and . Follow different queries, each with an and an such that , , and .
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.
The expected solution solves three maximum cases in about two seconds.
Input
5 42 23 100 23 42 0 2 1 3 2 4 0 4 1 4 7 1 2 3 1 2 3 4 0 6 0 5 0 2 3 5 1 5 1 4 0 4 5 4 0 4 0 4 0 4 1 4 2 4 1 3 0 2
Output
1 0 1 4 2 ---- 20 8 1 1 4 2 4 ---- 0 0 0 0 0 ----