All problems

Problem 690

Range Frequency Queries

Medium · 20 pts · Array, Hash Table, Binary Search

Problem

You are given an array of N integers (positions 1 to N) and Q queries (l, r, v). For each query, count how many times the value v appears among A[l], ..., A[r]. For every value, store the sorted list of positions where it appears. Then the answer to a query is the number of positions in v's list between l and r, which two binary searches find in O(log N).

Input

The first line contains N and Q. The second line contains the N values. Each of the next Q lines contains l, r and v.

Output

Print the answer to each query, one per line.

Constraints

1 <= N, Q <= 100000; 1 <= A[i], v <= 10000; 1 <= l <= r <= N

Example input

12 2
12 33 4 56 22 2 34 33 22 12 34 56
2 4 4
1 12 33

Example output

1
2

Explanation

For A = 12 33 4 56 22 2 34 33 22 12 34 56, the value 4 appears once among positions 2 to 4. The value 33 appears twice in the whole array (positions 2 and 8).

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Rectangle Area II Next: My Calendar I