QOJ.ac

QOJ

时间限制: 2 s 内存限制: 1024 MB 总分: 100 可 Hack ✓

#18299. Merging Branches

统计

Moloco is an ad-tech company in KAIST City which lies along a straight coordinate line. There are $n$ branches belonging to Moloco where the sales territory of the $i$-th branch is in the range $[\ell_i, r_i]$ meters. Initially, $\ell_i < r_i$ for $1 \leq i \leq n$ and $r_i \leq \ell_{i + 1}$ for $1 \leq i \leq n - 1$.

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

Moloco can widen all its branches' territories through efficient advertisement. If Moloco spends $k$ won, then every branch may widen its sales territory by at most $k$ meters. The start and end of the widened sales territory should have an integer value.

Formally, let the new sales territory of the $i$-th branch be $[\ell_i', r_i']$. Then the conditions below should be satisfied:

  • $l_i' \leq l_i$ and $r_i \leq r_i'$.
  • $(l_i - l_i') + (r_i' - r_i) \leq k$.
  • $l_i'$ and $r_i'$ are both integers.

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

You are employed by Moloco, and you are curious about the hypothetical scenarios when Moloco only owns branches from the $s$-th to the $e$-th. For each of these scenarios, you want to find the minimum cost to merge the branches owned by Moloco. Merging happens after spending money (possibly zero) on advertisement and widening all the branches.

Given $q$ queries where Moloco only owns the branches from the $s$-th to the $e$-th branch, find the minimum cost to merge those branches into one. Note that there is no cost when there is only one branch.

Input

The first line contains two integers: n and q (1 \leq n \leq 5000, 1 \leq q \leq 10^6).

The next $n$ lines describe the branches. The $i$-th of these lines contains two integers, \ell_i and r_i, denoting the sales territory of the $i$-th branch (1 \leq \ell_i < r_i \leq 10^9, r_i \leq \ell_{i + 1}).

The next $q$ lines describe the queries. The $i$-th of these lines contains two integers, s_i and e_i, denoting the $i$-th query (1 \leq s_i \leq e_i \leq n).

Output

Print q lines. On the i-th line, print the answer to the i-th query: the minimum cost to merge all the branches from the s_i-th to the e_i-th.

Examples

Input 1

5 2
1 3
5 6
10 15
20 24
28 33
1 5
3 5

Output 1

4
3

Input 2

7 7
1 3
6 10
14 18
18 19
22 24
28 29
32 40
1 7
3 5
2 6
1 2
4 4
4 7
3 4

Output 2

3
2
3
2
0
3
0

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.