fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4. //check common interval existence
  5. inline bool range(long n1, long n2, long n3, long n4)
  6. {
  7. return min(n3, n4) <= max(n1, n2) && max(n3, n4) >= min(n1, n2);
  8. }
  9.  
  10. bool intersect( long A, long tA, long B, long tB, long C, long tC, long D, long tD )
  11. {
  12. long a1 = tA-tB; long a2 = tC-tD;
  13. long b1 = B-A; long b2 = D-C;
  14. long c1 = A*tB - B*tA; long c2 = C*tD - D*tC;
  15. long r1 = a1*C+b1*tC+c1; long r3 = a2*A+b2*tA+c2;
  16. long r2 = a1*D+b1*tD+c1; long r4 = a2*B+b2*tB+c2;
  17. if (r1*r2 <= 0 && r3*r4 <= 0) return true;
  18. return false;
  19. }
  20.  
  21. int main()
  22. {
  23. long A, B, C, D; //X-axis
  24. long tA, tB, tC, tD; //Y-axis
  25.  
  26. cin>> A >> tA;
  27. cin>> B >> tB;
  28. cin>> C >> tC;
  29. cin>> D >> tD;
  30.  
  31. if (range(A, B, C, D))
  32. {
  33. //if both dots lie in different semiareas, or at least one of them lies on the segment
  34. bool r1 = intersect(A, tA, B, tB, C, tC, D, tD); //{(A,tA), (B,tB)} and {(C, tC), (D, tD)}
  35. bool r2 = intersect(A, tA, B, tB, C, 0, C, tC); //{(A,tA), (B,tB)} and {(C, 0), (C, tC)}
  36. bool r3 = intersect(A, tA, B, tB, D, tD, D, tD+tB); //{(A,tA), (B,tB)} and {(D, tD), (D, tD+tB)}
  37. bool r4 = intersect(C, tC, D, tD, A, 0, A, tA); //{(C,tC), (D,tD)} and {(A, 0), (A, tA)}
  38. bool r5 = intersect(C, tC, D, tD, B, tB, B, tB+tD); //{(C,tC), (D,tD)} and {(B, tB), (B, tB+tD)}
  39. if (r1 || r2 || r3 || r4 || r5) //if at least one term is true
  40. {
  41. cout<<"Yes\n";
  42. return 0;
  43. }
  44. }
  45. cout<<"No\n";
  46. return 0;
  47. }
Success #stdin #stdout 0s 3460KB
stdin
0 0 
10 10
0 10
10 0
stdout
Yes