Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. pair<int, int> f1(int l1, int l2, int r1, int r2, int s1, int s2, int d1, int d2){
  5.     if (s1+d1 < s2){
  6.         return {s1, s2};
  7.     }
  8.     else if (l1 + d1 > r2-d2){
  9.         return {-1, -1};
  10.     }
  11.     else{
  12.         s1 = s2-d1;
  13.         s1 = max(l1, s1);
  14.         if (s1+d1 >= s2){
  15.             s2 = s1+d1;
  16.             s2 = min(s2, r2);
  17.         }
  18.         if (s1+d1 <= s2){
  19.             return {s1, s2};
  20.         }
  21.         else return {-1, -1};
  22.     }
  23. }
  24.  
  25. pair<int, int> f2(int l1, int l2, int r1, int r2, int s1, int s2, int d1, int d2){
  26.     if (s1+d1 < s2){
  27.         return {s1, s2};
  28.     }
  29.     else if (l2+d2 > r1-d1){
  30.         return {-1, -1};
  31.     }
  32.     else{
  33.         s2 = s1 - d2;
  34.         s2 = max(l2, s2);
  35.         if (s2+d2 >= s1){
  36.             s1 = s2 + d2;
  37.             s1 = min(s1, r1);
  38.         }
  39.         if (s2+d2 <= s1){
  40.             return {s1, s2};
  41.         }
  42.         else return {-1, -1};
  43.     }
  44. }
  45. int main() {
  46.     iostream::sync_with_stdio(false); cout.tie(0); cin.tie(0);
  47.     int q; cin >> q;
  48.     while(q--){
  49.         int l1, l2, r1, r2;
  50.         cin >> l1 >> r1 >> l2 >> r2;
  51.         int s1, s2, d1, d2;
  52.         cin >> s1 >> d1 >> s2 >> d2;
  53.         pair<int, int> p1 = f1(l1, l2, r1, r2, s1, s2, d1, d2), p2 = f2(l1,l2,  r1, r2, s1, s2, d1, d2);
  54.         if (p1.first == -1 && p2.first == -1) cout << -1 << ' ' << -1 << '\n';
  55.         else{
  56.             if (p1.first == -1){
  57.                 cout << p2.first << ' ' << p2.second << '\n';
  58.             }
  59.             else if (p2.first == -1){
  60.                 cout << p1.first << ' ' << p1.second << '\n';
  61.             }
  62.             else{
  63.                 if (abs(s1 - p1.first) + abs(s2 - p1.second) < abs(s1-p2.first) + abs(s2-p2.second)){
  64.                     cout << p1.first << ' ' << p1.second << '\n';
  65.                 }
  66.                 else cout << p2.first << ' ' << p2.second << '\n';
  67.             }
  68.         }
  69.     }
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement