PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#FT0000111. Chiếc vòng cổ của HH (HH's Necklace)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Chiếc vòng cổ của HH (HH's Necklace)
HH's Necklace
Source: Luogu
Version: Phuoc Hung OJ Extended
Problem Statement
HH has a necklace consisting of shells arranged in order. The shell at position has type identifier . Shells with equal identifiers represent the same type.
For each of queries, HH selects the consecutive positions through , including both endpoints, and wants the number of different shell types in that segment. For example, (5,2,5,2,8) consists of three types: , and . No shell is moved or changed between queries. Print the answer for each query in its original order.
Input
- The first line contains .
- The next line contains the array elements.
- The following line contains the number of queries, .
- Each following query line contains two integers .
Output
Print one answer per query in the same order as the input.
Subtasks
- Subtask 1 (20 points): ; ; ; .
- Subtask 2 (20 points): ; ; ; .
- Subtask 3 (20 points): ; ; ; .
- Subtask 4 (40 points): ; ; ; .
Examples
Example 1
Input
7
1 2 1 3 2 4 1
5
1 7
2 5
3 3
4 7
5 7
Output
4
3
1
4
3
Explanation
- Query 1
[1,7]: values in order are[1, 2, 1, 3, 2, 4, 1]. Removing repetitions leaves[1, 2, 3, 4], giving 4 distinct values and output4. - Query 2
[2,5]: values in order are[2, 1, 3, 2]. Removing repetitions leaves[1, 2, 3], giving 3 distinct values and output3. - Query 3
[3,3]: values in order are[1]. Removing repetitions leaves[1], giving 1 distinct values and output1. - Query 4
[4,7]: values in order are[3, 2, 4, 1]. Removing repetitions leaves[1, 2, 3, 4], giving 4 distinct values and output4. - Query 5
[5,7]: values in order are[2, 4, 1]. Removing repetitions leaves[1, 2, 4], giving 3 distinct values and output3.
Example 2
Input
6
5 5 5 1 1 2
4
1 3
3 6
1 6
4 5
Output
1
3
3
1
Explanation
- Query 1
[1,3]: values in order are[5, 5, 5]. Removing repetitions leaves[5], giving 1 distinct values and output1. - Query 2
[3,6]: values in order are[5, 1, 1, 2]. Removing repetitions leaves[1, 2, 5], giving 3 distinct values and output3. - Query 3
[1,6]: values in order are[5, 5, 5, 1, 1, 2]. Removing repetitions leaves[1, 2, 5], giving 3 distinct values and output3. - Query 4
[4,5]: values in order are[1, 1]. Removing repetitions leaves[1], giving 1 distinct values and output1.