Merging Branches

아직 제출이 없습니다시간 제한2초메모리 제한1024 MB

문제

Moloco is an ad-tech company in KAIST city which can be described as a 1-dimensional line. There are NN branches in Moloco, and the sales territory of the iith branch is range \[l_i,r_i]\[l\_i, r\_i]. Also it is known that l_i<r_il\_i < r\_i for 1iN1 \leq i \leq N and r_il_i+1r\_i \leq l\_{i+1} for 1iN11 \leq i \leq N-1.

Moloco defines two branches to intersect if there exists a point which is included in both branches' sales territory. For example \[3,5]\[3, 5] and \[5,7]\[5, 7] intersect, \[1,4]\[1, 4] and \[2,5]\[2, 5] intersect while \[1,2]\[1, 2] and \[3,5]\[3, 5] does not.

Moloco can widen the whole branch's sales territory by efficient advertisement. If Moloco spends KK won, then every branch may widen its sales territory by at most KK length units. Start and end of widened sales territory should have integer value.

Formally, let the new sales territory of iith branch be \[l_i,r_i]\[l\_i', r\_i']. Below conditions should satisfy.

  • l_il_il\_i' \leq l\_i and r_ir_ir\_i \leq r\_i'
  • (l_il_i)+(r_ir_i)K(l\_i-l\_i')+(r\_i'-r\_i) \leq K
  • l_il\_i' and r_ir\_i' are both integers.

Since too many branches cause difficulty in company management, Moloco is trying to merge all branches into one. Two branch can be merged if they intersect. The sales territory of the new branch will be the union of sales territories of two branches.

Fig 1. Illustration of sample 1 when K=4K=4

You are employed to Moloco. You are curious of hypothetical scenarios when Moloco only owns ssth to eeth branches. For each of these scenarios, you want to find the minimum cost to merge the branches owned by Moloco. Given QQ queries where Moloco only owns ssth to eeth branches, solve the minimum cost to merge all branches into one.

입력

First line contains two integers NN, QQ (1N5000,1Q1061 \leq N \leq 5000, 1 \leq Q \leq 10^6).

NN lines follow. ii-th line contains two integers l_il\_i, r_ir\_i (1l_i<r_i1091 \leq l\_i < r\_i \leq 10^9), denoting the sales territory of iith branch. Additionaly, r_il_i+1r\_i \leq l\_{i+1} for all 1iN11 \leq i \leq N-1.

QQ lines follow. ii-th line contains two integers s_is\_i, e_ie\_i (1s_ie_iN1 \leq s\_i \leq e\_i \leq N), denoting the iith query.

출력

Print QQ lines. On the iith line, print the answer to the iith query, the minimum cost to merge all branches from s_is\_i-th to e_ie\_i-th.

힌트

Notice that you don't need any cost if there is only one branch.