Change the sequence

0

0 votes
Dynamic Programming, Hard, Sqrt-Decomposition, Algorithms, 3 Dimensional, 難しい
Problem

Let v be a sequence of length K. In one step, you can select an index i and change the value vi to either vi1 or vi+1. Let f(v1,v2,,vK) be the minimum number of steps required to satisfy v1v2vK.

You are given an array s of length N. You are required to answer Q independent queries of the type:  For given pair  (l,r), what is the value of f(sl,sl+1,,sr)?

Input format

  • First line: Two integers, N and Q (1N,Q105)
  • Second line: N integers - s1,s2,,sN (1si32)
  • Next Q lines: Two integers ai and bi (1ai,biN). The ith query will be equal to (li,ri)=(((ai+last1)mod N)+1,((bi+last1)modN)+1), where last is the answer for the last query. For the first query, consider (l1,r1)=(a1,b1). The condition 1liriN is satisfied for all i with 1iQ.

Output format

Print Q lines where the ith line represents the answer for the ith query.

Additional information

For 20 points: N,Q512 is satisfied

For additional 30 points: N216,Q214 are satisfied

Original constraints for remaining points

Time Limit: 6
Memory Limit: 1568
Source Limit:
Explanation

The queries are (1,5),(1,3),(2,5).

In the first query, the optimal cost is achieved by obtaining sequence (3,3,3,3,3) or (2,2,3,3,3) etc.

In the second query, the optimal cost is achieved by obtaining sequence (4,4,4).

In the third query, the optimal cost is achieved by obtaining sequence (1,3,3,3)

Editor Image

?