#P7416. [USACO21FEB] No Time to Dry P

[USACO21FEB] No Time to Dry P

Description

Bessie recently received a set of paints, and she wants to paint the fence at one end of her pasture. The fence consists of NN small segments, each 11 meter long (1N21051\le N\le 2\cdot 10^5). Bessie can use NN different colors, labeled from 11 to NN from light to dark (11 is very light, and NN is very dark). Thus, she can describe the desired color for each fence segment with an integer array of length NN.

At the beginning, all fence segments are unpainted. In one stroke, Bessie can paint any number of consecutive segments with the same color, as long as she does not paint a lighter color over a darker color (she may only use darker colors to cover lighter colors).

For example, an unpainted fence of length 44 can be painted as follows:

0000 -> 1110 -> 1122 -> 1332

Unfortunately, Bessie does not have enough time to wait for the paint to dry. Therefore, she thinks she may need to give up painting some segments of the fence. Now, she is considering QQ candidate intervals (1Q21051\le Q\le 2\cdot 10^5). Each interval is given by two integers (a,b)(a,b) satisfying 1abN1 \leq a \leq b \leq N, representing the endpoints of the segments aba \ldots b that need to be painted.

For each candidate interval, if all fence segments within the interval are painted to the desired colors and all segments outside the interval are left unpainted, what is the minimum number of strokes needed? Note that Bessie does not actually paint anything during this process, so the answer for each candidate interval is independent.

Input Format

The first line contains NN and QQ.

The next line contains an integer array of length NN, representing the desired color of each fence segment.

The following QQ lines each contain two space-separated integers aa and bb, representing a candidate interval to be painted.

Output Format

For each of the QQ candidate intervals, output one line containing the answer.

8 4
1 2 2 1 1 2 3 2
4 6
3 6
1 6
5 8
2
3
3
3

Hint

Sample 1 Explanation

In this sample, painting the subarray with colors 1 1 2 requires 22 strokes.
Painting the subarray with colors 2 1 1 2 requires 33 strokes.
Painting the subarray with colors 1 2 2 1 1 2 requires 33 strokes.
Painting the subarray with colors 1 2 3 2 requires 33 strokes.

Subtask Properties

  • For 10%10\% of the testdata, N,Q100N,Q\le 100.
  • For another 15%15\% of the testdata, N,Q5000N,Q\le 5000.
  • For another 25%25\% of the testdata, the input array does not contain numbers greater than 1010.
  • For the remaining 50%50\% of the testdata, there are no additional constraints.

Problem by: Andi Qu, Brian Dean, Benjamin Qi.

Translated by ChatGPT 5