Volleyball
Time limit2sMemory limit1024 MB
Given n pole heights and m intervals, find for each interval the longest distance between two poles of equal height with no taller pole between them.
- Level
Medium7 of 10
- Topics
- Divide and conquer, Array, Stack
- Solved
- No attempts yet
Statement
During a PE class, Foma has to leave school on an urgent errand. So that the children do not wander around the school and reveal his absence, he gives them a task.
There are poles in a row, spaced 1 meter apart. The -th pole has height . A volleyball net can be strung between two poles if their heights are equal and no pole between them is taller. Each student is assigned an interval of poles and must find the longest net that can be strung within that interval. The length of a net is the distance between the two poles it connects.
The students know this will take them a long time, so they ask you for help.
Input
The first line contains two integers and (, ), the number of poles and the number of students. The second line contains positive integers (), the heights of the poles. Each of the next lines contains two integers and (), the interval assigned to one student.
Output
Print lines. The -th line contains the answer for the -th student.