Sumex

Time limit: 0.9s Memory limit: 256MB Input: Output:

You are given a sequence a1,,ana_1, \ldots, a_n and qq independent queries. For each query, you are provided with two natural numbers ll and rr. Consider the subsequence al,al+1,,ara_l, a_{l+1}, \ldots, a_{r}. Your task is to calculate the sum of the minimum excluded numbers of all subsequences of the form ai,ai+1,,aja_{i}, a_{i+1}, \ldots, a_{j}, for lijrl \leq i \leq j \leq r.

The minimum excluded number of a subsequence is the smallest natural number that does not appear in the subsequence. For example, for the subsequence 0,1,4,20, 1, 4, 2, it is 33, but for the subsequence 1,2,3,41, 2, 3, 4, it is 00.

Input data

The first line of the input contains the numbers nn and qq. The second line contains nn numbers a1,a2,,ana_1, a_2, \dots, a_n, which represent the initial sequence. Each of the following qq lines contains two numbers ll and rr, which describe, respectively, each query.

Output data

The output should contain the answers to the qq queries in order, each on a new line.

Constraints

  • 1n,q21051 \leq n, q \leq 2 \cdot 10^5
  • 0ain0 \leq a_i \leq n
  • 1lrn1 \leq l \leq r \leq n

Subtask 1 (3 points)

  • 1ain1 \leq a_i \leq n

Subtask 2 (10 points)

  • 1q2001 \leq q \leq 200
  • rl200r - l \leq 200 for each query

Subtask 3 (12 points)

  • 1n5 0001 \leq n \leq 5 \ 000

Subtask 4 (15 points)

  • Each number from 00 to n1n - 1 appears exactly once in a1,a2,,ana_1, a_2, \dots, a_n.

Subtask 5 (15 points)

  • 0ai1000 \leq a_i \leq 100
  • There are no two queries ii and jj such that li<ljl_i < l_j and rj<rir_j < r_i.

Subtask 6 (22 points)

  • l=1l = 1 for each query.

Subtask 7 (23 points)

  • No additional constraints.

Example

stdin

6 3
0 1 2 0 1 3
1 2
3 5
1 6

stdout

3
7
39

Explanations

Explanations for the first two queries:



Explanation for the third query:

Log in or sign up to be able to send submissions!