链接:区间合并
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef pair<int, int>PII;
void merge(vector<PII>& segs)
{
vector<PII>ans;
int st = -2e9, ed = -2e9;
for (const auto &seg : segs)
{
if (ed < seg.first)
{
if (st!=-2e9)
{
ans.push_back({ st,ed });
}
st = seg.first, ed = seg.second;
}
else
{
ed = max(ed, seg.second);
}
}
if (st!= -2e9)
{
ans.push_back({ st,ed });
}
segs = ans;
}
int main(void)
{
int n;
scanf("%d", &n);
vector<PII>segs;
for (int i = 0; i < n; i++)
{
int l, r;
scanf("%d %d", &l, &r);
segs.push_back({ l,r });
}
sort(segs.begin(), segs.end());
merge(segs);
printf("%d", segs.size());
return 0;
}