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

#SRD0000140. Tần suất giá trị trên đoạn tĩnh (Static Range Frequency)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tần suất giá trị trên đoạn tĩnh (Static Range Frequency)

Static Range Frequency

Source: Library Checker

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a sequence of NN nonnegative integers a0,a1,…,aN−1a_0,a_1,\ldots,a_{N-1}. The sequence never changes while processing queries.

There are QQ queries, each described by integers (l,r,x)(l,r,x). For each query, count the indices ii satisfying both l≤i<rl\le i<r and ai=xa_i=x.

The queried interval is half-open [l,r)[l,r): it includes index ll but excludes rr. If l=rl=r, the interval is empty and the answer is 00. Sequence indices are zero-based.

Answer all queries in the given order.

Input

  • The first line contains integers N,QN,Q.
  • The second line contains NN integers a0,a1,…,aN−1a_0,a_1,\ldots,a_{N-1}.
  • Each of the following QQ lines contains three integers lj,rj,xjl_j,r_j,x_j describing query jj.

Output

Print QQ lines. Line jj is the number of occurrences of value xjx_j among alj,alj+1,…,arj−1a_{l_j},a_{l_j+1},\ldots,a_{r_j-1}; print 0 for an empty interval.

Subtasks

  • Subtask 1 (20 points): 1≤N,Q≤1001\le N,Q\le100; 0≤ai,xj≤1090\le a_i,x_j\le10^9; 0≤lj≤rj≤N0\le l_j\le r_j\le N for every valid index.
  • Subtask 2 (30 points): 1≤N,Q≤2 0001\le N,Q\le2\,000; 0≤ai,xj≤1090\le a_i,x_j\le10^9; 0≤lj≤rj≤N0\le l_j\le r_j\le N for every valid index.
  • Subtask 3 (50 points): 1≤N,Q≤200 0001\le N,Q\le200\,000; 0≤ai,xj≤1090\le a_i,x_j\le10^9; 0≤lj≤rj≤N0\le l_j\le r_j\le N for every valid index.

Examples

Example 1

Input

5 5
3 7 3 2 3
0 5 3
1 4 3
0 0 3
2 5 7
0 5 2

Output

3
1
0
0
1

Explanation

The sequence is 3 7 3 2 3. In [0,5), value 3 occurs at indices 0,2,4: answer 3. In [1,4), only index 2 contains 3: answer 1. Interval [0,0) is empty: 0. There is no 7 in [2,5): 0. Finally, value 2 occurs only at index 3 of [0,5): 1.