fork(16) download
  1. #include<set>
  2. #include<cmath>
  3. /*
  4.  * Dynamic version of data structure
  5.  * to be used in dynamic programming optimisation
  6.  * called "Convex Hull trick"
  7.  * 'Dynamic' means that there is no restriction on adding lines order
  8.  */
  9. class ConvexHullDynamic
  10. {
  11. typedef long long coef_t;
  12. typedef long long coord_t;
  13. typedef long long val_t;
  14.  
  15. /*
  16.   * Line 'y=a*x+b' represented by 2 coefficients 'a' and 'b'
  17.   * and 'xLeft' which is intersection with previous line in hull(first line has -INF)
  18.   */
  19. private:
  20. struct Line
  21. {
  22. coef_t a, b;
  23. double xLeft;
  24.  
  25. enum Type {line, maxQuery, minQuery} type;
  26. coord_t val;
  27.  
  28. explicit Line(coef_t aa=0, coef_t bb=0) : a(aa), b(bb), xLeft(-INFINITY), type(Type::line), val(0) {}
  29. val_t valueAt(coord_t x) const { return a*x+b; }
  30. friend bool areParallel(const Line& l1, const Line& l2) { return l1.a==l2.a; }
  31. friend double intersectX(const Line& l1, const Line& l2) { return areParallel(l1,l2)?INFINITY:1.0*(l2.b-l1.b)/(l1.a-l2.a); }
  32. bool operator<(const Line& l2) const
  33. {
  34. if (l2.type == line)
  35. return this->a < l2.a;
  36. if (l2.type == maxQuery)
  37. return this->xLeft < l2.val;
  38. if (l2.type == minQuery)
  39. return this->xLeft > l2.val;
  40. }
  41. };
  42.  
  43. private:
  44. bool isMax; //whether or not saved envelope is top(search of max value)
  45. std::set<Line> hull; //envelope itself
  46.  
  47. private:
  48. /*
  49.   * INFO: Check position in hull by iterator
  50.   * COMPLEXITY: O(1)
  51.   */
  52. bool hasPrev(std::set<Line>::iterator it) { return it!=hull.begin(); }
  53. bool hasNext(std::set<Line>::iterator it) { return it!=hull.end() && std::next(it)!=hull.end(); }
  54.  
  55. /*
  56.   * INFO: Check whether line l2 is irrelevant
  57.   * NOTE: Following positioning in hull must be true
  58.   * l1 is next left to l2
  59.   * l2 is right between l1 and l3
  60.   * l3 is next right to l2
  61.   * COMPLEXITY: O(1)
  62.   */
  63. bool irrelevant(const Line& l1, const Line& l2, const Line& l3) { return intersectX(l1,l3) <= intersectX(l1,l2); }
  64. bool irrelevant(std::set<Line>::iterator it)
  65. {
  66. return hasPrev(it) && hasNext(it)
  67. && ( isMax && irrelevant(*std::prev(it), *it, *std::next(it))
  68. || !isMax && irrelevant(*std::next(it), *it, *std::prev(it)) );
  69. }
  70.  
  71. /*
  72.   * INFO: Updates 'xValue' of line pointed by iterator 'it'
  73.   * COMPLEXITY: O(1)
  74.   */
  75. std::set<Line>::iterator updateLeftBorder(std::set<Line>::iterator it)
  76. {
  77. if (isMax && !hasPrev(it) || !isMax && !hasNext(it))
  78. return it;
  79.  
  80. double val = intersectX(*it, isMax?*std::prev(it):*std::next(it));
  81. Line buf(*it);
  82. it = hull.erase(it);
  83. buf.xLeft = val;
  84. it = hull.insert(it, buf);
  85. return it;
  86. }
  87.  
  88. public:
  89. explicit ConvexHullDynamic(bool isMax): isMax(isMax) {}
  90.  
  91. /*
  92.   * INFO: Adding line to the envelope
  93.   * Line is of type 'y=a*x+b' represented by 2 coefficients 'a' and 'b'
  94.   * COMPLEXITY: Adding N lines(N calls of function) takes O(N*log N) time
  95.   */
  96. void addLine(coef_t a, coef_t b)
  97. {
  98. //find the place where line will be inserted in set
  99. Line l3 = Line(a, b);
  100. auto it = hull.lower_bound(l3);
  101.  
  102. //if parallel line is already in set, one of them becomes irrelevant
  103. if (it!=hull.end() && areParallel(*it, l3))
  104. {
  105. if (isMax && it->b < b || !isMax && it->b > b)
  106. it = hull.erase(it);
  107. else
  108. return;
  109. }
  110.  
  111. //try to insert
  112. it = hull.insert(it, l3);
  113. if (irrelevant(it)) { hull.erase(it); return; }
  114.  
  115. //remove lines which became irrelevant after inserting line
  116. while (hasPrev(it) && irrelevant(std::prev(it))) hull.erase(std::prev(it));
  117. while (hasNext(it) && irrelevant(std::next(it))) hull.erase(std::next(it));
  118.  
  119. //refresh 'xLine'
  120. it = updateLeftBorder(it);
  121. if (hasPrev(it))
  122. updateLeftBorder(std::prev(it));
  123. if (hasNext(it))
  124. updateLeftBorder(std::next(it));
  125. }
  126. /*
  127.   * INFO: Query, which returns max/min(depends on hull type - see more info above) value in point with abscissa 'x'
  128.   * COMPLEXITY: O(log N), N-amount of lines in hull
  129.   */
  130. val_t getBest(coord_t x) const
  131. {
  132. Line q;
  133. q.val = x;
  134. q.type = isMax ? Line::Type::maxQuery : Line::Type::minQuery;
  135.  
  136. auto bestLine = hull.lower_bound(q);
  137. if (isMax) --bestLine;
  138. return bestLine->valueAt(x);
  139. }
  140. };
  141.  
  142.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
/usr/lib/gcc/i586-linux-gnu/5/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty