题目描述
模板题:区间合并
C++代码
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int N = 1e5 + 10;
int n, l, r;
vector<PII> alls;
void merge(vector<PII> &segs){
vector<PII> res;
int st = -1e9, ed = -1e9;
sort(segs.begin(), segs.end());
for(auto item : segs){
if(ed < item.first){
if(st != -1e9)
res.push_back({st, ed});
st = item.first;
ed = item.second;
}
else
ed = max(ed, item.second);
}
if(st != -1e9)
res.push_back({st, ed});
segs = res;
}
int main(){
cin >> n;
for(int i = 0; i < n; i++){
cin >> l >> r;
alls.push_back({l, r});
}
merge(alls);
cout << alls.size() << endl;
}