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

#FT0000253. Tulip và các con số (Tulip And Numbers)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tulip và các con số (Tulip And Numbers)

Tulip And Numbers

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

Tulip writes nn positive integers in a row, never writing a number smaller than the preceding one. Therefore, the sequence is nondecreasing.

For each query i j, count the number of distinct values among positions ii through jj, inclusive. Repeated occurrences of the same value count once.

Input

  • The first line contains n,qn,q.
  • The second line contains nn nondecreasing positive integers a1,…,ana_1,\ldots,a_n.
  • The next qq lines each contain i,ji,j.

Output

Print qq lines, each giving the number of distinct values in the corresponding query interval.

Subtasks

  • Subtask 1 (20 points): 1≤n≤301\le n\le 30; 1≤q≤601\le q\le 60; 1≤a1≤a2≤⋯≤an≤1091\le a_1\le a_2\le\cdots\le a_n\le 10^9; 1≤i≤j≤n1\le i\le j\le n.

  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; 1≤q≤20001\le q\le 2000; 1≤a1≤a2≤⋯≤an≤1091\le a_1\le a_2\le\cdots\le a_n\le 10^9; 1≤i≤j≤n1\le i\le j\le n.

  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; 1≤q≤1000001\le q\le 100000; 1≤a1≤a2≤⋯≤an≤1091\le a_1\le a_2\le\cdots\le a_n\le 10^9; 1≤i≤j≤n1\le i\le j\le n.

Examples

Input

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

Output

2
4
2

Explanation

The interval [1,2][1,2] contains 11 and 22, so its answer is 22. The full interval [1,5][1,5] contains distinct values 1,2,4,51,2,4,5, giving 44. The interval [4,5][4,5] contains 4,54,5, giving 22. Each answer occupies one line without a case label.