atcoder#ABC293G. [ABC293G] Triple Index

[ABC293G] Triple Index

Score : 600600 points

Problem Statement

You are given a length-NN sequence (A1,A2,,AN)(A_1, A_2, \ldots, A_N) of positive integers, and QQ queries about the sequence.

For each q=1,2,,Qq = 1, 2, \ldots, Q, the qq-th query gives you an integer pair (lq,rq)(l_q, r_q); print the number of integer triplets (i,j,k)(i, j, k) such that

  • lqi<j<krql_q \leq i \lt j \lt k \leq r_q, and
  • Ai=Aj=AkA_i = A_j = A_k.

Constraints

  • 1N2×1051 \leq N \leq 2 \times 10^5
  • 1Q2×1051 \leq Q \leq 2 \times 10^5
  • 1Ai2×1051 \leq A_i \leq 2 \times 10^5
  • 1lqrqN1 \leq l_q \leq r_q \leq N
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN QQ

A1A_1 A2A_2 \ldots ANA_N

l1l_1 r1r_1

l2l_2 r2r_2

\vdots

lQl_Q rQr_Q

Output

Print QQ lines. For q=1,2,,Qq = 1, 2, \ldots, Q, the qq-th line should contain the answer to the qq-th query.

10 4
2 7 1 8 2 8 1 8 2 8
1 10
1 9
2 10
5 5
5
2
4
0

For the first query, there are five triplets of integers that satisfy the conditions in the problem statement: (1,5,9),(4,6,8),(4,6,10),(4,8,10)(1, 5, 9), (4, 6, 8), (4, 6, 10), (4, 8, 10), and (6,8,10)(6, 8, 10).

20 10
2 2 2 2 1 1 2 2 1 1 1 2 1 2 1 2 2 1 2 1
12 16
17 18
12 18
4 9
13 13
2 5
6 13
2 14
7 14
8 12
1
0
5
2
0
1
11
55
8
1