fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int MaxN=2e5;
  5. struct LINE{
  6. int s,e;
  7. LINE(int s=0, int e=0):s(s),e(e){}
  8. }line[MaxN];
  9.  
  10. bool comp(LINE lhs, LINE rhs){
  11. return lhs.s<rhs.s;
  12. }
  13. int main() {
  14. int N,s,e;
  15. cin>>N;
  16. for(int n=0;n<N;n++){
  17. cin>>s>>e;
  18. line[n]=LINE(s,e);
  19. }
  20. sort(line,line+N,comp);
  21. vector<LINE> ans={line[0]};
  22. for(int n=1;n<N;n++){
  23. if(line[n].s<=ans.back().e){
  24. ans.back().e=max(ans.back().e,line[n].e);
  25. }else{
  26. ans.push_back(line[n]);
  27. }
  28. }
  29.  
  30. for(LINE one:ans)
  31. cout<<one.s<<" "<<one.e<<"\n";
  32. return 0;
  33. }
Success #stdin #stdout 0.01s 5296KB
stdin
3
10 20
20 30
40 50
stdout
10 30
40 50