1 #include <iostream>
2 #include <algorithm>
3 #include <vector>
4 #include <climits>
5
6 using namespace std;
7
8 const int N = 1e5+10;
9 vector<pair<int, int> > interval;//存储所有区间
10 int n;
11
12 void merge_interval(vector<pair<int, int> >& tmp){
13 sort(tmp.begin(), tmp.end());//pair类型排序先按first排序,再按照second排序
14
15 vector<pair<int, int> > ans;
16
17 int l = INT_MIN, r = INT_MIN;//相当于增加一个虚拟区间
18
19 for(auto intex : tmp){
20 //没有交集
21 if(intex.first > r){
22 if(l != INT_MIN && r != INT_MIN) ans.push_back({l, r});
23 l = intex.first;
24 r = intex.second;
25 }else
26 //有交集
27 r = max(r,intex.second);
28 }
29
30 if(l != INT_MIN && r != INT_MIN)ans.push_back({l, r});
31
32 interval = ans;
33 }
34
35 int main(){
36 cin >> n;
37 for(int i = 0;i < n;++i){
38 int l, r;
39 cin >> l >> r;
40 interval.push_back({l, r});
41 }
42 //区间合并
43 merge_interval(interval);
44
45 cout << interval.size() << endl;
46
47 return 0;
48 }