QOJ.ac

QOJ

実行時間制限: 2 s メモリ制限: 1024 MB 満点: 100 ハック可能 ✓

#17967. Beam

統計

You are running an interval storage service. The storehouse currently contains $N$ intervals, where the $i$-th interval is represented as $[l_i, r_i]$ on the number line.

$Q$ upcoming laser strikes on the storehouse have been announced! The damage range of the $j$-th laser can be represented as the interval $[s_j, e_j]$. Each time a laser strike occurs, your job is to move the stored intervals such that no interval is hit by the laser.

Specifically, for each stored interval $[l_i, r_i]$, a suitable integer $x_{ij}$ is selected. Then, for all $(i, j)$, the length of the intersection between $[l_i+x_{ij}, r_i+x_{ij}]$ and $[s_j, e_j]$ must be $0$. Here, the length of the intersection between two intervals $[a, b]$ and $[c, d]$ is defined as $\max(0, \min(b, d) - \max(a, c))$.

Intervals are heavy and require special machines when moving them. Moving an interval costs an amount of (Distance moved) $\times$ (Length of interval) in electricity bills. Thus, the fee to be paid before the $j$-th laser beam strikes is $\sum_{i=1}^{N} (r_i-l_i)|x_{ij}|$.

Note that all intervals are moved back to their initial position after each laser strike, and this also costs electricity.

Your task is to pay the minimum amount in electricity bills while keeping all stored intervals safe. Calculate the cost required to do so.

Input

The first line of input contains $N$, denoting the number of intervals, and $Q$, denoting the number of laser strikes. ($1 \le N, Q \le 250\,000$)

The $i$-th of the following $N$ lines contains $l_i$ and $r_i$, denoting the endpoints of the $i$-th interval. ($1 \le l_i < r_i \le 1\,000\,000$)

Then, the $j$-th of the following $Q$ lines contains $s_j$ and $e_j$, denoting the endpoints of the range damaged by the $j$-th laser strike. ($1 \le s_j < e_j \le 1\,000\,000$)

All input values are integers.

Output

Print $Q$ lines in total: the minimum amount to pay in electricity bills when moving all intervals to a safe position and back to the original state, for each laser strike in a single line.

Examples

Input 1

2 2
1 5
4 8
3 5
8 9

Output 1

24
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.