#P14479. 生成序列

    ID: 13433 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划 DP贪心洛谷原创O2优化洛谷月赛Ad-hoc

生成序列

Description

Hint: Please note the constraints on nn and qq in this problem.

Let len(a)\text{len}(a) denote the length of sequence aa, and a[l,r]a_{[l,r]} denote the subsequence of aa from index ll to rr inclusive.

Initially, Little L has an empty sequence bb. Little L can perform the following operations in order several times to eventually transform bb into the target sequence aa:

  • Generate a sequence cc such that len(b)len(c)\text{len}(b)\leq\text{len}(c).
  • If there exists 1ilen(b)1\leq i\leq\text{len}(b) such that bi=cib_i=c_i, then gain 11 point; otherwise, gain 00 points.
  • Append sequence cc to the end of sequence bb.

Little L wants to know, under the goal of achieving the target sequence, what is the maximum number of points that can be obtained. Let f(a)f(a) be the answer to this problem when aa is the target sequence.

To prevent you from cheating Little L, Little L will first give you an array aa and then perform qq queries. Each query gives two integers l,rl, r, and you need to answer the value of f(a[l,r])f(a_{[l,r]}).

Input Format

The first line contains two integers n,qn, q, representing the length of sequence aa and the number of queries, respectively.

The second line contains nn non-negative integers aia_i.

Then, qq lines follow, each with two integers l,rl, r, representing the query interval.

Output Format

Output qq lines, each containing an integer, representing the value of f(a[l,r])f(a_{[l,r]}) for each query.

9 2
3 3 3 1 0 2 0 1 0
1 9
2 8
3
1

Hint

【Sample Explanation】

For the first query, the sequence is a[1,9]={3,3,3,1,0,2,0,1,0}a_{[1,9]}=\{3,3,3,1,0,2,0,1,0\}. One possible way to achieve the maximum points is as follows:

  • First operation: let c={3}c=\{3\}, gain 00 points, now b={3}b=\{3\}.
  • Second operation: let c={3}c=\{3\}, gain 11 point, now b={3,3}b=\{3,3\}.
  • Third operation: let c={3,1}c=\{3,1\}, gain 11 point, now b={3,3,3,1}b=\{3,3,3,1\}.
  • Fourth operation: let c={0,2,0,1,0}c=\{0,2,0,1,0\}, gain 11 point, now b={3,3,3,1,0,2,0,1,0}b=\{3,3,3,1,0,2,0,1,0\}.

The total points gained is 33. It can be proven that no scheme can gain more than 33 points.

【Data Range】

This problem uses bundled tests.

Subtask nn qq Special Constraints Score
11 20\leq20 10\leq10 None 88
22 300\leq300 99
33 5×103\leq5\times10^3 1212
44 105\leq10^5 A 1010
55 B 1313
66 None 1111
77 106\leq10^6
88 50\leq50 2626
  • Special Constraint A: ai50a_i\leq50.
  • Special Constraint B: Each number in sequence aa appears at most 5050 times.

For all data, 1n1061\leq n\leq10^6, 1q501\leq q\leq50, 1lrn1\leq l\leq r\leq n, 1ain1\leq a_i\leq n.