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
| #include <iostream> #include <vector> #include <algorithm>
using namespace std;
typedef pair<int, int> PII;
const int N = 1e5 + 10;
int n; vector<PII> segs;
void merge(vector<PII> &segs){ vector<PII> res; sort(segs.begin(), segs.end()); int st = -2e9, ed = -2e9; for(auto seg : segs) if(ed < seg.first){ if(st != -2e9) res.push_back({st,ed}); st = seg.first; ed = seg.second; } else ed = max(ed, seg.second); if(st != -2e9) res.push_back({st,ed}); segs = res; }
int main(){ cin >> n; for(int i = 0; i < n; i ++){ int l, r; cin>>l>>r; segs.push_back({l,r}); } merge(segs); cout<<segs.size()<<endl; return 0; }
|