Magical Creatures
InterviewTime limit2sMemory limit1024 MB
Count how many terms of the sequence t, t+s, t+2s, ... fall inside each interval [a_i, b_i], then print the total over all intervals.
- Level
Easy3 of 10
- Topics
- Math, Implementation
- Solved
- No attempts yet
Statement
The famous British magizoologist Newt Scamander owns a magical suitcase. Newt cast an Undetectable Extension Charm on it and kept inside a large collection of rare, endangered magical creatures he found during his trips around the world. One day Newt left the suitcase half open, and the creatures decided to escape. They cannot all leave at once, because the opening of the suitcase is very narrow. Each pet needs time units to climb out. The first pet leaves at time , and the following pets leave at times , , and so on. Newt learned about the creatures' clever plan too late. Now he wants to know the total number of creatures that escaped during the time intervals , endpoints included. The suitcase holds an infinite number of pets. The answer for each interval is computed independently of the others.
Input
The first line contains the integers and , the moment when the first pet left the suitcase and the time between consecutive escapes (, ). The second line contains , the number of time intervals Newt is interested in (). Each of the next lines contains and , the left and right bounds of the -th interval ().
Output
Print a single number, the total number of creatures that escaped during the given intervals. The answer for each interval is computed independently of the others.
Hint
In the first test, the creatures escape at times 7, 10, and 13, all of which belong to the second interval. No creature escapes during the first interval.