Under construction

Still being built. Things may change

Magical Creatures

Interview

Time limit2sMemory limit1024 MB

Summary
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 ss time units to climb out. The first pet leaves at time tt, and the following pets leave at times t+st + s, t+2⋅st + 2 \cdot s, 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 [ai,bi][a_i, b_i], 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 tt and ss, the moment when the first pet left the suitcase and the time between consecutive escapes (0≤t≤10120 \le t \le 10^{12}, 1≤s≤10121 \le s \le 10^{12}). The second line contains nn, the number of time intervals Newt is interested in (1≤n≤1000001 \le n \le 100000). Each of the next nn lines contains aia_i and bib_i, the left and right bounds of the ii-th interval (0≤ai,bi≤10120 \le a_i, b_i \le 10^{12}).

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.

Examples2

  1. Example 1

    Input
    4 3
    2
    1 3
    5 13
    
    Expected output
    3
    
  2. Example 2

    Input
    1 10
    3
    1 1
    1 1
    1 1
    
    Expected output
    3