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

#SRD0000073. Cắt và ghép (Cut and Stick)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Cắt và ghép (Cut and Stick)

Cut and Stick

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Partition each queried range [l,r][l,r] into the minimum number of subsequences. In any subsequence of length xx, no value may occur more than ⌈x/2⌉\lceil x/2\rceil times. Every element belongs to exactly one subsequence and order is preserved within each subsequence.

Input

Read n,qn,q, the array and qq inclusive 1-based ranges.

Output

Print the minimum number of subsequences for every query.

Subtasks

  • Subtask 1 (10 points): 1≤n≤501\le n\le 50, 1≤q≤601\le q\le 60. 1≤ai≤n1\le a_i\le n; 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≤n1\le a_i\le n; 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≤n1\le a_i\le n; 1≤l≤r≤n1\le l\le r\le n.
  • Subtask 4 (45 points): 1≤n≤3000001\le n\le 300000, 1≤q≤3000001\le q\le 300000. 1≤ai≤n1\le a_i\le n; 1≤l≤r≤n1\le l\le r\le n.

Examples

Input

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

Output

1
2

Explanation

The six-element range contains three occurrences of 3, meeting the limit of ⌈6/2⌉=3\lceil6/2\rceil=3: one piece is enough. For [3,2,3,3][3,2,3,3], three copies of 3 exceed the limit 2; two subsequences such as [3][3] and [2,3,3][2,3,3] suffice.