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

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

1 lộ trình

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 nn shells arranged in order. The shell at position ii has type identifier aia_i. Shells with equal identifiers represent the same type.

For each of mm queries, HH selects the consecutive positions ll through rr, 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: 22, 55 and 88. No shell is moved or changed between queries. Print the answer for each query in its original order.

Input

  • The first line contains nn.
  • The next line contains the nn array elements.
  • The following line contains the number of queries, mm.
  • Each following query line contains two integers l,rl,r.

Output

Print one answer per query in the same order as the input.

Subtasks

  • Subtask 1 (20 points): 1≤n≤50001\le n\le 5000; 1≤m≤50001\le m\le 5000; 1≤ai≤10000001\le a_i\le 1000000; 1≤l≤r≤n1\le l\le r\le n.
  • Subtask 2 (20 points): 1≤n≤1000001\le n\le 100000; 1≤m≤1000001\le m\le 100000; 1≤ai≤10000001\le a_i\le 1000000; 1≤l≤r≤n1\le l\le r\le n.
  • Subtask 3 (20 points): 1≤n≤5000001\le n\le 500000; 1≤m≤5000001\le m\le 500000; 1≤ai≤10000001\le a_i\le 1000000; 1≤l≤r≤n1\le l\le r\le n.
  • Subtask 4 (40 points): 1≤n≤10000001\le n\le 1000000; 1≤m≤10000001\le m\le 1000000; 1≤ai≤10000001\le a_i\le 1000000; 1≤l≤r≤n1\le l\le r\le n.

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 output 4.
  • Query 2 [2,5]: values in order are [2, 1, 3, 2]. Removing repetitions leaves [1, 2, 3], giving 3 distinct values and output 3.
  • Query 3 [3,3]: values in order are [1]. Removing repetitions leaves [1], giving 1 distinct values and output 1.
  • Query 4 [4,7]: values in order are [3, 2, 4, 1]. Removing repetitions leaves [1, 2, 3, 4], giving 4 distinct values and output 4.
  • Query 5 [5,7]: values in order are [2, 4, 1]. Removing repetitions leaves [1, 2, 4], giving 3 distinct values and output 3.

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 output 1.
  • Query 2 [3,6]: values in order are [5, 1, 1, 2]. Removing repetitions leaves [1, 2, 5], giving 3 distinct values and output 3.
  • 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 output 3.
  • Query 4 [4,5]: values in order are [1, 1]. Removing repetitions leaves [1], giving 1 distinct values and output 1.