Under construction

Still being built. Things may change

Prof. Pang's sequence

Time limit3sMemory limit1024 MB

Summary
For each query [l, r], count the subarrays a_i..a_j inside it whose number of distinct values is odd.
Level

Hard10 of 10

Topics
Divide and conquer, Array, Hash map, Dynamic programming
Solved
No attempts yet

Problem

Prof. Pang is given a fixed sequence a1,…,ana_1, \ldots, a_n and mm queries.

Each query gives two integers ll and rr with 1≤l≤r≤n1\le l\le r\le n. For each query, count the pairs of integers (i,j)(i, j) with l≤i≤j≤rl\le i\le j\le r such that the number of distinct integers in ai,…,aja_i, \ldots, a_j is odd.

Input

The first line contains one integer nn (1≤n≤5×1051\le n\le 5\times 10^5).

The second line contains nn integers a1,…,ana_1, \ldots, a_n (1≤ai≤n1\le a_i\le n), separated by single spaces.

The next line contains one integer mm (1≤m≤5×1051\le m\le 5\times 10^5).

Each of the next mm lines contains two integers ll and rr (1≤l≤r≤n1\le l\le r\le n), separated by a single space.

Output

For each query, output the answer on its own line.

Examples3

  1. Example 1

    Input
    5
    1 2 3 2 1
    5
    1 5
    2 4
    1 3
    2 5
    4 4
    
    Expected output
    10
    3
    4
    6
    1
    
  2. Example 2

    Input
    5
    2 3 5 1 5
    5
    2 3
    1 1
    1 3
    2 5
    2 4
    
    Expected output
    2
    1
    4
    6
    4
    
  3. Example 3

    Input
    10
    2 8 5 1 10 5 9 9 3 5
    10
    6 8
    1 2
    3 5
    5 7
    1 7
    3 9
    4 9
    1 4
    3 7
    2 5
    
    Expected output
    4
    2
    4
    4
    16
    16
    12
    6
    9
    6