代码比较易读,暂不作解释
#include<bits/stdc++.h>
using namespace std;
#define l first
#define r second
typedef pair<int, int> PII;
vector <PII> segs;
int n, l, r, cnt;
int main()
{
cin >> n;
while(n --)
{
cin >> l >> r;
segs.push_back({l, r});
}
sort(segs.begin(), segs.end());
l = segs[0].l, r = segs[0].r, cnt = 1;
for(int i = 1; i < segs.size(); i ++)
if(segs[i].l <= r) r = min(r, segs[i].r);
else cnt ++, l = segs[i].l, r = segs[i].r;
cout << cnt;
}