Advertisement
TrickmanOff

C task

May 3rd, 2020
722
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.83 KB | None | 0 0
  1. #pragma optimization_level 3
  2. //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
  3. #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <fstream>
  7. #include <vector>
  8. #include <queue>
  9. #include <functional>
  10. #include <set>
  11. #include <map>
  12. #include <math.h>
  13. #include <cmath>
  14. #include <string>
  15. #include <random>
  16. #include <unordered_set>
  17. #include <unordered_map>
  18. #include <bitset>
  19. #include <string.h>
  20. #include <stack>
  21. #include <assert.h>
  22. #include <list>
  23. #include <time.h>
  24. #include <memory>
  25. #include <chrono>
  26. using namespace std;
  27. //
  28. #define fast cin.tie(0);cout.tie(0);cin.sync_with_stdio(0);cout.sync_with_stdio(0);
  29. #define cin in
  30. //#define cout out
  31. #define ll long long
  32. #define db double
  33. #define ld long double
  34. #define uset unordered_set
  35. #define umap unordered_map
  36. #define F first
  37. #define S second
  38. #define ms multiset
  39. #define pb push_back
  40. #define pq priority_queue
  41. #define umap unordered_map
  42. #define uset unordered_set
  43. #define ull unsigned long long
  44. #define pii pair<int, int>
  45. #define pll pair<ll, ll>
  46. #define pdd pair<ld, ld>
  47. #define pnn pair<Node*, Node*>
  48. #define uid uniform_int_distribution
  49. #define PI acos(-1.0)
  50. //#define sort(a, b) sort(a.begin(), a.end(), b())
  51. //mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
  52. ifstream in("input.txt");
  53. ofstream out("output.txt");
  54.  
  55. struct pnt {
  56.     int x, y;
  57. };
  58.  
  59. int n, k;
  60. const int MAX_N = 1000;
  61.  
  62. vector<pnt> n_pnts, k_pnts;
  63. bool used[MAX_N];
  64. vector<vector<pnt>> hulls;
  65.  
  66. int cw(pnt a, pnt b, pnt c) {
  67.     ll s = (ll)(a.x - b.x) * (a.y - c.y) - (a.y - b.y) * (a.x - c.x);
  68.     return -(s == 0 ? 0 : s / abs(s));
  69. }
  70.  
  71. class Compare {
  72. public:
  73.     bool operator()(const pnt& a, const pnt& b) const {
  74.         return a.x < b.x || (a.x == b.x && a.y < b.y);
  75.     }
  76. };
  77.  
  78. void build_hull() {
  79.     memset(used, 0, sizeof(used));
  80.     pnt p0 = n_pnts[0], pn = n_pnts.back();
  81.     vector<int> upper = { 0 }, lower = {0 };
  82.    
  83.     for (int i = 1; i < n_pnts.size(); i++) {
  84.         if (used[i]) continue;
  85.  
  86.         pnt p = n_pnts[i];
  87.  
  88.         if (i == n_pnts.size() - 1 || cw(p0, p, pn) == 1) {
  89.             while (upper.size() >= 2 && cw(n_pnts[upper[upper.size() - 2]], n_pnts[upper.back()], p) <= 0)
  90.                 upper.pop_back();
  91.             upper.push_back(i);
  92.         }
  93.         if (i == n_pnts.size() - 1 || cw(p0, p, pn) == -1){
  94.             while (lower.size() >= 2 && cw(n_pnts[lower[lower.size() - 2]], n_pnts[lower.back()], p) >= 0)
  95.                 lower.pop_back();
  96.             lower.push_back(i);
  97.         }
  98.     }
  99.    
  100.     hulls.push_back(vector<pnt>());
  101.     for (int x : upper) {
  102.         used[x] = 1;
  103.         hulls.back().push_back(n_pnts[x]);
  104.     }
  105.     for (int i = lower.size() - 2; i > 0; i--) {
  106.         used[lower[i]] = 1;
  107.         hulls.back().push_back(n_pnts[lower[i]]);
  108.     }
  109.  
  110.     int p = 0;
  111.     for (int i = 0; i < n_pnts.size(); i++) {
  112.         if (!used[i])
  113.             n_pnts[p++] = n_pnts[i];
  114.     }
  115.     n_pnts.resize(p);
  116. }
  117.  
  118. void build_hulls() {
  119.     sort(n_pnts.begin(), n_pnts.end(), Compare());
  120.     while (!n_pnts.empty())
  121.         build_hull();
  122. }
  123.  
  124. bool is_in_tr(pnt a, pnt b, pnt c, pnt x) {
  125.     return cw(a, b, x) >= 0 && cw(b, c, x) >= 0 && cw(c, a, x) >= 0;
  126. }
  127.  
  128. bool is_on_line(pnt a, pnt b, pnt x) {
  129.     return cw(a, b, x) == 0 && x.x >= a.x && x.x <= b.x;
  130. }
  131.  
  132. bool is_in_rect(int rect, pnt x) {
  133.     vector<pnt>& pnts = hulls[rect];
  134.     pnt p0 = pnts[0];
  135.  
  136.     //1st upper
  137.     int l = -1, r = pnts.size();
  138.     while (r - l > 1) {
  139.         int m = (l + r) / 2;
  140.         if (cw(p0, pnts[m], x) >= 0) l = m;
  141.         else r = m;
  142.     }
  143.  
  144.     if (l == -1) return 0;
  145.  
  146.     if (l == pnts.size() - 1) {
  147.         if (is_on_line(p0, pnts[l], x)) return 1;
  148.     }
  149.     else if (is_in_tr(p0, pnts[l], pnts[l + 1], x)) return 1;
  150.    
  151.     return 0;
  152. }
  153.  
  154. int find_zone(pnt x) {
  155.  
  156. }
  157.  
  158. void input() {
  159.     cin >> n;
  160.     n_pnts.resize(n);
  161.     for (pnt& x : n_pnts) cin >> x.x >> x.y;
  162.  
  163.     cin >> k;
  164.     k_pnts.resize(k);
  165.     for (pnt& x : k_pnts) cin >> x.x >> x.y;
  166. }
  167.  
  168. int main() {
  169.     fast;
  170.     input();
  171.     build_hulls();
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement