---
id: 682
lang: en
translation_of: index.md
source_sha256: 94a41085b853fe8d9a765d5602f45b19732cfa3cdc54dcacbde5a003912d67e4
title: "Li Chao Tree"
description: "A short introduction to the Li Chao tree, a segment tree for dynamically maintaining a convex hull of lines, with code for BZOJ 1568."
tags: ["OI","Segment Tree","Li Chao Tree","Convex Hull"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "The author's backslash in 上\\下 and 最高\\最低 (used as a slash) is kept as written: upper\\lower, highest\\lowest."
  - "杭州学军中学 rendered as Hangzhou Xuejun High School."
---

## Overview

The Li Chao tree is named after its inventor, <span class="slang" data-zh="大神" data-gloss="dàshén, “great god”. An admiring title for a highly skilled person, an expert.">the great god</span> Li Chao of Hangzhou Xuejun High School. This data structure is mainly used to dynamically maintain an (upper\lower) convex hull. It is a really <span class="slang" data-zh="神仙" data-gloss="shénxiān, “immortal”. Someone, or a problem or idea, so brilliant it seems superhuman.">godlike</span> thing, and the main difficulty lies in handling the lazy tags on the segment tree.

## Algorithm Code and Explanation

The algorithm has the following functions:

- INTERSECT(k1, k2, b1, b2): finds the x-coordinate of the intersection point of two lines.
- UPDATE(k, b, l, r, p): adds a line to the Li Chao tree.
- QUERY(qx, l, r, p): asks for the height of the y-coordinate when the x-coordinate is $qx$.

Let's first think about the UPDATE operation. Consider the following situations:

- If we find that this interval has no line stored, we just assign it directly.
- If we find that there is already a line there, compute the values of the two lines at the two endpoints: the value of the line to be inserted at $l$ is $ff$ and at $r$ is $fb$, and the value of the line it is compared against at $l$ is $gf$ and at $r$ is $gb$. We consider the following cases:
  - If the two lines are parallel (that is, when $ff,fb$ and $gf,gb$ have the same ordering), we just compare heights and take the highest\lowest line.
  - If the two lines intersect, we find the x-coordinate $intx$ of the intersection point, then consider each side separately and update the left and right children.

## [Example Problem](https://www.lydsy.com/JudgeOnline/problem.php?id=1568)

```cpp
// BZ1568.cpp
#include <bits/stdc++.h>
#define mid ((l + r) >> 1)
#define lson (p << 1)
#define rson ((p << 1) | 1)
using namespace std;
const int MAX_Q = 100020, MAX_N = 50010, ENDPOINT = 50000;
int n, tmpx;
char opt[10];
double lnk[MAX_N << 2], lnb[MAX_N << 2], tmpw, tmpz;
bool tag[MAX_N << 2];
double intersect(double k1, double b1, double k2, double b2) { return 1.0 * (b2 - b1) / (k1 - k2); }
void update(double k, double b, int l, int r, int p)
{
    if (tag[p])
    {
        double ff = k * l + b, gf = lnk[p] * l + lnb[p], fb = k * r + b, gb = lnk[p] * r + lnb[p];
        if (ff <= gf && fb <= gb)
            return;
        else if (ff >= gf && fb >= gb)
        {
            lnk[p] = k, lnb[p] = b;
            return;
        }
        double intx = intersect(k, b, lnk[p], lnb[p]);
        if (ff >= gf)
            if (intx <= mid)
                update(k, b, l, mid, lson);
            else
                update(lnk[p], lnb[p], mid + 1, r, rson), lnk[p] = k, lnb[p] = b;
        else if (intx > mid)
            update(k, b, mid + 1, r, rson);
        else
            update(lnk[p], lnb[p], l, mid, lson), lnk[p] = k, lnb[p] = b;
    }
    else
        lnk[p] = k, lnb[p] = b, tag[p] = true;
}
double query(int qx, int l, int r, int p)
{
    double ans = 0;
    if (tag[p])
        ans = max(ans, 1.0 * qx * lnk[p] + lnb[p]);
    if (l == r)
        return ans;
    if (qx <= mid)
        ans = max(ans, query(qx, l, mid, lson));
    else
        ans = max(ans, query(qx, mid + 1, r, rson));
    return ans;
}
int main()
{
    scanf("%d", &n);
    while (n--)
    {
        scanf("%s", opt + 1);
        if (opt[1] == 'Q')
            scanf("%d", &tmpx), printf("%d\n", (int)(query(tmpx, 1, ENDPOINT, 1) / 100.0));
        else
            scanf("%lf%lf", &tmpw, &tmpz), update(tmpz, tmpw - tmpz, 1, ENDPOINT, 1);
    }
    return 0;
}
```
