#C6763. Unique Subarray Sum

    ID: 50559 Type: Default 1000ms 256MiB

Unique Subarray Sum

Unique Subarray Sum

You are given an array of n integers and q queries. Each query consists of two indices l and r (1-indexed). For each query, you need to compute the sum of the subarray from index l to index r (i.e., \(\sum_{i=l}^{r} a_i\)). However, if any element appears more than once in the subarray, you should output \(-1\) for that query.

Input Constraints:

  • 1 ≤ n, q ≤ 105 (depending on the version of the problem)
  • 1 ≤ ai ≤ 109
  • 1 ≤ l ≤ r ≤ n

Note: The indices in the queries are 1-indexed.

inputFormat

The input is read from standard input (stdin) with the following format:

  1. The first line contains two integers n and q, representing the number of elements in the array and the number of queries respectively.
  2. The second line contains n space-separated integers representing the array.
  3. Each of the next q lines contains two integers l and r representing the bounds of the query.

outputFormat

For each query, output a single line. The line should contain the sum of the elements from index l to r if all elements in the subarray are distinct; otherwise, output -1.

## sample
6 3
1 2 3 3 2 1
1 3
4 6
2 5
6

6 -1

</p>