Balanced Lineup
Time limit1sMemory limit128 MB
Given N cow heights and Q ranges, report the difference between the maximum and minimum height within each query range.
- Level
Medium6 of 10
- Topics
- Segment tree, Array, Implementation, Prefix sum
- 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 lineup to play. However, for all the cows to have fun, their heights should not differ too much.
Farmer John prepares candidate groups () along with the cows' heights (). For each group, determine the difference in height between the shortest and the tallest cow in that group.
Note: on the largest test case, I/O takes up the majority of the runtime.
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: Each line contains a single integer, the answer to one query — the difference in height between the tallest and the shortest cow in the given range.