Balanced Lineup
Time limit1sMemory limit128 MB
Given a static array of cow heights, answer queries giving the max minus min over each range [A, B].
- Level
Medium5 of 10
- Topics
- Segment tree, Array
- Solved
- No attempts yet
Problem
For the daily milking, Farmer John's cows () always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he takes a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun, they should not differ too much in height.
Farmer John has made a list of () potential groups of cows and their heights (). For each group, help him determine the difference in height between the shortest and the tallest cow in the group.
Input
- Line 1: Two space-separated integers, and .
- Lines 2 to : Line contains a single integer, the height of cow .
- Lines to : Two integers and (), representing the range of cows from to inclusive.
Output
- Lines 1 to : Each line contains a single integer, the difference in height between the tallest and the shortest cow in the corresponding range.