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