問題一覧 > 通常問題

No.1244 Black Segment

レベル : / AI推定難易度 : 1725 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 122
作問者 : 👑 tute7627 / テスター : tempura_pp
お気に入りにしたユーザー ProblemId : 3952 / 自分の提出
問題文最終更新日: 2020-10-03 00:47:21
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 268 (順位表) の他の問題:

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.
There are $M$ buttons. Pressing the $i$-th button flips the color (white and black) of the cells numbered from $L_i$ to $R_i$ inclusive.
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もしくは右上の雲マークをクリックしてアカウントを作成してください。