QOJ.ac

QOJ

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

#16464. Circular Ring

統計

Yuki is a robot who loves playing games!

The rules of Yuki's favorite game are as follows:

  • The game takes place on a circle of length $n$, where position $i$ is adjacent to position $(i \bmod n)+1$. Initially, Yuki's two hands are at position $1$ and position $n$, respectively. During the game, Yuki can spend one unit of stamina to move one of her hands to an adjacent position.
  • There are $m$ notes that appear on this circle. At time $x_i$, a note appears at position $y_i$. At this moment, Yuki must have at least one hand at position $y_i$; otherwise, Yuki loses the game. If Yuki fulfills all requirements, she completes the game.
  • It is guaranteed that no notes overlap; that is, for every pair of positive integers $i, j$ such that $1 \le i < j \le m$, it is guaranteed that $x_i \ne x_j$ or $y_i \ne y_j$.
  • It is guaranteed that there are at most two notes at any given time; that is, for every positive integer $t$, it is guaranteed that there are no more than two positive integers $i$ such that $x_i = t$.

Since Yuki is a robot, she has an advantage in this game—her hands will never get tangled regardless of how she moves, and both of her hands can be at the same position.

Now, Yuki wants you to help her calculate the minimum amount of stamina she needs to spend to complete the game. It is easy to prove that Yuki can always complete the game.

Input

The first line contains three integers $c, n, m$, where $c$ is the test case number. $c=0$ indicates that the test case is the sample.

The next $m$ lines each contain two integers $x_i, y_i$.

Output

Output a single integer representing the minimum amount of stamina she needs to spend to complete the game.

Examples

Input 1

0 3 4
1 2
2 3
2 2
3 1

Output 1

2

Note 1

One possible way to complete the game is:

  • Move Yuki's first hand from position $1$ to position $2$ at time $0.5$.
  • Move Yuki's second hand from position $3$ to position $1$ at time $2.5$.

It can be proven that Yuki must move at least $2$ times, spending $2$ units of stamina.

Input 2

See $\textit{circle/circle2.in}$ and $\textit{circle/circle2.ans}$ in the provided files.

This sample satisfies the constraints for test case $3$.

Input 3

See $\textit{circle/circle3.in}$ and $\textit{circle/circle3.ans}$ in the provided files.

This sample satisfies the constraints for test case $7$.

Input 4

See $\textit{circle/circle4.in}$ and $\textit{circle/circle4.ans}$ in the provided files.

This sample satisfies the constraints for test case $13$.

Input 5

See $\textit{circle/circle5.in}$ and $\textit{circle/circle5.ans}$ in the provided files.

This sample satisfies the constraints for test case $16$.

Input 6

See $\textit{circle/circle6.in}$ and $\textit{circle/circle6.ans}$ in the provided files.

This sample satisfies the constraints for test case $18$.

Input 7

See $\textit{circle/circle7.in}$ and $\textit{circle/circle7.ans}$ in the provided files.

This sample satisfies the constraints for test case $25$.

Constraints

For all test data, it is guaranteed that:

  • $2 \le n \le 3\times10^5$, $1 \le m \le 3\times10^5$;
  • $1 \le x_i \le m$, $1 \le y_i \le n$;
  • For every pair of positive integers $i, j$ such that $1 \le i < j \le m$, $x_i \ne x_j$ or $y_i \ne y_j$;
  • For every positive integer $t$, there are no more than two positive integers $i$ such that $x_i = t$.
Test Case Number $n, m \le$ Special Property
$1\sim2$ $20$ None
$3\sim4$ $80$ None
$5$ $400$ A
$6$ $400$ B
$7\sim8$ $400$ None
$9\sim10$ $5\times10^3$ A
$11\sim12$ $5\times10^3$ B
$13\sim15$ $5\times10^3$ None
$16\sim17$ $3\times10^5$ A
$18\sim21$ $3\times10^5$ B
$22\sim25$ $3\times10^5$ None
  • Special Property A: For every positive integer $t$, the number of positive integers $i$ such that $x_i = t$ is even.
  • Special Property B: For every positive integer $t$, there is at most one positive integer $i$ such that $x_i = t$.

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.