Under construction

Still being built. Things may change

Bouquet

Time limit3sMemory limit1024 MB

Summary
Given N tulips with left and right protection ranges, find the largest set of tulips where no picked tulip falls inside another picked tulip's protected range.
Level

Medium6 of 10

Topics
Dynamic programming, Segment tree
Solved
No attempts yet

Problem

After visiting Keukenhof, one of the world's largest flower gardens, Lieke became very fond of flowers. She decided to pick some tulips growing next to the road to build a beautiful bouquet. However, the strict tulip protection laws in the Netherlands limit which flowers she can collect.

There are NN tulips numbered from 00 to N−1N-1 growing in a line along the road, from left to right. The protection law assigns two integers, lil_i and rir_i, to tulip ii. If Lieke picks tulip ii, then the lil_i tulips immediately to its left and the rir_i tulips immediately to its right cannot be in the bouquet. If fewer than lil_i tulips lie to the left (or fewer than rir_i to the right), all tulips on that side are excluded.

Find the maximum number of tulips Lieke can pick while following the law.

Input

The first line contains a single integer NN, the number of tulips.

The next NN lines describe the protection law. The iith line contains two integers lil_i and rir_i.

Output

Print a single integer, the maximum number of tulips Lieke can pick.

Constraints

  • 1≤N≤2⋅1051 \leq N \leq 2 \cdot 10^5.
  • 0≤li,ri≤N0 \leq l_i, r_i \leq N for i=0,1,…,N−1i = 0,1,\ldots, N-1.

Hint

In the first sample, if Lieke picks tulip 00, she cannot pick the two tulips on the right. Picking tulip 11 does not prohibit her from picking tulip 22, but tulip 22 prohibits her from picking tulip 11, so she cannot pick both. The maximum is 11.

In the second sample, the maximum is 33. The way to get it is shown in the picture below. Other choices give a smaller answer.

Examples5

  1. Example 1

    Input
    3
    0 3
    1 0
    1 0
    
    Expected output
    1
    
  2. Example 2

    Input
    5
    0 3
    1 0
    0 1
    2 0
    1 0
    
    Expected output
    3
    
  3. Example 3

    Input
    7
    0 0
    0 0
    1 0
    1 0
    2 0
    3 0
    2 0
    
    Expected output
    4
    
  4. Example 4

    Input
    6
    2 2
    2 2
    2 2
    2 2
    2 2
    2 2
    
    Expected output
    2
    
  5. Example 5

    Input
    7
    0 2
    2 0
    1 1
    2 2
    0 0
    0 1
    0 1
    
    Expected output
    3