Sumex
Time limit1sMemory limit1024 MB
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 and independent queries. In each query you are given two integers and . Consider the sequence . Your task is to compute the sum of the minimum excluded element of all sequences of form , for .
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 , , , it is , but for the sequence , , , it is .
Input
The first line contains the integers and . The second line contains integers , the initial sequence. Each of the next lines contains two integers and , describing one query.
Output
Print the answers to the queries in order, each on a new line.
Constraints
Hint
The answers to the three queries in the sample are , , and . The breakdown for each query lists the minimum excluded element of every subarray inside the query range.