Advertisement
erfanul007

UVa 191

Jun 27th, 2021
1,027
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.14 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. // #include <iostream>
  3. // #include <cstdio>
  4. // #include <cstdlib>
  5. // #include <algorithm>
  6. // #include <cmath>
  7. // #include <vector>
  8. // #include <set>
  9. // #include <map>
  10. // #include <queue>
  11. // #include <stack>
  12. // #include <ctime>
  13. // #include <cassert>
  14. // #include <complex>
  15. // #include <string>
  16. // #include <cstring>
  17. // #include <bitset>
  18. using namespace std;
  19.  
  20. // #pragma GCC optimize("Ofast,no-stack-protector")
  21. // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  22. // #pragma GCC optimize("unroll-loops")
  23.  
  24. #define ll              long long int
  25. #define vi              vector< int >
  26. #define vll             vector< ll >
  27.  
  28. #define sc              scanf
  29. #define pf              printf
  30. #define cspf(i)         pf("Case %d:\n", i)
  31. #define spc             pf(" ")
  32. #define line            pf("\n")
  33.  
  34. #define ff              first
  35. #define ss              second
  36. #define mp              make_pair
  37. #define pb              push_back
  38. #define ppb             pop_back
  39. #define tp(v,j)         get<j>(v)
  40. #define Log(b,x)        (log(x)/log(b))
  41.  
  42. #define FOR(i,x,y)      for(int i = int(x); i < int(y); i++)
  43. #define ROF(i,x,y)      for(int i = int(x)-1; i >= int(y); i--)
  44. #define clr(arr,x)      memset(arr, x, sizeof arr)
  45. #define vout(v,sz)      for(int w=0;w<sz;w++){if(w) spc; cout<<v[w];}
  46. #define all(v)          v.begin(), v.end()
  47. #define rall(v)         v.rbegin(), v.rend()
  48. #define unq(v)          sort(all(v)),(v).resize(unique(all(v))-v.begin())
  49. #define fastIO          ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr)
  50.  
  51. #define sc1(x)          sc("%d",&x)
  52. #define sc2(x,y)        sc("%d %d", &x, &y)
  53. #define sc3(x,y,z)      sc("%d %d %d", &x, &y, &z)
  54. #define scl1(x)         sc("%lld",&x)
  55. #define scl2(x,y)       sc("%lld %lld", &x, &y)
  56. #define scf1(x)         sc("%lf",&x);
  57. #define scf2(x,y)       sc("%lf %lf", &x, &y)
  58.  
  59. #define pf1(x)          pf("%d",x)
  60. #define pf2(x,y)        pf("%d %d", x, y)
  61. #define pfl1(x)         pf("%lld ",x)
  62. #define pfl2(x,y)       pf("%lld %lld", x, y)
  63.  
  64. #define MOD             (int)(1e9)
  65. #define MaxN            1000000
  66. #define inf             0x3f3f3f3f
  67. #define PI              acos(-1.0)  // 3.1415926535897932
  68. #define eps             1e-6
  69.  
  70. #ifdef ERFANUL007
  71.     #define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
  72.     template < typename Arg1 >
  73.     void __f(const char* name, Arg1&& arg1){
  74.         cout << name << " = " << arg1 << std::endl;
  75.     }
  76.     template < typename Arg1, typename... Args>
  77.     void __f(const char* names, Arg1&& arg1, Args&&... args){
  78.         const char* comma = strchr(names, ',');
  79.         cout.write(names, comma - names) << " = " << arg1 <<" | ";
  80.         __f(comma+1, args...);
  81.     }
  82. #else
  83.     #define debug(...)
  84. #endif
  85.  
  86. template <class T> inline T bigMod(T p,T e,T M){T ret=1; for(;e>0;e>>=1){ if(e&1) ret=(ret*p)%M; p=(p*p)%M;} return (T)ret;}
  87. template <class T> inline T modInverse(T a,T M){return bigMod(a,M-2,M);}
  88. template <class T> inline T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
  89. template <class T> inline T lcm(T a,T b) {a=abs(a);b=abs(b); return (a/gcd(a,b))*b;}
  90.  
  91. int dx[] = { 1,-1, 0, 0};                //graph moves
  92. int dy[] = { 0, 0, 1,-1};               //graph moves
  93.  
  94. struct PT{
  95.     ll x, y;
  96.     PT(){}
  97.     PT(ll x, ll y) : x(x), y(y) {}
  98.     void scan(){ scanf("%lld %lld",&x,&y);}
  99.     inline bool operator == (const PT &p) const {
  100.         return (x == p.x && y == p.y);
  101.     }
  102.     inline bool operator < (const PT &p) const {
  103.         return ((x < p.x) || (x == p.x && y < p.y));
  104.     }
  105. };
  106.  
  107. ll SQ(ll x){ return x*x;}
  108. /* return the euclidean distance of two point */
  109. ll Dis(PT a,PT b){ return SQ(a.x-b.x) + SQ(a.y-b.y);}
  110.  
  111. double absDis(PT a,PT b){ return sqrt(Dis(a,b));}
  112.  
  113. /*convert degree to radian*/
  114. double toRadian(double x){ return (x*(PI/180.0));}
  115. /*convert radian to degree*/
  116. double toDegree(double x){ return (x*(180.0/PI));}
  117.  
  118. /*convert a coordinate p from cartesian to polar
  119. r = sqrt(x*x + y*y)
  120. theta = atan(y/x) [in radian]*/
  121. PT toPolar(PT p){
  122.     return PT(sqrt(SQ(p.x)+SQ(p.y)), atan(p.y/p.x));
  123. }
  124. /*convert a coordinate p from polar to cartesian
  125. x = r * cos(theta) [in radian]
  126. y = r * sin(theta) [in radian]*/
  127. PT toCartesian(PT a){
  128.     return PT(a.x * cos(a.y), a.x * sin(a.y));
  129. }
  130.  
  131. /* divide line ab into m:n and return midpoint */
  132. PT divideLine(PT a, PT b, double m, double n){
  133.     return PT((a.x * m + b.x * n)/(m+n), (a.y * m + b.y * n)/(m+n));
  134. }
  135.  
  136. /* convert two points to vector line */
  137. PT vect(PT a,PT b){ return PT(a.x-b.x, a.y-b.y);}
  138. /* two vector lines Dot product */
  139. ll Dot(PT a,PT b){ return a.x*b.x + a.y*b.y;}
  140. /* two vector lines Cross product */
  141. ll Cross(PT a,PT b){ return a.x*b.y - b.x*a.y;}
  142.  
  143. /* line ab to point c
  144. 0 => if ab and c collinear
  145. + => clockwise
  146. - => anticlockwise */
  147. int orientation(PT a, PT b, PT c)
  148. {
  149.     ll val = (b.y-a.y)*(c.x-b.x)-(b.x-a.x)*(c.y-b.y);
  150.     if(val == 0) return 0;
  151.     return val > 0 ? 1 : -1;
  152. }
  153.  
  154. /* area of Triangle abc
  155. positive -> anticlockwise
  156. negative -> clockwise
  157. zero -> collinear */
  158. ll TriArea(PT a, PT b, PT c){ return Cross(vect(b, a), vect(c, a));}
  159.  
  160. /*line ab and line cd is parallel if ab X cd = 0 */
  161. bool ifParallel(PT a,PT b,PT c,PT d){ return Cross(vect(a,b),vect(c,d)) == 0;}
  162.  
  163. /*line ab and line cd is perpendicular if ab . cd = 0 */
  164. bool ifPerpendicular(PT a,PT b,PT c,PT d){ return Dot(vect(a,b),vect(c,d)) == 0;}
  165.  
  166. /* segment ab and segment cd intersect or not */
  167. bool isSegIntersect(PT a,PT b,PT c,PT d){
  168.     if(a.x > b.x) swap(a, b);
  169.     if(a.y > b.y) swap(a, b);
  170.     if(orientation(a, b, c) == 0) return c.x >= a.x && c.x <= b.x && c.y >= a.y && c.y <= b.y;
  171.     if(orientation(a, b, d) == 0) return d.x >= a.x && d.x <= b.x && d.y >= a.y && d.y <= b.y;
  172.     return (orientation(a, b, c) != orientation(a, b, d)
  173.         && orientation(c, d, a) != orientation(c, d, b));
  174. }
  175.  
  176. /* init first coordinate of array to f = arr[0] */
  177. PT _f;
  178. /*for clockwise sorting*/
  179. bool CC(PT &a,PT &b){ return (_f.x-a.x)*(_f.y-b.y)<(_f.x-b.x)*(_f.y-a.y);}
  180. /*for anti-clockwise sorting*/
  181. bool CCW(PT &a,PT &b){ return (_f.x-a.x)*(_f.y-b.y)>(_f.x-b.x)*(_f.y-a.y);}
  182. //--------------------------//
  183.  
  184. /* recieve a polygon v and a line ab, returns if they intersect */
  185. bool LineIntersectPolygon(const vector< PT > v, PT a, PT b){
  186.     int n = v.size();
  187.     for(int i=1; i<n; i++){
  188.         if(isSegIntersect(v[i-1], v[i], a, b)) return true;
  189.     }
  190.     return isSegIntersect(v[n-1], v[0], a, b);
  191. }
  192.  
  193. bool PointInPolygon(const vector< PT > v, PT a){
  194.     int n = v.size();
  195.     int ori = orientation(v[n-1], v[0], a);
  196.     for(int i=1; i<n; i++){
  197.         if(ori != orientation(v[i-1], v[i], a)) return false;
  198.     }
  199.     return true;
  200. }
  201.  
  202. int main()
  203. {
  204.     #ifdef ERFANUL007
  205.         clock_t tStart = clock();
  206.         freopen("input.txt", "r", stdin);
  207.         freopen("output.txt", "w", stdout);
  208.     #endif
  209.  
  210.     int t; sc1(t);
  211.  
  212.     while(t--){
  213.         PT a, b, reca, recb;
  214.         a.scan(); b.scan(); reca.scan(); recb.scan();
  215.         vector< PT > v;
  216.         v.pb(reca);
  217.         v.pb(PT(reca.x, recb.y));
  218.         v.pb(recb);
  219.         v.pb(PT(recb.x, reca.y));
  220.         if(LineIntersectPolygon(v, a, b) || PointInPolygon(v, a) || PointInPolygon(v, b)) pf("T\n");
  221.         else pf("F\n");
  222.     }
  223.  
  224.  
  225.     #ifdef ERFANUL007
  226.         fprintf(stderr, "\n>> Runtime: %.10fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC);
  227.     #endif
  228.  
  229.     return 0;
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement