Advertisement
tiom4eg

point in polygon WIP

Apr 4th, 2022
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.61 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. // tiom4eg's precompiler options
  3. // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
  4. // IO settings
  5. #define fastIO ios_base::sync_with_stdio(false); cin.tie(0)
  6. // Quick types
  7. #define ll long long
  8. //#define ld double
  9. #define ull unsigned long long
  10. #define pii pair <int, int>
  11. #define pll pair <ll, ll>
  12. #define vi vector <int>
  13. #define mi vector <vector <int> >
  14. // Quick functions
  15. #define endl "\n"
  16. #define F first
  17. #define S second
  18. #define all(a) a.begin(), a.end()
  19. #define sz(a) a.size()
  20. #define pb push_back
  21. #define mp make_pair
  22. //#define min(a, b) ((a < b) ? (a) : (b))
  23. //#define max(a, b) ((a > b) ? (a) : (b))
  24. // Quick fors
  25. #define FOR(i, a, b) for (int i = a; i < b; ++i)
  26. #define RFOR(i, a, b) for (int i = a; i >= b; --i)
  27. // Pragmas
  28. #pragma GCC optimize("O3,unroll-loops") // let the chaos begin!
  29. //#pragma GCC target("avx,avx2,bmi,bmi2,popcnt,lzcnt,tune=native")
  30. #pragma GCC comment(linker, "/stack:200000000")
  31. // PBDS
  32. #include <ext/pb_ds/assoc_container.hpp>
  33. #include <ext/pb_ds/tree_policy.hpp>
  34. #define ordered_set tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update>
  35. #define ook order_of_key
  36. #define fbo find_by_order
  37. // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
  38. using namespace std;
  39. using namespace __gnu_pbds;
  40. mt19937 rng(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
  41. #define int long long
  42. // can you phil mahar
  43. int sign(int x) {
  44.     if (x > 0) return 1;
  45.     if (x < 0) return -1;
  46.     return 0;
  47. }
  48. struct pt {
  49.     int x, y;
  50.     pt() {}
  51.     pt(int _x, int _y) : x(_x), y(_y) {}
  52.     pt operator+(const pt &p) const { return pt(x + p.x, y + p.y); }
  53.     pt operator-(const pt &p) const { return pt(x - p.x, y - p.y); }
  54.     int operator*(const pt& oth) const { return x * oth.x + y * oth.y; }
  55.     int operator^(const pt& oth) const { return x * oth.y - y * oth.x; }
  56.     bool operator<(const pt& oth) const {
  57.         if (sign(x - oth.x)) return x < oth.x;
  58.         if (sign(y - oth.y)) return y < oth.y;
  59.         return false;
  60.     }
  61.     bool operator==(const pt& oth) const { return !sign(x - oth.x) && !sign(y - oth.y);  }
  62. };
  63. bool ccw(const pt& a, const pt& b, const pt& c) {
  64.     return ((a.x - c.x) * (b.y - c.y) - (a.y - c.y) * (b.x - c.x)) > 0;
  65. }
  66. bool in_angle(const pt& a, const pt& b, const pt& c, const pt& x) {
  67.     return (ccw(c, a, x) && !ccw(c, b, x)) || (!ccw(c, a, x) && ccw(c, b, x));
  68. }
  69. bool in_triangle(const pt& a, const pt& b, const pt& c, const pt& x) {
  70.     return x == a || x == b || x == c || in_angle(a, b, c, x) && in_angle(c, a, b, x);
  71. }
  72. bool on_segment(const pt& a, const pt& b, const pt& x) {
  73.     return (sign((x - a).x) != sign((x - b).x) && (x - a).x * (x - b).y == (x - a).y * (x - b).x);
  74. }
  75. int area(const vector <pt>& p) {
  76.     int n = sz(p);
  77.     int s = 0;
  78.     FOR(i, 0, n) s += p[i] ^ p[i == n - 1 ? 0 : i + 1];
  79.     return abs(s);
  80. }
  81.  
  82. struct polygon {
  83.     vector <pt> p;
  84.     int n, ar;
  85.     void init(vector <pt>& f) {
  86.         n = sz(f), ar = area(f);
  87.         int pos = 0;
  88.         FOR(i, 1, n) if (f[i] < f[pos]) pos = i;
  89.         p.resize(n);
  90.         FOR(i, 0, n - pos) p[i] = f[pos + i];
  91.         FOR(i, n - pos, n) p[i] = f[i - (n - pos)];
  92.     }
  93.     bool in(const pt& x) {
  94.         if (p[0] == x) return true;
  95.         if (!in_angle(p[1], p[n - 1], p[0], x)) return false;
  96.         int l = 0, r = n - 2;
  97.         while (l < r - 1) {
  98.             int m = (l + r) / 2;
  99.             if (in_angle(p[l + 1], p[m + 2], p[0], x)) l = m;
  100.             else r = m;
  101.         }
  102.         //cout << l << ' ' << r << endl;
  103.         //cout << p[l + 1].x << ' ' << p[l + 1].y << endl;
  104.         //cout << p[l + 2].x << ' ' << p[l + 2].y << endl;
  105.         return in_triangle(p[l + 1], p[l + 2], p[0], x);
  106.     }
  107. };
  108. /*
  109. 1
  110. 4
  111. 2 2
  112. -2 2
  113. -2 -2
  114. 2 -2
  115. 1
  116. -2 2
  117. */
  118.  
  119. signed main() {
  120.     fastIO;
  121.     cout << fixed << setprecision(6);
  122.     int n; cin >> n;
  123.     vector <polygon> pol(n);
  124.     FOR(i, 0, n) {
  125.         int s; cin >> s;
  126.         vector <pt> f(s);
  127.         FOR(i, 0, s) cin >> f[i].x >> f[i].y;
  128.         pol[i].init(f);
  129.     }
  130.     sort(all(pol), [](const polygon& a, const polygon& b){ return a.ar > b.ar; });
  131.     int m; cin >> m;
  132.     int res = 0;
  133.     FOR(i, 0, m) {
  134.         pt q; cin >> q.x >> q.y;
  135.         if (pol[0].in(q)) {
  136.             int l = 0, r = n;
  137.             while (l < r - 1) {
  138.                 int mid = (l + r) / 2;
  139.                 if (pol[mid].in(q)) l = mid;
  140.                 else r = mid;
  141.             }
  142.             if (l == n - 1) res += pol[l].ar;
  143.             else res += pol[l].ar - pol[r].ar;
  144.         }
  145.     }
  146.     cout << (long double)(res) / 2.0L;
  147. }
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement