This page is still under construction.

Parts of this page are still being built. What you see may change.

Sumex

Time limit1sMemory limit1024 MB

Summary
Given an array and q queries, find the sum of the mex (minimum excluded value) over every subarray inside each query range [l, r].
Level

Hard9 of 10

Topics
Segment tree, Array, Sorting
Solved
No attempts yet

Problem

You are given a sequence a_1,…,a_na\_1,\dots , a\_n and qq independent queries. In each query you are given two integers ll and rr. Consider the sequence a_l,a_l+1,…,a_ra\_l , a\_{l+1}, \dots , a\_r. Your task is to compute the sum of the minimum excluded element of all sequences of form a_i,a_i+1,…,a_ja\_i , a\_{i+1}, \dots , a\_j, for l≤i≤j≤rl \le i \le j \le r.

The minimum excluded element of a sequence is the smallest non-negative integer that does not appear in the sequence. For example, for the sequence 00, 11, 44, 22 it is 33, but for the sequence 11, 22, 33, 44 it is 00.

Input

The first line contains the integers nn and qq. The second line contains nn integers a_1,a_2,…,a_na\_1, a\_2, \dots , a\_n, the initial sequence. Each of the next qq lines contains two integers ll and rr, describing one query.

Output

Print the answers to the qq queries in order, each on a new line.

Constraints

  • 1≤n,q≤2×1051 \le n, q \le 2 \times 10^5
  • 0≤a_i≤n0 \le a\_i \le n
  • 1≤l≤r≤n1 \le l \le r \le n

Hint

The answers to the three queries in the sample are 33, 77, and 3939. The breakdown for each query lists the minimum excluded element of every subarray inside the query range.

Examples1

  1. Example 1

    Input
    6 3
    0 1 2 0 1 3
    1 2
    3 5
    1 6
    
    Expected output
    3
    7
    39