Advertisement
Guest User

Untitled

a guest
Aug 18th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. bool qu=0;
  2. struct line
  3. {
  4. long long m,b;
  5. mutable function<const line*()> succ;
  6. bool operator<(const line& rhs) const
  7. {
  8. if (!qu)
  9. return (m<rhs.m);
  10. const line* s=succ();
  11. if (!s)
  12. return 0;
  13. return b-s->b<rhs.m*(s->m-m);
  14. }
  15. };
  16. struct hull:public multiset<line>
  17. {
  18. bool bad(iterator y)
  19. {
  20. auto z=next(y);
  21. if (y==begin())
  22. {
  23. if (z==end())
  24. return 0;
  25. return (y->m==z->m && y->b<=z->b);
  26. }
  27. auto x=prev(y);
  28. if (z==end())
  29. return (y->m==x->m && y->b<=x->b);
  30. return 1.0*(x->b-y->b)*(z->m-y->m)>=1.0*(y->b-z->b)*(y->m-x->m);
  31. }
  32. void add(long long m,long long b)
  33. {
  34. auto it=insert({m,b});
  35. it->succ=[=] { return (next(it)==end())? 0:&*next(it); };
  36. if (bad(it))
  37. {
  38. erase(it);
  39. return;
  40. }
  41. while (next(it)!=end() && bad(next(it)))
  42. erase(next(it));
  43. while (it!=begin() && bad(prev(it)))
  44. erase(prev(it));
  45. }
  46. long long eval(long long x)
  47. {
  48. qu=1;
  49. line l=*lower_bound((line){x,0});
  50. qu=0;
  51. return l.m*x+l.b;
  52. }
  53. };
  54. hull h;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement