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

#BS0000222. Truy vấn tần suất trên đoạn (Range Frequency Queries)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Truy vấn tần suất trên đoạn (Range Frequency Queries)

Range Frequency Queries

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

An immutable integer array a0,a1,…,an−1a_0,a_1,\ldots,a_{n-1} is indexed from zero. A query specifies l,r,xl,r,x. Count the indices ii between ll and rr, inclusive, satisfying ai=xa_i=x. Different positions containing the same value count separately.

Input

The first line gives integers n,qn,q, the array length and query count.

The second line contains the nn integers a0,a1,…,an−1a_0,a_1,\ldots,a_{n-1}.

Each of the next qq lines gives integers l,r,xl,r,x: the two inclusive endpoints and the value to count.

Output

Print qq lines, one count for each query in input order.

Subtasks

  • Subtask 1 (20 points): 1≤n,q≤1001\le n,q\le 100; 1≤ai,x≤1041\le a_i,x\le 10^4; 0≤i<n0\le i<n; 0≤l≤r<n0\le l\le r<n.
  • Subtask 2 (30 points): 1≤n,q≤1051\le n,q\le 10^5; 1≤ai≤31\le a_i\le 3; 1≤x≤1041\le x\le 10^4; 0≤i<n0\le i<n; 0≤l≤r<n0\le l\le r<n.
  • Subtask 3 (50 points): 1≤n,q≤1051\le n,q\le 10^5; 1≤ai,x≤1041\le a_i,x\le 10^4; 0≤i<n0\le i<n; 0≤l≤r<n0\le l\le r<n.

Examples

Input

6 4
2 5 2 2 7 5
0 5 2
1 3 2
4 4 2
0 5 9

Output

3
2
0
0

Explanation

Value 22 occurs at indices 0,2,30,2,3. The first interval contains all three; [1,3][1,3] contains indices 2,32,3 only. The singleton [4,4][4,4] contains 77, and 99 is absent from the array, giving zero for both remaining queries.