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

#FT0000130. Công chúa hái hoa (Flower Picking)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Công chúa hái hoa (Flower Picking)

Flower Picking

Source: Luogu

Version: Phuoc Hung OJ Extended

Problem Statement

A garden consists of nn flowers in one row. Flower ii has color aia_i in the range 1,…,c1,\ldots,c. There are mm independent queries, each selecting a contiguous interval [l,r][l,r].

For each query, count the number of distinct colors occurring at least twice among flowers in the interval. A color occurring once is not counted; a length-one query therefore has answer zero. Queries do not remove flowers or alter the array.

Input

  • First line: n,c,mn,c,m.
  • Second line: nn colors aia_i.
  • Next mm lines: one query l,rl,r each.

Output

Print mm lines in query order: the number of colors appearing at least twice.

Subtasks

  • Subtask 1 (20 points): 1≤n≤401\le n\le 40; 1≤c,m≤401\le c,m\le 40; 1≤ai≤c1\le a_i\le c; 1≤l≤r≤n1\le l\le r\le n, where 4040 is the maximum nn in this subtask.
  • Subtask 2 (30 points): 1≤n≤40001\le n\le 4000; 1≤c,m≤40001\le c,m\le 4000; 1≤ai≤c1\le a_i\le c; 1≤l≤r≤n1\le l\le r\le n, where 40004000 is the maximum nn in this subtask.
  • Subtask 3 (50 points): 1≤n≤20000001\le n\le 2000000; 1≤c,m≤20000001\le c,m\le 2000000; 1≤ai≤c1\le a_i\le c; 1≤l≤r≤n1\le l\le r\le n, where 20000002000000 is the maximum nn in this subtask.

Examples

Example 1

Input

5 3 5
1 2 2 3 1
1 5
1 2
2 2
2 3
3 5

Output

2
0
0
1
0

Explanation

In [1,5][1,5], colors 1 and 2 each occur twice, while color 3 occurs once, yielding 2. In [1,2][1,2], both colors occur once: 0. A length-one query yields 0. In [2,3][2,3], color 2 occurs twice: 1. In [3,5][3,5], each of colors 2, 3 and 1 occurs once: 0.

Example 2

Input

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

Output

2
1
1
0

Explanation

The sequence is 1 1 2 2 2 3. The full range has two colors occurring at least twice. Range [2,5][2,5] contains one color occurring at least twice (color 2), range [4,6][4,6] contains color 2 twice, and [5,5][5,5] contains no duplicated color. The answers are 2, 1, 1, 0.