Li Chao Tree
Overview
The Li Chao tree is named after its inventor, the great god 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 godlike 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
// 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;
}