AtCoder Regular Contest 159
E
Difference Sum Query
AtCoder
⌀ 3229
原题
← DF →
翻译中…

Problem Statement

翻译中…

You are given a positive integer $N$, and $M$ pairs of positive integers: $(a_0,b_0),\ldots,(a_{M-1},b_{M-1})$ (note that $a_i$ and $b_i$ start with index $0$).

翻译中…

We have the following sequence of non-negative integers $X=(x_1,\ldots,x_N)$.

翻译中…
  • $x_i$ is determined as follows.
  • Let $l=1$, $r=N$, and $t=0$.
    • Let $m=\left \lfloor \dfrac{a_{t \bmod M} \times l + b_{t \bmod M} \times r}{a_{t \bmod M} +b_{t \bmod M}} \right \rfloor$ ($\lfloor x \rfloor$ is the greatest integer not exceeding $x$). If $m=i$, let $x_i=t$ and terminate.
    • If $l \leq i \lt m$, let $r=m-1$; otherwise, let $l=m+1$. Increment $t$ by $1$ and return to step 2.
翻译中…

Find $\sum_{j=c_i}^{d_i-1} |x_j - x_{j+1}|$ for $i=1,2,\ldots,Q$. It can be proved that the answers are at most $10^{18}$ under the constraints of this problem.

翻译中…

Constraints

翻译中…
  • $2 \leq N \leq 10^{15}$
  • $1 \leq M \leq 100$
  • $1 \leq a_i,b_i \leq 1000$
  • $\max \left (\dfrac{a_i}{b_i},\dfrac{b_i}{a_i}\right ) \leq 2$
  • $1 \leq Q \leq 10^4$
  • $1 \leq c_i \lt d_i \leq N$
  • All values in the input are integers.
翻译中…

Input

翻译中…

The input is given from Standard Input in the following format:

翻译中…

N M
a_0 b_0
\vdots
a_{M-1} b_{M-1}
Q
c_1 d_1
\vdots
c_Q d_Q

翻译中…

Output

翻译中…

Print $Q$ lines. The $x$-th line should contain the answer to the question for $i=x$.

翻译中…

样例

Input #1
5 1
1 1
3
1 2
2 4
3 5
Output #1
1
3
2
We have $X=(1,2,0,1,2)$. For example, $x_1$ is determined as follows. - Let $l=1$, $r=5(=N)$, and $t=0$. - Let $m=3(=\left \lfloor \dfrac{1 \times 1 + 1 \times 5}{1+1} \right \rfloor)$. - Since $l \leq 1 \lt m$, let $r=2(=m-1)$. Increment $t$ by $1$ to $1$. - Let $m=1(= \left \lfloor \dfrac{1 \times 1 + 1 \times 2}{1+1} \right \rfloor )$. Since $m=1$, let $x_1=1(=t)$ and terminate. The answer to the question for $(c_1,d_1)$, for example, is $\sum_{j=c_i}^{d_i-1} |x_j - x_{j+1}| = |x_1-x_2| = 1$.
Input #2
1000000000000000 2
15 9
9 15
3
100 10000
5000 385723875
150 17095708
Output #2
19792
771437738
34191100
题目信息
题号
arc159_e
时间限制
4 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。