# 1、区间合并

# AcWing 803. 区间合并

import java.util.*;

//ACWing
public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.qujianhebing();
    }

    class Qujian implements Comparable {
        int l, r;

        public Qujian(int l, int r) {
            this.l = l;
            this.r = r;
        }

        @Override
        public int compareTo(Object o) {
            Qujian q = (Qujian) o;
            if (l - q.l == 0)
                return (r - q.r);
            else
                return (l - q.l);
        }
    }

    //区间合并
    void qujianhebing() {
        Scanner sc = new Scanner(System.in);
        TreeSet<Qujian> treeset = new TreeSet<Qujian>();
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            int l = sc.nextInt(), r = sc.nextInt();
            treeset.add(new Qujian(l, r));
        }
        int res = 0;
        int start = (int) -2e9, end = (int) -2e9;
        for (Qujian item : treeset) {
            int l = item.l;
            int r = item.r;
            if (l > end) {
                res++;
                start = l;
                end = r;
            } else {
                end = Math.max(end, r);
            }
        }
        System.out.println(res);
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53