Matrix_code

geo - Convex hull - simple

Mar 23rd, 2020 (edited)
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef pair<long long, bool> answer;
  5. struct rect {
  6.   long long p, q, idx;
  7.   long long eval(long long x) { return p * x + q; }
  8. };
  9.  
  10. bool bad(rect a, rect b, rect c) {
  11.   return (c.q - a.q) * (b.p - a.p) >= (b.q - a.q) * (c.p - a.p);
  12. }
  13. struct cht {
  14.   int beg;
  15.   vector<rect> ch;
  16.  
  17.   void insert(rect r) {
  18.     while (ch.size() >= 2 && bad(ch[ch.size() - 2], ch.back(), r))
  19.       ch.pop_back();
  20.  
  21.     while (!ch.empty() && ch.back().p == r.p)
  22.       ch.pop_back();
  23.  
  24.     ch.push_back(r);
  25.   }
  26.  
  27.   answer eval(long long x) {
  28.     if (beg == ch.size())
  29.       return {0, false};
  30.  
  31.     while (beg + 1 < ch.size() && ch[beg + 1].eval(x) >= ch[beg].eval(x))
  32.       ++beg;
  33.  
  34.     return {ch[beg].eval(x), true};
  35.   }
  36. };
Add Comment
Please, Sign In to add comment