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

#SRD0000058. Truy vấn số giá trị phân biệt (D-query)

HỌC THEO LỘ TRÌNH

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

3 lộ trình

Truy vấn số giá trị phân biệt (D-query)

D-query

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

Given an array of positive integers, determine the number of distinct values occurring in each inclusive subarray [l,r][l,r].

Array positions are numbered from 11 to nn. Every query uses an inclusive interval [l,r][l,r] containing both endpoints. Queries do not modify the original array and are independent of one another.

Input

First line contains nn. The next line contains nn values aia_i. The following line contains qq. There follow qq lines l,rl,r.

Output

Print one integer per query line, preserving the original query order.

Subtasks

  • Subtask 1 (10 points): 1≤n≤601\le n\le 60; 1≤q≤901\le q\le 90; 1≤ai≤1 000 0001\le a_i\le 1\,000\,000; 1≤l≤r≤n1\le l\le r\le n.
  • Subtask 2 (20 points): 1≤n≤2 0001\le n\le 2\,000; 1≤q≤5 0001\le q\le 5\,000; 1≤ai≤1 000 0001\le a_i\le 1\,000\,000; 1≤l≤r≤n1\le l\le r\le n.
  • Subtask 3 (30 points): 1≤n≤10 0001\le n\le 10\,000; 1≤q≤40 0001\le q\le 40\,000; 1≤ai≤1 000 0001\le a_i\le 1\,000\,000; 1≤l≤r≤n1\le l\le r\le n.
  • Subtask 4 (40 points): 1≤n≤30 0001\le n\le 30\,000; 1≤q≤200 0001\le q\le 200\,000; 1≤ai≤1 000 0001\le a_i\le 1\,000\,000; 1≤l≤r≤n1\le l\le r\le n.

Examples

Input

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

Output

3
2
3

Explanation

Range [1,5][1,5] contains 1,2,31,2,3 (three distinct values); [2,4][2,4] has 1,21,2 (two); and [3,5][3,5] has 2,1,32,1,3 (three).