Advertisement
tiom4eg

point in polygon wa5

Apr 4th, 2022
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.29 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. #define pii pair <int, int>
  43. int rand(int n) { return rng() % n + 1; }
  44. int randrange(int l, int r) { return rng() % (r - l) + l; } // >= l && <= r
  45. // can you phil mahar
  46. struct pt { int x, y; };
  47. struct ang { int a, b; };
  48. bool operator<(const ang& p, const ang& q) { return ((!p.b && !q.b) ? (p.a < q.a) : (p.a * q.b < p.b * q.a)); }
  49. int sq(const pt& a, const pt& b, const pt& c) { return a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y); }
  50. bool on(const pt& a, const pt& b, const pt& x) { return min(a.x, b.x) <= x.x <= max(a.x, b.x) && ((x.x - a.x) * (b.y - a.y) - (x.y - a.y) * (b.x - a.x) == 0); }
  51. int area(const vector <pt>& p) { // get double area of figure
  52.     int n = sz(p);
  53.     int s = 0;
  54.     FOR(i, 0, n) s += p[i].x * p[i == n - 1 ? 0 : i + 1].y - p[i].y * p[i == n - 1 ? 0 : i + 1].x;
  55.     return abs(s);
  56. }
  57. struct polygon {
  58.     vector <pt> p;
  59.     vector <ang> a;
  60.     int ar, st = 0, n;
  61.     pt start;
  62.     void calc(vector <pt>& c) {
  63.         n = sz(c);
  64.         p.resize(n);
  65.         FOR(i, 0, n) {
  66.             p[i] = c[i];
  67.             if (p[i].x < p[st].x || p[i].x == p[st].x && p[i].y < p[st].y) st = i;
  68.         }
  69.         ar = area(p);
  70.         start = p[st];
  71.         rotate(p.begin(), p.begin() + st, p.end());
  72.         p.erase(p.begin());
  73.         n--;
  74.         a.resize(n);
  75.         FOR(i, 0, n) {
  76.             a[i].a = p[i].y - start.y, a[i].b = p[i].x - start.x;
  77.             if (!a[i].a) a[i].b = a[i].b < 0 ? -1 : 1;
  78.         }
  79.     }
  80.     bool in(const pt& q) {
  81.         if (q.x < start.x) return false;
  82.         if (q.x == start.x && q.y == start.y) return true;
  83.         ang now = {q.y - start.y, q.x - start.x};
  84.         if (!now.a) now.b = now.b < 0 ? -1 : 1;
  85.         auto it = upper_bound(all(a), now);
  86.         if (it == a.end() && now.a == a[n - 1].a && now.b == a[n - 1].b) --it;
  87.         if (it != a.end() && it != a.begin()) {
  88.             int pos = (int)(it - a.begin());
  89.             if (on(p[pos], p[pos - 1], q)) return false;
  90.             if (sq(p[pos], p[pos - 1], q) <= 0) return true;
  91.         }
  92.         return false;
  93.     }
  94. };
  95.  
  96. signed main() {
  97.     fastIO;
  98.     cout << fixed << setprecision(6);
  99.     int n; cin >> n;
  100.     vector <polygon> pol(n);
  101.     FOR(i, 0, n) {
  102.         int s; cin >> s;
  103.         vector <pt> f(s);
  104.         FOR(i, 0, s) cin >> f[i].x >> f[i].y;
  105.         pol[i].calc(f);
  106.     }
  107.     sort(all(pol), [](const polygon& a, const polygon& b){ return a.ar > b.ar; });
  108.     int m; cin >> m;
  109.     int res = 0;
  110.     FOR(i, 0, m) {
  111.         pt q; cin >> q.x >> q.y;
  112.         if (pol[0].in(q)) {
  113.             int l = 0, r = n;
  114.             while (l < r - 1) {
  115.                 int mid = (l + r) / 2;
  116.                 if (pol[mid].in(q)) l = mid;
  117.                 else r = mid;
  118.             }
  119.             if (l == n - 1) res += pol[l].ar;
  120.             else res += pol[l].ar - pol[r].ar;
  121.         }
  122.     }
  123.     cout << (long double)(res) / 2.0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement