PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000063. Đếm bộ ba chỉ số (Triple Index)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đếm bộ ba chỉ số (Triple Index)

Triple Index

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

Given a1,…,ana_1,\ldots,a_n, for each range [l,r][l,r] count index triplets (i,j,k)(i,j,k) satisfying $$l\le i<j<k\le r,\qquad a_i=a_j=a_k.$$ Different index triplets are counted separately, even if they share the same value.

Array positions are numbered from 11 to nn. Every query uses an inclusive interval [l,r][l,r] containing both endpoints. Queries do not modify the original array and are independent of one another.

Input

First line contains n,qn,q. The next line contains nn values aia_i. There follow qq lines l,rl,r.

Output

Print one integer per query line, preserving the original query order.

Subtasks

  • Subtask 1 (10 points): 1≤n≤901\le n\le 90; 1≤q≤901\le q\le 90; 1≤ai≤200 0001\le a_i\le 200\,000; 1≤l≤r≤n1\le l\le r\le n.
  • Subtask 2 (20 points): 1≤n≤2 0001\le n\le 2\,000; 1≤q≤4 0001\le q\le 4\,000; 1≤ai≤200 0001\le a_i\le 200\,000; 1≤l≤r≤n1\le l\le r\le n.
  • Subtask 3 (25 points): 1≤n≤35 0001\le n\le 35\,000; 1≤q≤40 0001\le q\le 40\,000; 1≤ai≤200 0001\le a_i\le 200\,000; 1≤l≤r≤n1\le l\le r\le n.
  • Subtask 4 (45 points): 1≤n≤200 0001\le n\le 200\,000; 1≤q≤200 0001\le q\le 200\,000; 1≤ai≤200 0001\le a_i\le 200\,000; 1≤l≤r≤n1\le l\le r\le n.

Examples

Input

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

Output

5
2
4
0

Explanation

For [1,10][1,10], the five triplets are (1,5,9)(1,5,9), (4,6,8)(4,6,8), (4,6,10)(4,6,10), (4,8,10)(4,8,10), and (6,8,10)(6,8,10). The singleton range [5,5][5,5] has no triplet. In [1,9][1,9], values 22 and 88 each appear three times, giving 1+1=21+1=2 triplets. In [2,10][2,10], value 88 occurs four times, contributing (43)=4\binom 43=4 triplets; no other value occurs three times.