Road Construction
Time limit1.5sMemory limit256 MB
For each query range of the road, find the minimum number of K-length spells that make all segment heights equal, or -1 if that is impossible.
- Level
Medium7 of 10
- Topics
- Array, Prefix sum, Math
- Solved
- No attempts yet
Problem
SciOI City's mayor, Sangryeol, wants to build a new road. The ground where he plans to build it is too bumpy, and compacting the ground alone is expected to take years. The land is a straight strip split into segments, whose heights may be equal or different. Heights can be negative. The state of the road is given by a sequence of length , where is the height of the -th segment.
Fortunately, Sangryeol is a wizard who can cast a spell that raises or lowers the heights of consecutive segments by the same amount. is fixed.
For example, let , , and the road be as follows.
3 5 6 2 9
If Sangryeol casts a spell that raises the third and fourth segments by 2, the road changes as follows.
3 5 8 4 9
Sangryeol's spells can also reach beyond the ends of the road, and any part of a spell outside the road is ignored. For example, when , a spell can be cast on the 0th, 1st, and 2nd segments. The spell on the 0th segment is ignored, so only the 1st and 2nd segments are affected.
Input
The first line contains , , and the number of candidate segments . (, )
The second line contains the sequence of length that describes the road. ()
Each of the next lines contains a candidate segment. The -th candidate is given by two integers and , meaning it covers the segments from the -th to the -th of the road. ()
Output
For each candidate segment, print on its own line the minimum number of spells Sangryeol must cast. If the heights of the road segments cannot be made equal with spells, print .