Advertisement
Dang_Quan_10_Tin

Static Convex Hull Trick

Nov 8th, 2020 (edited)
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. // Bài yêu cầu tìm nhỏ nhất nhớ duyệt hệ số giảm dần và chỉnh B[line.back()] < B[i] thành B[line.back()] > B[i]
  2.  
  3. struct ConvexHullTrick
  4. {
  5.     vector<ll> A, B;
  6.     vector<int> line;
  7.     vector<ld> point;
  8.     ConvexHullTrick(int n = 0)
  9.     {
  10.         A.resize(n + 2, 0);
  11.         B.resize(n + 2, 0);
  12.         point.emplace_back(-Inf);
  13.     }
  14.  
  15.     ld ff(int x, int y)
  16.     {
  17.         return (ld)1.0 * (B[y] - B[x]) / (A[x] - A[y]);
  18.     }
  19.  
  20.     void Add(int i)
  21.     {
  22.         while ((int)line.size() > 1 || ((int)line.size() == 1 && A[line.back()] == A[i]))
  23.         {
  24.             if (A[line.back()] == A[i])
  25.             {
  26.                 if (B[line.back()] < B[i])
  27.                 {
  28.                     line.pop_back();
  29.                     if (!line.empty())
  30.                         point.pop_back();
  31.                 }
  32.                 else
  33.                     break;
  34.             }
  35.             else
  36.             {
  37.                 if (ff(i, line.back()) <= ff(i, line[line.size() - 2]))
  38.                 {
  39.                     line.pop_back();
  40.                     if (!line.empty())
  41.                         point.pop_back();
  42.                 }
  43.                 else
  44.                     break;
  45.             }
  46.         }
  47.  
  48.         if (line.empty() || A[line.back()] != A[i])
  49.         {
  50.             if (!line.empty())
  51.                 point.emplace_back(ff(line.back(), i));
  52.             line.emplace_back(i);
  53.         }
  54.     }
  55.  
  56.     ll More(int x)
  57.     {
  58.         return ;
  59.     }
  60.  
  61.     ll X(int x)
  62.     {
  63.         return ;
  64.     }
  65.  
  66.     ll Get(int x)
  67.     {
  68.         int j = lower_bound(point.begin(), point.end(), X(x)) - point.begin();
  69.         return A[line[j - 1]] * X(x) + B[line[j - 1]] + More(x);
  70.     }
  71. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement