D - LIS 2 Editorial /

Time Limit: 2.5 sec / Memory Limit: 1024 MB

配点 : 600

問題文

数列 X があります。初め、X は空です。
高橋君は i=1,2,\ldots,N の順に次の操作をしました。

  • X の末尾に l_i,l_i+1,\ldots,r_i をこの順番で追加する。

操作後の X の狭義単調増加部分列の長さの最大値を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq l_i \leq r_i \leq 10^9
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

N
l_1 r_1
\vdots
l_{N} r_{N}

出力

答えを出力せよ。


入力例 1

4
1 1
2 4
10 11
7 10

出力例 1

8

操作後の X(1,2,3,4,10,11,7,8,9,10) です。
この数列の 1,2,3,4,7,8,9,10 項目からなる部分列は狭義単調増加であり、かつこれが長さが最大のものです。


入力例 2

4
1 1
1 1
1 1
1 1

出力例 2

1

操作後の X(1,1,1,1) です。


入力例 3

1
1 1000000000

出力例 3

1000000000

Score : 600 points

Problem Statement

We have a sequence X, which is initially empty.
Takahashi has performed the following operation for i=1,2,\ldots,N in this order.

  • Append l_i,l_i+1,\ldots,r_i in this order to the end of X.

Find the greatest length of a strictly increasing subsequence of the final X.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq l_i \leq r_i \leq 10^9
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N
l_1 r_1
\vdots
l_{N} r_{N}

Output

Print the answer.


Sample Input 1

4
1 1
2 4
10 11
7 10

Sample Output 1

8

The final X is (1,2,3,4,10,11,7,8,9,10).
The 1-st, 2-nd, 3-rd, 4-th, 7-th, 8-th, 9-th, and 10-th elements form a strictly increasing subsequence with the greatest length.


Sample Input 2

4
1 1
1 1
1 1
1 1

Sample Output 2

1

The final X is (1,1,1,1).


Sample Input 3

1
1 1000000000

Sample Output 3

1000000000