# Convex Hull Trick

Author: Andi Qu

### Prerequisites

A way to find the maximum or minimum value of several convex functions at given points.

We wish to solve problems that are of the following form:

Consider a set of functions ${f_{i}(x)}$ on some range $[l,r]$ such that for any two functions $f_{i}$ and $f_{j}$, there exists some $m$ such that

- For all $x∈[l,m]$, $f_{i}(x)≤f_{j}(x)$
- For all $x∈[m,r]$, $f_{i}(x)≥f_{j}(x)$
Answer queries of the form "what is the maximum/minimum $f_{i}(x)$ for some given $x∈[l,r]$," given that we have a way of finding $m$ efficiently.

A sufficient (but not necessary) set of conditions:

- All the functions are continuous along the range $[l,r]$.
- No two functions intersect at more than one point.

The most common case is where each $f_{i}(x)$ is of the form $a_{i}x+b_{i}$. Given two lines $f_{i}(x)=a_{i}x+b_{i}$ and $f_{j}(x)=a_{j}x+b_{j}$ such that $a_{i}<a_{j}$, their intersection point at $m=a_{j}−a_{i}b_{i}−b_{j} $ can be found in $O(1)$ time. Then it's clear that

- For all $x≤m$, $f_{i}(x)≥f_{j}(x)$.
- For all $x≥m$, $f_{i}(x)≤f_{j}(x)$.

The linear case is known as the **convex hull trick** because $max_{i}(f_{i}(x))$ as a function of $x$ is concave up (similarly, $min_{i}(f_{i}(x))$ as a function of $x$ is concave down). Check the images from the CF tutorial below if you don't know what this means.

Some possible nonlinear forms of $f_{i}(x)$:

- $f_{i}(x)=x_{2}+a_{i}x+b_{i}$
- Reduces to the linear case, since we can ignore the $x_{2}$ term when comparing two functions.

- $f_{i}(x)=x−a_{i} +b_{i}$
- If this function is defined for all $x∈[l,r]$.
- Note that when $a_{i}<a_{j}$, $x−a_{i} −x−a_{j} $ is strictly decreasing over the range $x∈[a_{j},∞)$.

In this module, we'll focus on the special case of CHT where "slopes" of functions are monotonic. This specific case is solvable in $O(N)$ using a `std::deque`

in C++. For the more general $O(NgN)$ CHT (which involves a `std::set`

), see the LineContainer module.

Focus Problem – read through this problem before continuing!

## Tutorial

Resources | |||
---|---|---|---|

CF | solves problem above | ||

GCP | |||

Jeffrey Xiao |

## Solution - The Fair Nut and Rectangles

I won't analyse this problem in great detail since the Codeforces blog in the resources already does so, but essentially, we sort the rectangles by $x$-coordinate and get the following DP recurrence:

$dp[i]=p_{i}⋅q_{i}−a_{i}+j<imax (−p_{j}⋅q_{i}+dp[j])$Notice how the $−p_{j}⋅q_{i}+dp[j]$ part of the recurrence describes a straight line $y=mx+c$.

Since we sorted the rectangles and no two rectangles are nested, the slopes of the lines we insert are strictly increasing. The query positions are also strictly increasing.

This means we can solve this problem using CHT in $O(N)$ time! Here is my implementation:

#include <bits/stdc++.h>typedef long long ll;using namespace std;struct Rect {ll x, y, a;bool operator<(Rect B) { return x < B.x; }};Rect a[1000001];

## Problems

### Alternative Solution

Some of these problems can also be solved using divide & conquer.

Status | Source | Problem Name | Difficulty | Tags | Solution | URL |
---|---|---|---|---|---|---|

APIO | Easy | ## Show TagsDP, convex | External Sol | |||

CEOI | Easy | ## Show TagsDP, convex | View Solution | |||

CEOI | Normal | ## Show TagsDP, convex | External Sol | |||

IOI | Normal | ## Show TagsDP, convex | External Sol | |||

APIO | Normal | ## Show TagsDP, convex | ||||

POI | Normal | ## Show TagsDP, convex | ||||

POI | Normal | ## Show TagsDP, convex | View Solution | |||

Plat | Normal | ## Show TagsDP, convex | ||||

Plat | Normal | ## Show Tagsconvex | External Sol | |||

JOI | Hard | ## Show TagsDP, convex | View Solution |

### Module Progress:

### Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!

## Give Us Feedback on Convex Hull Trick!

Leave suggestions for us by making a post on the USACO Forum! Ex: Unclear sections, mislabeled problems, etc.