No.1244 Black Segment
タグ : / 解いたユーザー数 122
作問者 : 👑
tute7627
/ テスター :
tempura_pp
Problem Statement
$N$ cells are arranged in a horizontal row, numbered from $1$ to $N$ from left to right.
Initially, all cells are painted white.
You want to paint black exactly the cells of a single segment that contains both cells numbered $A,B$ $(A \le B)$.
More precisely, you need to reach a state for which there exists a pair of integers $X,Y$ satisfying the following conditions.
- $X \le A \le B \le Y$
- The cells numbered from $X$ to $Y$ inclusive are painted black, and all other cells are painted white.
You may press the buttons in any order, any number of times.
Find the minimum number of button presses needed to make the cells satisfy the condition.
Input
$N\ M\ A\ B$ $L_1\ R_1$ $L_2\ R_2$ $\vdots$ $L_M\ R_M$
- All inputs are integers.
- $1 \le N \le 10^5$
- $1 \le M \le 10^5$
- $1 \le A \le B \le N$
- $1 \le L_i \le R_i \le N$
Output
Output the minimum number of button presses.
If it is impossible to satisfy the condition, output -1.
Print a newline at the end.
Sample
Sample 1
Input
4 3 2 3 1 2 3 3 2 4
Output
1
Pressing the $3$-rd button satisfies the condition.
Although it does not give the minimum number of presses, pressing the $1$-st button and the $2$-nd button in this order also satisfies the condition.
Sample 2
Input
5 3 2 4 1 3 3 5 1 1
Output
-1
No matter how you press the buttons, the condition cannot be satisfied.
Sample 3
Input
5 5 1 5 1 3 3 5 2 2 4 4 2 4
Output
5
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。