実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 266 点
問題文
高橋君のクラスには N 人の生徒(1 から N の番号が付けられている)がいます。
このクラスでは M 組の友達関係があります。友達関係は双方向であり、i 番目の友達関係は生徒 U_i と生徒 V_i が互いに友達であることを意味します。
ここで、生徒 k の「人気度」を、生徒 k の友達の番号の総和と定義します。たとえば、生徒 k の友達が生徒 2, 5, 8 の 3 人であるとき、生徒 k の人気度は 2 + 5 + 8 = 15 です。友達が一人もいない生徒の人気度は 0 とします。
N 人の生徒すべての人気度のうち、最大値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq U_i, V_i \leq N
- U_i \neq V_i(自分自身との友達関係はない)
- 同じ友達関係が複数回与えられることはない(すなわち、i \neq j ならば (U_i, V_i) \neq (U_j, V_j) かつ (U_i, V_i) \neq (V_j, U_j))
- 入力はすべて整数である
入力
N M U_1 V_1 U_2 V_2 \vdots U_M V_M
- 1 行目には、生徒の人数を表す N と、友達関係の数を表す M が、スペース区切りで与えられる。
- 続く M 行の i 行目 (1 \leq i \leq M) には、i 番目の友達関係を構成する 2 人の生徒の番号 U_i と V_i が、スペース区切りで与えられる。
出力
すべての生徒の人気度の最大値を 1 行で出力せよ。
入力例 1
4 3 1 2 1 3 2 4
出力例 1
5
入力例 2
6 7 1 2 1 3 2 3 3 4 4 5 4 6 5 6
出力例 2
14
入力例 3
5 0
出力例 3
0
Score : 266 pts
Problem Statement
There are N students (numbered from 1 to N) in Takahashi's class.
In this class, there are M friendship relations. Friendships are bidirectional, and the i-th friendship means that student U_i and student V_i are friends with each other.
Here, the "popularity" of student k is defined as the sum of the numbers of student k's friends. For example, if student k's friends are students 2, 5, and 8 (three people), then the popularity of student k is 2 + 5 + 8 = 15. The popularity of a student who has no friends is 0.
Find the maximum value among the popularities of all N students.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq U_i, V_i \leq N
- U_i \neq V_i (no self-friendships)
- The same friendship is not given more than once (i.e., if i \neq j, then (U_i, V_i) \neq (U_j, V_j) and (U_i, V_i) \neq (V_j, U_j))
- All input values are integers
Input
N M U_1 V_1 U_2 V_2 \vdots U_M V_M
- The first line contains N, the number of students, and M, the number of friendships, separated by a space.
- The i-th of the following M lines (1 \leq i \leq M) contains the numbers U_i and V_i of the two students forming the i-th friendship, separated by a space.
Output
Print the maximum value of the popularity among all students in a single line.
Sample Input 1
4 3 1 2 1 3 2 4
Sample Output 1
5
Sample Input 2
6 7 1 2 1 3 2 3 3 4 4 5 4 6 5 6
Sample Output 2
14
Sample Input 3
5 0
Sample Output 3
0
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
高橋君は N 店舗を展開する小売チェーンの在庫管理を担当しています。各店舗には商品が保管されており、i 番目の店舗 (1 \leq i \leq N) には現在 A_i 個の商品があります。
これから Q 回の在庫更新が順に行われます。j 回目 (1 \leq j \leq Q) の更新では、店舗 X_j の在庫数を Y_j 個に上書きします。すなわち、更新前の在庫数によらず、店舗 X_j の在庫数はちょうど Y_j 個になります。
各更新が完了した直後における、全店舗の在庫数の合計をそれぞれ求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 0 \leq A_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq X_j \leq N (1 \leq j \leq Q)
- 0 \leq Y_j \leq 10^9 (1 \leq j \leq Q)
- 入力はすべて整数である。
入力
N Q A_1 A_2 \ldots A_N X_1 Y_1 X_2 Y_2 \vdots X_Q Y_Q
- 1 行目には、店舗数を表す整数 N と、更新回数を表す整数 Q が、スペース区切りで与えられる。
- 2 行目には、各店舗の初期在庫数を表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
- 続く Q 行のうち j 行目 (1 \leq j \leq Q) には、j 回目の更新で在庫数を上書きする店舗の番号 X_j と、上書き後の在庫数 Y_j が、スペース区切りで与えられる。
出力
Q 行出力せよ。j 行目 (1 \leq j \leq Q) には、j 回目の更新が完了した直後における、全店舗の在庫数の合計を出力せよ。
入力例 1
3 4 10 20 30 1 15 2 25 3 35 1 0
出力例 1
65 70 75 60
入力例 2
5 6 100 200 300 400 500 3 350 1 150 5 450 2 200 4 0 3 300
出力例 2
1550 1600 1550 1550 1150 1100
入力例 3
8 10 1000000000 500000000 250000000 125000000 62500000 31250000 15625000 7812500 1 0 2 1000000000 8 0 4 500000000 3 0 6 100000000 5 0 7 0 1 999999999 2 1
出力例 3
992187500 1492187500 1484375000 1859375000 1609375000 1678125000 1615625000 1600000000 2599999999 1600000000
Score : 300 pts
Problem Statement
Takahashi is in charge of inventory management for a retail chain that operates N stores. Each store holds products, and the i-th store (1 \leq i \leq N) currently has A_i products.
From now on, Q inventory updates will be performed in order. In the j-th update (1 \leq j \leq Q), the inventory of store X_j is overwritten to Y_j items. That is, regardless of the previous inventory count, the inventory of store X_j becomes exactly Y_j items.
For each update, determine the total inventory across all stores immediately after the update is completed.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 0 \leq A_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq X_j \leq N (1 \leq j \leq Q)
- 0 \leq Y_j \leq 10^9 (1 \leq j \leq Q)
- All input values are integers.
Input
N Q A_1 A_2 \ldots A_N X_1 Y_1 X_2 Y_2 \vdots X_Q Y_Q
- The first line contains an integer N representing the number of stores and an integer Q representing the number of updates, separated by a space.
- The second line contains integers A_1, A_2, \ldots, A_N representing the initial inventory of each store, separated by spaces.
- In the following Q lines, the j-th line (1 \leq j \leq Q) contains the store number X_j whose inventory is to be overwritten in the j-th update and the new inventory count Y_j after overwriting, separated by a space.
Output
Output Q lines. The j-th line (1 \leq j \leq Q) should contain the total inventory across all stores immediately after the j-th update is completed.
Sample Input 1
3 4 10 20 30 1 15 2 25 3 35 1 0
Sample Output 1
65 70 75 60
Sample Input 2
5 6 100 200 300 400 500 3 350 1 150 5 450 2 200 4 0 3 300
Sample Output 2
1550 1600 1550 1550 1150 1100
Sample Input 3
8 10 1000000000 500000000 250000000 125000000 62500000 31250000 15625000 7812500 1 0 2 1000000000 8 0 4 500000000 3 0 6 100000000 5 0 7 0 1 999999999 2 1
Sample Output 3
992187500 1492187500 1484375000 1859375000 1609375000 1678125000 1615625000 1600000000 2599999999 1600000000
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 366 点
問題文
高橋君は会社の会議予約管理システムを開発しています。
この会社では、1 日を T 分間として運営しており、時刻 0 から時刻 T までの時間帯で会議の予約を受け付けています。それぞれの会議には開始時刻と終了時刻が設定されています。
高橋君は、予約の重なり具合を数値化したいと考えました。具体的には、ある瞬間に同時に行われている会議の件数の最大値を求めたいです。
N 件の会議の予約があり、i 番目 (1 \leq i \leq N) の予約は時刻 S_i に開始し、時刻 E_i に終了します。会議は開始時刻を含み、終了時刻を含みません。すなわち、i 番目の会議が行われている時間帯は半開区間 [S_i, E_i) です。
ある時刻 t において、S_i \leq t < E_i を満たす予約の数を、その時刻の「同時利用数」と呼びます。
0 \leq t < T を満たすすべての実数 t における同時利用数の最大値を求めてください。
なお、異なる予約の開始時刻や終了時刻が互いに一致することや、まったく同じ時間帯の予約が複数存在することもあり得ます。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq T \leq 10^9
- 0 \leq S_i < E_i \leq T (1 \leq i \leq N)
- 入力はすべて整数
入力
N T S_1 E_1 S_2 E_2 \vdots S_N E_N
- 1 行目には、予約の件数を表す整数 N と、1 日の長さ(分)を表す整数 T が、スペース区切りで与えられる。
- 続く N 行のうち i 行目 (1 \leq i \leq N) には、i 番目の予約の開始時刻 S_i と終了時刻 E_i がスペース区切りで与えられる。
出力
0 \leq t < T を満たすすべての時刻における同時利用数の最大値を 1 行で出力せよ。
入力例 1
3 60 10 30 20 40 35 50
出力例 1
2
入力例 2
5 480 0 120 60 180 90 150 200 300 200 400
出力例 2
3
入力例 3
8 1000000000 100 500000000 100 999999999 200 300 200 400 200 500 999999000 1000000000 0 1000000000 50 150
出力例 3
6
Score : 366 pts
Problem Statement
Takahashi is developing a meeting reservation management system for his company.
This company operates with a day lasting T minutes, and accepts meeting reservations during the time period from time 0 to time T. Each meeting has a designated start time and end time.
Takahashi wants to quantify the degree of reservation overlap. Specifically, he wants to find the maximum number of meetings taking place simultaneously at any given moment.
There are N meeting reservations, and the i-th reservation (1 \leq i \leq N) starts at time S_i and ends at time E_i. A meeting includes its start time but excludes its end time. That is, the time interval during which the i-th meeting takes place is the half-open interval [S_i, E_i).
At a given time t, the number of reservations satisfying S_i \leq t < E_i is called the "concurrent usage count" at that time.
Find the maximum concurrent usage count over all real numbers t satisfying 0 \leq t < T.
Note that different reservations may share the same start times or end times, and there may be multiple reservations with exactly the same time interval.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq T \leq 10^9
- 0 \leq S_i < E_i \leq T (1 \leq i \leq N)
- All input values are integers
Input
N T S_1 E_1 S_2 E_2 \vdots S_N E_N
- The first line contains an integer N representing the number of reservations and an integer T representing the length of a day (in minutes), separated by a space.
- The following N lines each contain, on the i-th line (1 \leq i \leq N), the start time S_i and end time E_i of the i-th reservation, separated by a space.
Output
Output in a single line the maximum concurrent usage count over all times t satisfying 0 \leq t < T.
Sample Input 1
3 60 10 30 20 40 35 50
Sample Output 1
2
Sample Input 2
5 480 0 120 60 180 90 150 200 300 200 400
Sample Output 2
3
Sample Input 3
8 1000000000 100 500000000 100 999999999 200 300 200 400 200 500 999999000 1000000000 0 1000000000 50 150
Sample Output 3
6
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
高橋君は天文台で流れ星の観測を行っています。夜空を H 行 W 列のグリッドとしてモデル化しており、上から i 行目、左から j 列目の区画を (i, j) と表します(1 \leq i \leq H、1 \leq j \leq W)。行番号は上から下へ、列番号は左から右へ増加します。
この夜空には N 個の流れ星が出現します。k 番目の流れ星(1 \leq k \leq N)は、時刻 0 に区画 (R_k, C_k) に出現し、時刻が 1 進むごとに左上方向に 1 区画移動します。すなわち、時刻 t(t = 0, 1, 2, \ldots)において、k 番目の流れ星は区画 (R_k - t, C_k - t) に位置します。ただし、グリッドの外に出た流れ星(R_k - t < 1 または C_k - t < 1 となった場合)はその時刻以降存在しなくなります。
まとめると、k 番目の流れ星が存在する区画の集合は
\{(R_k - t,\ C_k - t) \mid t = 0, 1, \ldots, \min(R_k, C_k) - 1\}
です。
高橋君は、グリッド上の区画を 0 個以上選び、選んだ各区画にカメラを 1 台ずつ設置します。カメラを設置する区画はグリッド内の任意の区画から自由に選べます。各カメラはすべての時刻を通じて、設置された区画に固定されています。
ある流れ星がいずれかの時刻においてカメラの設置された区画に存在するならば、その流れ星はそのカメラによって撮影されます。1 台のカメラは、設置された区画を通過するすべての流れ星(異なる時刻に通過するものも含む)を撮影できます。
すべての流れ星がそれぞれ少なくとも 1 台のカメラによって撮影されるようにしたいとき、設置するカメラの台数の最小値を求めてください。
制約
- 1 \leq H \leq 10^9
- 1 \leq W \leq 10^9
- 1 \leq N \leq 2 \times 10^5
- 1 \leq R_k \leq H(1 \leq k \leq N)
- 1 \leq C_k \leq W(1 \leq k \leq N)
- (R_k, C_k) はすべて異なる(すなわち、どの 2 つの流れ星も初期位置が異なる)
- 入力はすべて整数である
入力
H W N R_1 C_1 R_2 C_2 \vdots R_N C_N
- 1 行目には、グリッドの行数 H、列数 W、流れ星の個数 N がスペース区切りで与えられる。
- 続く N 行のうち k 行目(1 \leq k \leq N)には、k 番目の流れ星の初期位置の行番号 R_k と列番号 C_k がスペース区切りで与えられる。
出力
すべての流れ星を撮影するために必要なカメラの最小台数を 1 行で出力せよ。
入力例 1
5 5 5 3 3 4 4 5 5 2 4 4 2
出力例 1
3
入力例 2
3 4 4 1 4 2 2 3 1 3 4
出力例 2
4
入力例 3
20 20 18 1 1 2 2 3 3 4 4 5 5 6 1 7 2 8 3 9 4 10 5 1 10 2 11 3 12 15 1 16 2 20 10 10 20 11 19
出力例 3
7
入力例 4
1000000000 1000000000 40 1 1 1 1000000000 1000000000 1 1000000000 1000000000 999999999 999999999 999999998 999999997 999999997 999999998 500000000 500000000 500000001 500000000 500000000 500000001 123456789 987654321 987654321 123456789 314159265 271828182 271828182 314159265 42 999999999 999999999 42 2 2 3 3 4 4 5 5 10 20 20 10 100 200 200 100 1000 2000 2000 1000 12345 54321 54321 12345 111111111 222222222 222222222 111111111 333333333 444444444 444444444 333333333 555555555 666666666 666666666 555555555 777777777 888888888 888888888 777777777 135791357 246802468 246802468 135791357 999999000 999998000 999998000 999999000
出力例 4
23
入力例 5
1 1 1 1 1
出力例 5
1
Score : 400 pts
Problem Statement
Takahashi is observing shooting stars at an observatory. He models the night sky as a grid with H rows and W columns, where the cell in the i-th row from the top and the j-th column from the left is denoted as (i, j) (1 \leq i \leq H, 1 \leq j \leq W). Row numbers increase from top to bottom, and column numbers increase from left to right.
N shooting stars appear in this night sky. The k-th shooting star (1 \leq k \leq N) appears at cell (R_k, C_k) at time 0, and moves one cell in the upper-left direction each time unit. That is, at time t (t = 0, 1, 2, \ldots), the k-th shooting star is located at cell (R_k - t, C_k - t). However, a shooting star that goes outside the grid (when R_k - t < 1 or C_k - t < 1) ceases to exist from that time onward.
In summary, the set of cells where the k-th shooting star exists is
\{(R_k - t,\ C_k - t) \mid t = 0, 1, \ldots, \min(R_k, C_k) - 1\}
Takahashi selects 0 or more cells on the grid and places one camera at each selected cell. The cells where cameras are placed can be freely chosen from any cells within the grid. Each camera remains fixed at its installed cell throughout all times.
If a shooting star is present at a cell where a camera is installed at any point in time, that shooting star is captured by that camera. A single camera can capture all shooting stars that pass through its installed cell (including those that pass through at different times).
Find the minimum number of cameras that need to be installed so that every shooting star is captured by at least one camera.
Constraints
- 1 \leq H \leq 10^9
- 1 \leq W \leq 10^9
- 1 \leq N \leq 2 \times 10^5
- 1 \leq R_k \leq H (1 \leq k \leq N)
- 1 \leq C_k \leq W (1 \leq k \leq N)
- All (R_k, C_k) are distinct (i.e., no two shooting stars share the same initial position)
- All input values are integers
Input
H W N R_1 C_1 R_2 C_2 \vdots R_N C_N
- The first line contains the number of rows H, the number of columns W, and the number of shooting stars N, separated by spaces.
- The k-th of the following N lines (1 \leq k \leq N) contains the row number R_k and column number C_k of the initial position of the k-th shooting star, separated by spaces.
Output
Output in one line the minimum number of cameras required to capture all shooting stars.
Sample Input 1
5 5 5 3 3 4 4 5 5 2 4 4 2
Sample Output 1
3
Sample Input 2
3 4 4 1 4 2 2 3 1 3 4
Sample Output 2
4
Sample Input 3
20 20 18 1 1 2 2 3 3 4 4 5 5 6 1 7 2 8 3 9 4 10 5 1 10 2 11 3 12 15 1 16 2 20 10 10 20 11 19
Sample Output 3
7
Sample Input 4
1000000000 1000000000 40 1 1 1 1000000000 1000000000 1 1000000000 1000000000 999999999 999999999 999999998 999999997 999999997 999999998 500000000 500000000 500000001 500000000 500000000 500000001 123456789 987654321 987654321 123456789 314159265 271828182 271828182 314159265 42 999999999 999999999 42 2 2 3 3 4 4 5 5 10 20 20 10 100 200 200 100 1000 2000 2000 1000 12345 54321 54321 12345 111111111 222222222 222222222 111111111 333333333 444444444 444444444 333333333 555555555 666666666 666666666 555555555 777777777 888888888 888888888 777777777 135791357 246802468 246802468 135791357 999999000 999998000 999998000 999999000
Sample Output 4
23
Sample Input 5
1 1 1 1 1
Sample Output 5
1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 466 点
問題文
高橋君と青木君は、二人対戦のゲームで遊んでいます。
盤面には N 個のマスが一列に並んでおり、左から順にマス 1 , マス 2 , \ldots , マス N と番号が付けられています。マス i には A_i 個の石が置かれています。
また、盤面には M 本の矢印があります。 j 番目の矢印はマス U_j からマス V_j への一方通行の経路です( U_j > V_j 、すなわち必ず番号の大きいマスから小さいマスへ向かいます)。この条件により、矢印をたどって同じマスに戻ることはできません。
高橋君が先手で、二人は交互に以下の操作を行います。
操作: 石が 1 個以上あり、かつそのマスから出発する矢印が 1 本以上存在するようなマスを 1 つ選ぶ。そのマスから石を 1 個取り、そのマスから出発する矢印を 1 本選んで、その石を矢印の先のマスへ移動させる。
操作を行えなくなったプレイヤー、すなわち、石が 1 個以上あるマスの中に矢印が出ているマスが 1 つも存在しない状態で手番が回ってきたプレイヤーが負けとなります。(盤面に石が 1 個もない場合も、操作を行えないため負けとなります。)
ここで、ゲーム開始前に高橋君は ちょうど 1 回 「除去」を行わなければなりません。除去とは、任意のマス i を 1 つ選び、そのマスの石を すべて 盤面から取り除く操作です( A_i を 0 にします)。石がもともと 0 個のマスを選んでもよく、その場合は盤面は変化しません。この選択により、除去を実質的に行わないことも可能です。
高橋君が除去の対象マスを最適に選び、その後のゲームでも両者が最適に行動するとき、高橋君が勝てるような「除去」の対象として選べるマスの個数を求めてください。
制約
- 1 \leq N \leq 10^6
- 0 \leq M \leq 10^5
- 0 \leq A_i \leq 10^9 ( 1 \leq i \leq N )
- 1 \leq V_j < U_j \leq N ( 1 \leq j \leq M )
- 同じ始点・終点の組の矢印は高々 1 本である。すなわち、 i \neq j ならば (U_i, V_i) \neq (U_j, V_j) である。
- 入力はすべて整数である。
入力
N M A_1 A_2 \ldots A_N U_1 V_1 U_2 V_2 \vdots U_M V_M
- 1 行目には、マスの数を表す整数 N と矢印の数を表す整数 M が、スペース区切りで与えられる。
- 2 行目には、各マスに置かれている石の個数を表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
- 3 行目から M 行にわたって、矢印の情報が与えられる。 2 + j 行目には j 番目の矢印の始点 U_j と終点 V_j がスペース区切りで与えられる。 U_j > V_j が保証される。
出力
高橋君が除去の対象として選んだとき、高橋君が勝てるようなマスの個数を 1 行で出力せよ。
入力例 1
4 3 1 2 0 1 2 1 3 1 4 2
出力例 1
0
入力例 2
5 4 0 1 1 0 2 2 1 3 2 4 3 5 4
出力例 2
4
入力例 3
10 12 3 0 5 2 1 4 0 7 2 6 2 1 3 1 4 2 5 1 5 3 6 2 7 4 8 3 8 6 9 5 10 7 10 8
出力例 3
9
入力例 4
30 35 0 12 5 0 9 3 14 0 1 7 20 2 0 11 4 6 13 0 8 15 1 10 0 19 5 2 17 0 6 21 2 1 3 1 4 2 5 2 5 3 6 1 6 4 7 3 8 5 9 4 9 7 10 6 11 8 12 9 13 10 14 11 15 12 16 13 17 14 18 15 19 16 20 17 21 18 22 19 23 20 24 21 25 22 26 23 27 24 28 25 29 26 30 27 30 15 28 10 25 5
出力例 4
8
入力例 5
1 0 1000000000
出力例 5
0
Score : 466 pts
Problem Statement
Takahashi and Aoki are playing a two-player game.
The board consists of N squares arranged in a row, numbered 1, 2, \ldots, N from left to right. Square i contains A_i stones.
There are also M arrows on the board. The j-th arrow is a one-way path from square U_j to square V_j (where U_j > V_j, meaning it always points from a square with a larger index to a square with a smaller index). This condition ensures that it is impossible to return to the same square by following the arrows.
Takahashi goes first, and the two players take turns making the following move:
Move: Choose a square that has at least 1 stone and at least 1 outgoing arrow. Take 1 stone from that square, choose 1 outgoing arrow from that square, and move the stone to the destination square of the arrow.
The player who cannot make a move (i.e., when it is their turn and there are no squares with both at least 1 stone and at least 1 outgoing arrow) loses. (If there are no stones on the board at all, the player also cannot make a move and loses.)
Before the game begins, Takahashi must perform a "removal" exactly once. A removal consists of choosing any square i and removing all stones from that square (setting A_i to 0). It is allowed to choose a square that already has 0 stones, in which case the board remains unchanged. This choice allows him to effectively not perform any removal.
Assuming Takahashi chooses the target square for removal optimally and both players play optimally thereafter, find the number of squares Takahashi can choose for the "removal" such that he wins.
Constraints
- 1 \leq N \leq 10^6
- 0 \leq M \leq 10^5
- 0 \leq A_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq V_j < U_j \leq N (1 \leq j \leq M)
- There is at most one arrow between any pair of starting and ending squares. That is, if i \neq j, then (U_i, V_i) \neq (U_j, V_j).
- All input values are integers.
Input
N M A_1 A_2 \ldots A_N U_1 V_1 U_2 V_2 \vdots U_M V_M
- The first line contains an integer N, the number of squares, and an integer M, the number of arrows, separated by a space.
- The second line contains integers A_1, A_2, \ldots, A_N separated by spaces, representing the number of stones placed on each square.
- The next M lines describe the arrows. The (2 + j)-th line contains the starting square U_j and the ending square V_j of the j-th arrow, separated by a space. It is guaranteed that U_j > V_j.
Output
Print the number of squares that Takahashi can choose for removal such that he wins, in a single line.
Sample Input 1
4 3 1 2 0 1 2 1 3 1 4 2
Sample Output 1
0
Sample Input 2
5 4 0 1 1 0 2 2 1 3 2 4 3 5 4
Sample Output 2
4
Sample Input 3
10 12 3 0 5 2 1 4 0 7 2 6 2 1 3 1 4 2 5 1 5 3 6 2 7 4 8 3 8 6 9 5 10 7 10 8
Sample Output 3
9
Sample Input 4
30 35 0 12 5 0 9 3 14 0 1 7 20 2 0 11 4 6 13 0 8 15 1 10 0 19 5 2 17 0 6 21 2 1 3 1 4 2 5 2 5 3 6 1 6 4 7 3 8 5 9 4 9 7 10 6 11 8 12 9 13 10 14 11 15 12 16 13 17 14 18 15 19 16 20 17 21 18 22 19 23 20 24 21 25 22 26 23 27 24 28 25 29 26 30 27 30 15 28 10 25 5
Sample Output 4
8
Sample Input 5
1 0 1000000000
Sample Output 5
0