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

#SRD0000075. Jeff và việc xóa các chu kỳ (Jeff and Removing Periods)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Jeff và việc xóa các chu kỳ (Jeff and Removing Periods)

Jeff and Removing Periods

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

One operation chooses equal-valued elements whose positions form an arithmetic progression in the current sequence, deletes them, then arbitrarily permutes the remaining elements. For every independent interval [l,r][l,r] of the original array, output the minimum operations needed to delete all its elements.

Input

Read nn, the sequence, qq, then qq inclusive ranges.

Output

Print the minimum number of operations per query.

Subtasks

  • Subtask 1 (10 points): 1≤n≤401\le n\le 40, 1≤q≤601\le q\le 60. 1≤ai≤1000001\le a_i\le100000; 1≤l≤r≤n1\le l\le r\le n.
  • Subtask 2 (20 points): 1≤n≤40001\le n\le 4000, 1≤q≤40001\le q\le 4000. 1≤ai≤1000001\le a_i\le100000; 1≤l≤r≤n1\le l\le r\le n.
  • Subtask 3 (25 points): 1≤n≤300001\le n\le 30000, 1≤q≤300001\le q\le 30000. 1≤ai≤1000001\le a_i\le100000; 1≤l≤r≤n1\le l\le r\le n.
  • Subtask 4 (45 points): 1≤n≤1000001\le n\le 100000, 1≤q≤1000001\le q\le 100000. 1≤ai≤1000001\le a_i\le100000; 1≤l≤r≤n1\le l\le r\le n.

Examples

Input

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

Output

2
1
1
2
2

Explanation

In [1,5][1,5], the positions of value 1 are 3 and 4, an arithmetic progression; remove them first and then remove all copies of 2, using two operations. Single-element intervals take one operation. Range [1,3]=[2,2,1][1,3]=[2,2,1] has two distinct values, so two operations are needed.