Advertisement
erfanul007

LOJ 1190

Jun 27th, 2021
965
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.06 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll              long long int
  5. #define ld              long double
  6. #define all(v)          v.begin(), v.end()
  7. #define unq(v)          sort(all(v)),(v).resize(unique(all(v))-v.begin())
  8. #define inf             0x3f3f3f3f
  9. #define PI              acos(-1.0)  // 3.1415926535897932
  10. #define eps             1e-9
  11. #define radianof(x)     x * (PI / 180.0)
  12. #define degreeof(x)     x * (180.0 / PI)
  13. #define fixdouble(x)    cout << fixed << setprecision(x)
  14.  
  15. #ifdef ERFANUL007
  16.     #define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
  17.     template < typename Arg1 >
  18.     void __f(const char* name, Arg1&& arg1){
  19.         cout << name << " = " << arg1 << std::endl;
  20.     }
  21.     template < typename Arg1, typename... Args>
  22.     void __f(const char* names, Arg1&& arg1, Args&&... args){
  23.         const char* comma = strchr(names, ',');
  24.         cout.write(names, comma - names) << " = " << arg1 <<" | ";
  25.         __f(comma+1, args...);
  26.     }
  27. #else
  28.     #define debug(...)
  29. #endif
  30.  
  31. struct PT{
  32.     ll x, y;
  33.  
  34.     PT(){}
  35.     PT(ll x, ll y) : x(x), y(y) {}
  36.  
  37.     inline void input() {
  38.         scanf("%lld %lld", &x, &y);
  39.     }
  40.     inline void output() {
  41.         printf("{%lld, %lld}\n", x, y);
  42.     }
  43.     inline bool operator == (const PT &p) const {
  44.         return (abs(x - p.x) < eps && abs(y - p.y) < eps);
  45.     }
  46.     inline bool operator < (const PT &p) const {
  47.         return ((x + eps < p.x) || (abs(x - p.x) < eps && y + eps < p.y));
  48.     }
  49. } origin = PT(0, 0);
  50. //-----------operators-----------//
  51. inline PT operator + (const PT &u, const PT &v) {
  52.     return PT(u.x + v.x, u.y + v.y);
  53. }
  54. inline PT operator - (const PT &u, const PT &v) {
  55.     return PT(u.x - v.x, u.y - v.y);
  56. }
  57. inline PT operator * (const PT &u, const PT &v) {
  58.     return PT(u.x * v.x - u.y * v.y, u.x * v.y + v.x * u.y);
  59. } // multiplying two complex numbers
  60. inline PT operator * (const PT &u, const double &d) {
  61.     return PT(u.x * d, u.y * d);
  62. }
  63. inline PT operator * (const double &d, const PT &u) {
  64.     return PT(u.x * d, u.y * d);
  65. }
  66. inline PT operator / (const PT &u, const double &d) {
  67.     return PT(u.x / d, u.y / d);
  68. }
  69. //---------basic tools---------//
  70. ll sqnorm(const PT &u) {
  71.     return (u.x * u.x) + (u.y * u.y);
  72. }
  73. double norm(const PT &u) {
  74.     return sqrt(sqnorm(u)); // |u|
  75. }
  76. PT unit_vector(const PT &u) {
  77.     return (u / norm(u)); // u/|u|
  78. }
  79. ll dot(const PT &u, const PT &v) {
  80.     return (u.x * v.x) + (u.y * v.y);
  81. }
  82. ll cross(const PT &u, const PT &v) {
  83.     return (u.x * v.y) - (v.x * u.y);
  84. }
  85. double angle(const PT &u, const PT &v) { // u.v = |u||v|cos(theta)
  86.     return acos(dot(u, v) / (norm(u) * norm(v)));
  87. }
  88. /* r = sqrt(x*x + y*y), theta = atan(y/x) [in radian] */
  89. PT toPolar(const PT &p){
  90.     return PT(norm(p), atan(p.y/p.x)); // {r, theta}
  91. }
  92. /* x = r * cos(theta), y = r * sin(theta) [in radian] */
  93. PT toCartesian(const PT &p){
  94.     return PT(p.x * cos(p.y), p.x * sin(p.y)); // {x, y}
  95. }
  96. PT extend(const PT &u, const PT &v, const double &t) {
  97.     return u + (v - u) * t; // parametric equation
  98. }
  99. PT projection(const PT &u, const PT &v, const PT &p) {
  100.     return u + unit_vector(v - u) * (dot(v - u, p - u) / norm(v - u));
  101. }
  102. PT rtt(const PT &piv, const PT &u, const double &theta) {
  103.     return (u - piv) * toCartesian(PT(1.0, theta)) + piv;
  104. }
  105. //---------being a vector guy---------//
  106. /* area of Parallelogram abc and d = a+b */
  107. ll TriArea(const PT &a, const PT &b, const PT &c){
  108.     return cross(b - a, c - a); // + acw, - ccw, 0 linear
  109. }
  110. /* line ab to point c */
  111. int orientation(const PT &a, const PT &b, const PT &c) {
  112.     ll val = (b.y-a.y)*(c.x-b.x)-(b.x-a.x)*(c.y-b.y);
  113.     if(val == 0) return 0;      // 0 linear
  114.     return val > 0 ? 1 : -1;    // + ccw, - acw
  115. }
  116. /* point c is on segment ab or not */
  117. bool isOnSegment(PT a,PT b,PT c){
  118.     if(orientation(a, b, c)) return false;
  119.     return (c.x >= min(a.x, b.x) && c.x <= max(a.x, b.x))
  120.         && (c.y >= min(a.y, b.y) && c.y <= max(a.y, b.y));
  121. }
  122. /*line ab and line cd is parallel if ab X cd = 0 */
  123. bool ifParallel(const PT &a, const PT &b, const PT &c, const PT &d){
  124.     return abs(cross(a - b, c - d)) < eps;
  125. }
  126. /*line ab and line cd is perpendicular if ab . cd = 0 */
  127. bool ifPerpendicular(const PT &a, const PT &b, const PT &c, const PT &d){
  128.     return abs(dot(a - b, c - d)) < eps;
  129. }
  130. /* segment ab and segment cd intersect or not */
  131. bool isSegIntersect(const PT &a, const PT &b, const PT &c, const PT &d){
  132.     return (orientation(a, b, c) != orientation(a, b, d)
  133.         && orientation(c, d, a) != orientation(c, d, b));
  134. }
  135. PT _f;
  136. /* angular sort _f = arr[0] */
  137. bool CC(const PT &a, const PT &b){
  138.     return (_f.x-a.x)*(_f.y-b.y)<(_f.x-b.x)*(_f.y-a.y);
  139. }
  140. bool CCW(PT &a,PT &b){
  141.     return (_f.x-a.x)*(_f.y-b.y)>(_f.x-b.x)*(_f.y-a.y);
  142. }
  143. /* Radial Sort: _f = any extreme point of hull */
  144. bool ClockCmp(const PT &a, const PT &b){
  145.     ll val = orientation(_f, a, b);
  146.     if(val == 0) return sqnorm(a - _f) < sqnorm(b - _f);
  147.     return val > 0;
  148. }
  149. bool AClockCmp(const PT &a, const PT &b){
  150.     ll val = orientation(_f, a, b);
  151.     if(val == 0) return sqnorm(a - _f) < sqnorm(b - _f);
  152.     return val < 0;
  153. }
  154. //----------------------------------//
  155.  
  156. /* receive a list of points and return their concave polygon */
  157. bool possible; // mark if possible or not
  158. vector< PT > GrahamScanConcave(vector< PT >& v){
  159.     int n = v.size();
  160.     if(n < 3) return v;
  161.     _f = v[0];
  162.     int pivot = 0;
  163.     for(int i=1; i<v.size(); i++){
  164.         if(v[i].y < _f.y) _f = v[i], pivot = i;
  165.         else if(v[i].y == _f.y && v[i].x < _f.x) _f = v[i], pivot = i;
  166.     }
  167.     swap(v[0], v[pivot]);
  168.     sort(v.begin()+1, v.end(), AClockCmp);
  169.     vector< PT > hull;
  170.     hull.push_back(v[0]); hull.push_back(v[1]);
  171.     for(int i=2; i<n; i++){
  172.         if(orientation(hull[hull.size()-2], hull.back(), v[i])) possible = true;
  173.         hull.push_back(v[i]);
  174.     }
  175.     pivot = n-1;
  176.     for(int i=n-2; i>=0; i--){
  177.         if(orientation(hull[0], hull[pivot], hull[i])) break;
  178.         pivot = i;
  179.     }
  180.     reverse(hull.begin()+pivot, hull.end());
  181.     return hull;
  182. }
  183.  
  184. /* receive a hull v and point a, returns if it is inside or not : Ray Casting algorithm */
  185. bool PointInPolygonRay(const vector< PT >& v, PT a){
  186.     int n = v.size(), cnt = 0;
  187.     const ll extra = 123456; // must be greater than points abs value
  188.     PT b = PT(a.x + extra, a.y + extra + 1);
  189.     for(int i=0; i<n; i++){
  190.         if(isOnSegment(v[i], v[(i+1)%n], a)) return true;
  191.         if(isSegIntersect(v[i], v[(i+1)%n], a, b)) cnt++;
  192.     }
  193.     return cnt % 2;
  194. }
  195.  
  196. int main()
  197. {
  198.     #ifdef ERFANUL007
  199.         clock_t tStart = clock();
  200.         freopen("input.txt", "r", stdin);
  201.         freopen("output.txt", "w", stdout);
  202.     #endif
  203.  
  204.     int t, cs=0; scanf("%d", &t);
  205.  
  206.     while(t--){
  207.         int n;
  208.         scanf("%d", &n);
  209.         vector< PT > hull(n);
  210.         for(int i=0; i<n; i++){
  211.             hull[i].input();
  212.         }
  213.         // hull = GrahamScanConcave(hull);
  214.         printf("Case %d:\n", ++cs);
  215.  
  216.         int q; scanf("%d", &q);
  217.         while(q--){
  218.             PT a; a.input();
  219.             if(PointInPolygonRay(hull, a)) printf("Yes\n");
  220.             else printf("No\n");
  221.         }
  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