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