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. bool operator<(const LINE rhs)const{
  9. return s<rhs.s;}
  10. } line[MaxN];
  11. bool comp(LINE lhs, LINE rhs){
  12. return lhs.s<rhs.s;
  13. }
  14.  
  15. int main() {
  16. int N,s,e;
  17. cin>>N;
  18. for(int n=0;n<N;n++){
  19. cin>>s>>e;
  20. line[n]=LINE(s,e);
  21. }
  22. sort(line, line+N);
  23. vector<LINE> ans={line[0]};
  24. for(int n=1;n<N;n++){
  25. if(line[n].s<=ans.back().e){
  26. ans.back().e=max(ans.back().e,line[n].e);
  27. }else{
  28. ans.push_back(line[n]);
  29. }
  30. }
  31. for(LINE one: ans)
  32. cout<<one.s<<" "<<one.e<<"\n";
  33. return 0;
  34. }
Success #stdin #stdout 0.01s 5276KB
stdin
3
10 40
30 60
20 50
stdout
10 60