Advertisement
_rashed

UVA 833

Jun 29th, 2022
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. #define ll long long
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. const int OO = 1e9;
  6. const double EPS = 1e-9;
  7.  
  8. class L {
  9. public:
  10.     ll x1,y1,x2,y2,c;
  11.  
  12.     double pry(pair<ll,ll> p) {
  13.         if((p.first == x1 && p.second == y1) || (p.first == x2 && p.second == y2))
  14.             return -1;
  15.         double py = ((y2-y1)*p.first+c)/(1.0*(x2-x1));
  16.         if(py+EPS >= min(y1,y2) && py-EPS <= max(y1,y2) && py < p.second)
  17.             return py;
  18.         else
  19.             return -1;
  20.     }
  21.  
  22.     pair<ll,ll> nxt() {
  23.         if(y1 < y2) {
  24.             return {x1,y1};
  25.         }
  26.         return {x2,y2};
  27.     }
  28. };
  29.  
  30. vector<L> lines;
  31.  
  32. int main()
  33. {
  34.     ios_base::sync_with_stdio(false);
  35.     cin.tie(NULL);
  36.     cout.tie(NULL);
  37.     int t;
  38.     cin >> t;
  39.     while(t--) {
  40.         lines.clear();
  41.         int np;
  42.         cin >> np;
  43.         for(int i = 0; i < np; i++) {
  44.             L lc;
  45.             cin >> lc.x1 >> lc.y1 >> lc.x2 >> lc.y2;
  46.             lc.c = lc.y1*(lc.x2-lc.x1)-(lc.y2-lc.y1)*lc.x1;
  47.             lines.push_back(lc);
  48.         }
  49.         int ns;
  50.         cin >> ns;
  51.         for(int i = 0; i < ns; i++) {
  52.             pair<ll,ll> curr;
  53.             cin >> curr.first >> curr.second;
  54.             while(true) {
  55.                 double py = -1;
  56.                 int py_idx = -1;
  57.                 for(int j = 0; j < np; j++) {
  58.                     double curry = lines[j].pry(curr);
  59.                     if(curry > py) {
  60.                         py_idx = j;
  61.                         py = curry;
  62.                     }
  63.                 }
  64.                 if(py_idx != -1) {
  65.                     curr = lines[py_idx].nxt();
  66.                 }
  67.                 else {
  68.                     cout << curr.first << "\n";
  69.                     break;
  70.                 }
  71.             }
  72.         }
  73.         if(t) {
  74.             cout << "\n";
  75.         }
  76.     }
  77.     return 0;
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement