Library-Python

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub Rin204/Library-Python

src/data_structure/ConvexHullTrick.py

Verified with

Code

from collections import deque


class ConvexHullTrick:
    def __init__(self):
        self.deq = deque()

    def check(f1, f2, f3):
        return (f2[0] - f1[0]) * (f3[1] - f2[1]) >= (f2[1] - f1[1]) * (f3[0] - f2[0])

    def f(self, _f, x):
        return _f[0] * x + _f[1]

    def add_line(self, a, b):
        """
        add  f_i(x) = a * x + b
        """
        f = (a, b)
        while len(self.deq) >= 2 and ConvexHullTrick.check(self.deq[-2], self.deq[-1], f):
            self.deq.pop()
        self.deq.append(f)

    def get(self, x):
        """
        return min_i f_i(x)
        """
        while len(self.deq) >= 2 and self.f(self.deq[0], x) >= self.f(self.deq[1], x):
            self.deq.popleft()

        return self.f(self.deq[0], x)
Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.11.4/x64/lib/python3.11/site-packages/onlinejudge_verify/documentation/build.py", line 81, in _render_source_code_stat
    bundled_code = language.bundle(
                   ^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.4/x64/lib/python3.11/site-packages/onlinejudge_verify/languages/python.py", line 108, in bundle
    raise NotImplementedError
NotImplementedError
Back to top page