#Q125. 「一本通 4.2 练习 2」Balanced Lineup
「一本通 4.2 练习 2」Balanced Lineup
Description
Original problem from USACO 2007 Jan. Gold
FJ's cows always line up in the same sequence. One day, FJ decided to let some cows play a frisbee game. He plans to select a group of cows standing in a continuous segment of the line for the competition. However, to avoid significant differences in skill levels, the heights of the cows should not vary too much. FJ has prepared possible groups of cows and the heights of all cows. He wants to know the difference between the tallest and shortest cows in each group.
Input Format
The first line contains and ;
The second to the -th lines, the -th line contains the height of the -th cow;
The -th to the -th lines each contain two integers and , representing the range of cows from to .
Output Format
The first to the -th lines each contain an integer, representing the answer to the query (i.e., the difference between the tallest and shortest cows in the group).
Sample 1
6 3
1
7
3
4
2
5
1 5
4 6
2 2
6
3
0
Data Range and Hints
For all data, , , , .