fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int l1, r1, l2, r2;
  7. cin >> l1 >> r1 >> l2 >> r2;
  8. // make l1 and r1 represent the left range
  9. // and l2 and r2 represent the right range
  10. if (l1 > l2)
  11. {
  12. // swap l1 with l2
  13. int tmpL = l1;
  14. l1 = l2;
  15. l2 = tmpL;
  16. // swap r1 with r2
  17. int tmpR = r1;
  18. r1 = r2;
  19. r2 = tmpR;
  20. }
  21. if (l2 > r1)
  22. {
  23. // no intersection between the 2 ranges
  24. cout << -1;
  25. }
  26. else
  27. {
  28. // check the start and end of the intersection between the 2 ranges
  29. int rangeL = l2, rangeR;
  30. // check the end of the range, it can be r1 or r2 (the least between them)
  31. if (r1 < r2)
  32. {
  33. rangeR = r1;
  34. }
  35. else
  36. {
  37. rangeR = r2;
  38. }
  39. cout << rangeL << " " << rangeR;
  40. }
  41. }
  42.  
Success #stdin #stdout 0s 5344KB
stdin
Standard input is empty
stdout
-1