fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define fi first
  4. #define se second
  5. #define ll long long
  6. #define el cout<<"\n"
  7. #define sz(x) (int)(x).size()
  8. #define all(x) (x).begin(),(x).end()
  9. #define f0(i,n) for(int i=0;i<n;i++)
  10. #define f1(i,n) for(int i=1;i<=n;i++)
  11. #define fz(i,a,n,z) for(int i=a;i<n;i+=z)
  12. #define rep(i,a,n,z) for(int i=a;i>n;i-=z)
  13. #define faster ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  14. #define file(name) freopen(name".inp","r",stdin);freopen(name".out","w",stdout);
  15. const int N = 1e5 + 5;
  16. pair<int, int> A[N];
  17. void solve() {
  18. int n;
  19. cin >> n;
  20. for (int i = 1; i <= n; ++i) {
  21. cin >> A[i].first >> A[i].second;
  22. }
  23. sort(A + 1, A + n + 1);
  24. int res = 2;
  25. for (int i = 2; i <= n; ++i) {
  26. if (A[i].first <= A[i - 1].second) {
  27. if (A[i - 1].second - A[i].first >= 1) {
  28. A[i].second = A[i - 1].second;
  29. }
  30. else if (A[i - 1].second - A[i].first < 0) {
  31. res += 2;
  32. }
  33. else {
  34. res++;
  35. }
  36. }
  37. else {
  38. res += 2;
  39. }
  40. cout << A[i].first << " " << A[i].second << endl;
  41. }
  42. cout << res;
  43. }
  44. int main() {
  45. faster
  46. solve();
  47. }
  48. /*-----------------------END-----------------------*/
  49.  
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
2