fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define dd double
  5. #define ld long double
  6. #define pi 3.14159265359
  7. #define ull unsigned long long
  8. #define yes cout<<"YES\n"
  9. #define no cout<<"NO\n"
  10. #define el "\n"
  11. #define Arwa ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  12. #define fix(x) cout<<fixed<<setprecision(x)
  13. ll gcd(ll a,ll b) {return b==0?a:gcd(b,a%b);}
  14. ll lcm(int a,int b) {return a/gcd(a,b)*b;}
  15. bool prime(ll n)
  16. {
  17. if(n==2) return true;
  18. if(n%2==0||n==1) return false;
  19. else { for(ll i=3; i*i<=n; i+=2) {if(n%i==0) return false;}}return true;
  20. }
  21. //here we go again
  22. bool comp(pair<int,int>a,pair<int,int>b)
  23. {
  24. return a.first<b.first;
  25. return a.second<b.second;
  26. }
  27. int main()
  28. {
  29. Arwa
  30. int t=1;
  31. //cin>>t;
  32. while(t--)
  33. {
  34. int n;
  35. cin>>n;
  36. pair<ll,ll> arr[n];
  37. for(int i=0;i<n;i++)
  38. cin>>arr[i].first>>arr[i].second;
  39. for(int i=0;i<n;i++)
  40. {
  41. if(arr[i].first>arr[i].second)
  42. swap(arr[i].first,arr[i].second);
  43. }
  44. sort(arr,arr+n,comp);
  45. //for(int i=0;i<n;i++)
  46. //cout<<arr[i].first<<' '<<arr[i].second<<el;
  47. int min=arr[0].first,max=arr[0].second;
  48. for(int i=0;i<n;i++)
  49. {
  50. if(arr[i].first==arr[i+1].first)
  51. i++;
  52. if(i-1>=0)
  53. {
  54. if(arr[i].first>arr[i-1].second)
  55. {
  56. max=arr[i-1].second;
  57. cout<<min<<' '<<max<<el;
  58. min=arr[i].first;
  59. }
  60. }
  61. if(arr[i].second>max)
  62. max=arr[i].second;
  63. }
  64. cout<<min<<' '<<max<<el;
  65. }
  66. return 0;
  67. }
  68. /*
  69. 5
  70. 1 2
  71. 1 3
  72. 4 6
  73. 5 9
  74. */
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
0 0