loj#P3481. 「USACO 2021.2 Platinum」No Time to Dry
「USACO 2021.2 Platinum」No Time to Dry
Description
Bessie has recently received a painting set, and she wants to paint the long fence at one end of her pasture. The fence consists of consecutive 1-meter segments (). Bessie has different colors available, which she labels with the letters through in increasing order of darkness ( is a very light color, and is very dark). She can therefore describe the desired color she wants to paint each fence segment as an array of integers.
Initially, all fence segments are uncolored. Bessie can color any contiguous range of segments with a single color in a single brush stroke as long as she never paints a lighter color over a darker color (she can only paint darker colors over lighter colors).
For example, an initially uncolored segment of length four can be colored as follows:
0000 -> 1110 -> 1122 -> 1332
Unfortunately, Bessie doesn't have time to waste watching paint dry. Thus, Bessie thinks she may need to leave some fence segments unpainted! Currently, she is considering candidate ranges (), each described by two integers with giving the indices of endpoints of the range of segments to be painted.
For each candidate range, what is the minimum number of strokes needed to paint every fence segment inside the range with its desired color while leaving all fence segments outside the range uncolored? Note that Bessie does not actually do any painting during this process, so the answers for each candidate range are independent.
Input Format
The first line contains and .
The next line contains an array of integers representing the desired color for each fence segment.
The next lines each contain two space-separated integers and representing a candidate range to possibly paint.
Output Format
For each of the candidates, output the answer on a new line.
8 4
1 2 2 1 1 2 3 2
4 6
3 6
1 6
5 8
2
3
3
3
Scoring
- Test cases satisfy .
- Test cases satisfy .
- In test cases , the input array contains no integer greater than .
- Test cases satisfy no additional constraints.