Advertisement
nullzero

Convex Hull

Sep 18th, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.57 KB | None | 0 0
  1. struct point{
  2.     ll x, y;
  3.     point(){}
  4.     point(ll _x, ll _y ){
  5.         x = _x;
  6.         y = _y;
  7.     }
  8. };
  9.  
  10. deque<point> H;
  11.  
  12. ll cross(point a, point b, point c){
  13.     return (b.x - a.x) * (c.y - a.y) -
  14.            (c.x - a.x) * (b.y - a.y);
  15. }
  16.  
  17. ll eval(point p, int x){
  18.     return p.x * x + p.y;
  19. }
  20.  
  21. void hull_add(point p) {
  22.     while(H.size() > 1 && cross(H[H.size() - 2], H[H.size() - 1], p) >= 0) H.pop_back();
  23.     H.push_back(p);
  24. }
  25.  
  26. ll hull_query(int x) {
  27.     while(H.size() > 1 and eval(H[0], x) >= eval(H[1], x)) H.pop_front();
  28.     return eval(H[0], x);
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement