fork download
  1. /* وَقُلْ رَبِّ زِدْنِي عِلْمًاً */
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <string>
  6. #include <iomanip>
  7. #include <stack>
  8. #include <set>
  9. #include <queue>
  10. #include <deque>
  11. #include <vector>
  12. #include <fstream>
  13. #include <cctype>
  14. #include <utility>
  15.  
  16. #define ll long long
  17. #define ld long double
  18. #define sz(v) ((int)(v).size())
  19. #define all(v) ((v).begin()),((v).end())
  20. #define setp(n) cout << fixed << setprecision(n)
  21. #define O_o ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  22. #define yes cout << "yes\n"
  23. #define no cout << "no\n"
  24. #define fi(nn) for(ll i = 0; i < (nn); i++)
  25. #define fj(nn) for(ll j = 0; j < (nn); j++)
  26. #define fl(nn) for (ll i = nn - 1; i >= 0; i--)
  27. #define ffij(nn,mm) for(ll i = 0; i < (nn); i++)for(ll j = 0; j < (mm); j++)
  28. #define null NULL
  29. using namespace std;
  30.  
  31.  
  32.  
  33. int main()
  34. {
  35. O_o
  36. ll n, m;
  37. cin >> n >> m;
  38. vector <pair<ll, ll>> v(n), ftra;
  39. fi(n)cin >> v[i].first >> v[i].second;
  40. sort(v.begin(), v.end());
  41. ll mini = v[0].second, maxi = v[0].second;
  42. for (ll i = 1;i < n;i++) {
  43. if (v[i].first <= maxi) {
  44. maxi = max(maxi, v[i].second);
  45. }
  46. else {
  47. ftra.push_back({ mini,maxi });
  48. mini = v[i].first;
  49. maxi = v[i].second;
  50. }
  51. }
  52. ftra.push_back({ mini,maxi });
  53. fi(sz(ftra)) {
  54. cout << ftra[i].first << ' ' << ftra[i].second << '\n';
  55. }
  56. return 0;
  57. }
Success #stdin #stdout 0.01s 5284KB
stdin
5 20
1 2
2 3
5 9
7 10
15 20
stdout
2 3
5 10
15 20