Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <cmath>
- #include <iostream>
- #include <vector>
- using std::cin;
- using std::cout;
- using std::max;
- using std::min;
- using std::string;
- using std::vector;
- using Pii = std::pair<int, int>;
- bool Cmp(Pii aa, Pii bb) {
- return aa.first < bb.first || (aa.first == bb.first && aa.second > bb.second);
- }
- int main() {
- std::ios::sync_with_stdio(false);
- std::cin.tie(0);
- std::cout.tie(0);
- int nn;
- cin >> nn;
- vector<Pii> aa;
- for (int ii = 0; ii < nn; ++ii) {
- int li;
- int ri;
- cin >> li >> ri;
- aa.push_back({li, 1});
- aa.push_back({ri, -1});
- }
- sort(aa.begin(), aa.end(), Cmp);
- /*cout << "SORTED\n";
- for (auto pp: aa) {
- cout << pp.first << ' ' << pp.second << "\n";
- }*/
- vector<Pii> ans;
- int bal = 0;
- int lbound;
- int rbound;
- for (int ii = 0; ii < nn * 2; ++ii) {
- bal += aa[ii].second;
- if (bal == 1 && aa[ii].second == 1) {
- lbound = aa[ii].first;
- // cout << "l r = " << lbound << ' ' << rbound << "\n";
- }
- if (bal == 0) {
- rbound = aa[ii].first;
- // cout << "l r = " << lbound << ' ' << rbound << "\n";
- ans.push_back({lbound, rbound});
- }
- }
- cout << ans.size() << "\n";
- for (auto pp : ans) {
- cout << pp.first << ' ' << pp.second << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement