QOJ.ac

QOJ

実行時間制限: 2 s メモリ制限: 512 MB 満点: 100

#8672. Queueing

統計

The bathhouse at Peking University requires queuing, and each person $i$ has a preference range $[l_i, r_i]$.

If person $i$ arrives at the bathhouse and finds that there are $> r_i$ people in the queue, they will leave immediately (because the queue is too long). If there are $[0, l_i)$ people in the queue, they will also leave (because the queue is too short, making it boring to wait without anyone to chat with, so they prefer to try their luck at a bathhouse on another floor). Therefore, person $i$ will choose to join the queue if and only if the current number of people in the queue is in the range $[l_i, r_i]$.

There are $n$ people and $q$ queries. For each query $j$, you are given $L_j$ and $R_j$. Assuming the bathhouse is currently full (so everyone who arrives must join the queue), determine how many people will be in the queue after people with indices $L_j, L_j+1, \dots, R_j$ arrive one by one. (Assume that during this process, no one finishes their bath and leaves.)

Formally, given $l_i, r_i$, define the piecewise function $f_i(x) = x + [x \in [l_i, r_i]]$. Answer multiple queries for $f_R(f_{R-1}(\dots f_L(0)\dots))$.

Input

The first line contains two positive integers $n$ and $q$.

The next $n$ lines each contain two integers representing $l_i$ and $r_i$.

The next $q$ lines each contain two integers $L_j$ and $R_j$ representing the query.

Output

Output $q$ lines, each containing an integer representing the answer to the query.

Examples

Input 1

3 3
0 0
1 2
0 2
1 1
1 3
2 3

Output 1

1
3
1

Constraints

For all data, $1 \le n, q \le 10^6$, $0 \le l_i \le r_i \le n$, $1 \le L_j \le R_j \le n$.

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.