Triplets of different numbers P35586


Statement
 

pdf   zip

Consider an array A[0..n1]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<kr\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[n1]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 00 \le \ell, +2r\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.

Public test cases
  • 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
    ----
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Official solutions
    C++
    User solutions
    C++