Segment Tree Practice 4

View as PDF

Submit solution

Points: 17
Time limit: 0.6s
Memory limit: 256M

Author:
Problem type

Given an array A of size N, support the Q of the following queries:

Count the number of elements appearing exactly once in the range [l, r].

Constraints

1 \le N, Q \le 2 \times 10^5

1 \le l \le r \le N

1 \le A_i \le N

Input Specification

The first line contains 2 integers N and Q.

The second line contains N integers A_1, A_2, \ldots, A_N, the elements of A.

The next Q lines each contain 2 integers l_i and r_i, representing a query on the range [l_i, r_i].

Output Specification

For each query output one integer on its own line, the answer to that query.

Sample Input

7 5
1 2 3 2 4 3 1
2 5
1 7
4 7
3 6
1 1

Sample Output

2
1
4
2
1

Comments


  • 2
    thomas_li  commented on June 16, 2025, 12:25 p.m.

    Is it possible to solve this problem using bitset?


  • 1
    Tinyfold  commented on June 14, 2025, 11:16 p.m.

    Is it possible to solve this problem using segment tree?


    • 3
      bruce  commented on June 16, 2025, 2:18 a.m.

      Yes. You can answer all the queries offline with segment tree.