A Li Chao tree (also known as a Dynamic Convex Hull or Segment Tree with lazy propagations) is a data structure that allows for efficient dynamic maintenance of the convex hull of a set of points in a 2D plane. The Li Chao tree allows for dynamic insertion, deletion, and query operations on the set of points, and can be used in a variety of geometric problems such as line segment intersection, visibility graphs, and polygon triangulation.
The structure of a Li Chao tree is a segment tree, a balanced binary tree data structure where each leaf node represents a single point, and each internal node represents a convex hull of the points represented by its child nodes.
The tree is balanced to ensure that each leaf node is at roughly the same depth, which allows for logarithmic time complexity for operations such as insertion and deletion.
To go further one should begin with some geometric utility functions as given below:
C++
structline{longdoublem,b;// This overloads the () operator,// allowing the struct to be used as a// function that takes in a value x and// returns the result of m * x + b.longdoubleoperator()(longdoublex){returnm*x+b;}};vector<line>a(N*4);
classLine{constructor(m,b){this.m=m;this.b=b;}// This overloads the () operator,// allowing the struct to be used as a// function that takes in a value x and// returns the result of m * x + b.call(x){returnthis.m*x+this.b;}}consta=newArray(N*4).fill(null).map(()=>newLine(0,0));
On every node of the segment tree, we are storing the line that maximizes (or minimizes) the value of the mid. If the interval of the node is [L, R), then the line stored in it will maximize(or minimize). \frac{L+R}{2}
Insert: Let's say we are inserting a new line to the node which is corresponding to Interval [L, R).To make it easy let's assume that the line on the node has a smaller slope. So, mid =\ frac{L + R}{2}. Now we have two cases to think about.
Note: Red is the original line in the node
Case 1: red(mid) < blue(mid)
In this Case, We have to replace red line with blue.Now, should we remove red? The answer is No, because there is need of red in segment [L, mid). Because of this reason we should pass red to the node with interval [L, mid), which is its left son.
representation of case 1
Case 2: red(mid) > blue(mid)
In similar way, we should pass blue to its right son and keep red in this node, whose interval is [mid, R).
representation of case 2
Below is the code for the above approach:
Code block
C++
voidinsert(intl,intr,linesegment,intidx=0){// if the range represented by the current node is just// one elementif(l+1==r){// if the segmentment to be inserted has a greater// slope than the one in the current node, replace// itif(segment(l)>a[idx](l))a[idx]=segment;return;}// find the middle of the rangeintmid=(l+r)/2;// calculate the indices of the left and right child// nodesintleftson=idx*2+1,rightson=idx*2+2;// if the segmentment to be inserted has a smaller slope// than the one in the current node, swap themif(a[idx].m>segment.m)swap(a[idx],segment);// if the segmentment in the current node has a smaller// value at the middle point than the segmentment to be// insertedif(a[idx](mid)<segment(mid)){// swap the segmentments in the current node and the// left child nodeswap(a[idx],segment);// insert the segmentment into the left child nodeinsert(l,mid,segment,leftson);}// otherwise, insert the segmentment into the right// child nodeelseinsert(mid,r,segment,rightson);}
Java
voidinsert(intl,intr,Linesegment,intidx){// if the range represented by the current node is just// one elementif(l+1==r){// if the segment to be inserted has a greater slope// than the one in the current node, replace itif(segment.get(l)>a[idx].get(l)){a[idx]=segment;}return;}// find the middle of the rangeintmid=(l+r)/2;// calculate the indices of the left and right child// nodesintleftson=idx*2+1,rightson=idx*2+2;// if the segment in the current node has a smaller// slope than the segment to be inserted, swap themif(a[idx].m>segment.m)swap(a[idx],segment);// if the segment in the current node has a smaller// value at the middle point than the segment to be// insertedif(a[idx].get(mid)<segment.get(mid)){// swap the segments in the current node and the// left child nodeswap(a[idx],segment);// insert the segment into the left child nodeinsert(l,mid,segment,leftson);}// otherwise, insert the segment into the right child// nodeelse{insert(mid,r,segment,rightson);}}
Python3
definsert(l,r,segment,idx=0):# if the range represented by the current node is just# one elementifl+1==r:# if the segmentment to be inserted has a greater# slope than the one in the current node, replace# itifsegment(l)>a[idx](l):a[idx]=segmentreturn# find the middle of the rangemid=(l+r)/2# calculate the indices of the left and right child# nodesleftson=idx*2+1rightson=idx*2+2# if the segmentment to be inserted has a smaller slope# than the one in the current node, swap themifa[idx].m>segment.m:swap(a[idx],segment)# if the segmentment in the current node has a smaller# value at the middle point than the segmentment to be# insertedifa[idx](mid)<segment(mid):# swap the segmentments in the current node and the# left child nodeswap(a[idx],segment)# insert the segmentment into the left child nodeinsert(l,mid,segment,leftson)# otherwise, insert the segmentment into the right# child nodeelse:insert(mid,r,segment,rightson)# This code is contribtued by Tapesh(tapeshdua420)
C#
voidInsert(intl,intr,Linesegment,intidx){// if the range represented by the current node is just// one elementif(l+1==r){// if the segment to be inserted has a greater slope// than the one in the current node, replace itif(segment.Get(l)>a[idx].Get(l)){a[idx]=segment;}return;}// find the middle of the rangeintmid=(l+r)/2;// calculate the indices of the left and right child// nodesintleftson=idx*2+1,rightson=idx*2+2;// if the segment in the current node has a smaller// slope than the segment to be inserted, swap themif(a[idx].m>segment.m)Swap(a[idx],segment);// if the segment in the current node has a smaller// value at the middle point than the segment to be// insertedif(a[idx].Get(mid)<segment.Get(mid)){// swap the segments in the current node and the// left child nodeSwap(a[idx],segment);// insert the segment into the left child nodeInsert(l,mid,segment,leftson);}// otherwise, insert the segment into the right child// nodeelse{Insert(mid,r,segment,rightson);}}// This code is contributed by Tapesh(tapeshdua420)
JavaScript
functioninsert(l,r,segment,idx=0){// if the range represented by the current node is just// one elementif(l+1===r){// if the segmentment to be inserted has a greater// slope than the one in the current node, replace// itif(segment(l)>a[idx](l)){a[idx]=segment;}return;}// find the middle of the rangeletmid=(l+r)/2;// calculate the indices of the left and right child// nodesletleftson=idx*2+1;letrightson=idx*2+2;// if the segmentment to be inserted has a smaller slope// than the one in the current node, swap themif(a[idx].m>segment.m){swap(a[idx],segment);}// if the segmentment in the current node has a smaller// value at the middle point than the segmentment to be// insertedif(a[idx](mid)<segment(mid)){// swap the segmentments in the current node and the// left child nodeswap(a[idx],segment);// insert the segmentment into the left child nodeinsert(l,mid,segment,leftson);}// otherwise, insert the segmentment into the right// child nodeelse{insert(mid,r,segment,rightson);}}// This code is contribtued by Tapesh(tapeshdua420)
Code for queries, as we know we only need to consider the intervals that contain the point we need to ask from the way we inserted lines.
C++
longdoublequery(intl,intr,intx,intidx=0){// If the range of the query is only// one element, return the result of// the function a at index o applied to xif(l+1==r)returna[idx](x);// Find the middle of the rangeintmid=(l+r)/2;// Find the index of the left and right// children of the current nodeintleftson=idx*2+1;intrightson=idx*2+2;// If the value being queried is less// than the middle of the range,// recursively call query on// the left childif(x<mid)returnmax(a[idx](x),query(l,mid,x,leftson));// Otherwise, recursively call query// on the right childelsereturnmax(a[idx](x),query(mid,r,x,rightson));}
Java
publicdoublequery(intl,intr,intx,intidx){// If the range of the query is only// one element, return the result of// the function a at index o applied to xif(l+1==r){returna[idx](x);}// Find the middle of the rangeintmid=(l+r)/2;// Find the index of the left and right// children of the current nodeintleftson=idx*2+1;intrightson=idx*2+2;// If the value being queried is less// than the middle of the range,// recursively call query on// the left childif(x<mid){returnMath.max(a[idx](x),query(l,mid,x,leftson));}// Otherwise, recursively call query// on the right childelse{returnMath.max(a[idx](x),query(mid,r,x,rightson));}}// This code is contributed by Tapesh(tapeshdua420)
Python3
defquery(l,r,x,idx=0):# If the range of the query is only# one element, return the result of# the function a at index o applied to xifl+1==r:returna[idx](x)# Find the middle of the rangemid=(l+r)//2# Find the index of the left and right# children of the current nodeleftson=idx*2+1rightson=idx*2+2# If the value being queried is less# than the middle of the range,# recursively call query on# the left childifx<mid:returnmax(a[idx](x),query(l,mid,x,leftson))# Otherwise, recursively call query# on the right childelse:returnmax(a[idx](x),query(mid,r,x,rightson))# This code is contributed by ik_9
C#
usingSystem;publicclassProgram{// Define the delegate type for the functions in the 'a' arraypublicdelegatelongdoubleFunction(intx);// Define the 'a' array of functionspublicstaticFunction[]a=newFunction[10];// Adjust the size as neededpublicstaticlongdoubleQuery(intl,intr,intx,intidx=0){// If the range of the query is only one element, return the result of// the function 'a' at index 'idx' applied to 'x'if(l+1==r){returna[idx](x);}// Find the middle of the rangeintmid=(l+r)/2;// Find the index of the left and right children of the current nodeintleftSon=idx*2+1;intrightSon=idx*2+2;// If the value being queried is less than the middle of the range,// recursively call Query on the left childif(x<mid){returnMath.Max(a[idx](x),Query(l,mid,x,leftSon));}// Otherwise, recursively call Query on the right childelse{returnMath.Max(a[idx](x),Query(mid,r,x,rightSon));}}publicstaticvoidMain(string[]args){// Example usage// Initialize the 'a' array with functionsa[0]=(intx)=>(longdouble)x;// Replace with your own functionsa[1]=(intx)=>(longdouble)(x*x);a[2]=(intx)=>(longdouble)(x+1);longdoubleresult=Query(0,10,5);// Example queryConsole.WriteLine("Result: "+result);}}
JavaScript
functionquery(l,r,x,idx=0){// If the range of the query is only// one element, return the result of// the function a at index o applied to xif(l+1===r){returna[idx](x);}// Find the middle of the rangeletmid=Math.floor((l+r)/2);// Find the index of the left and right// children of the current nodeletleftson=idx*2+1;letrightson=idx*2+2;// If the value being queried is less// than the middle of the range,// recursively call query on// the left childif(x<mid){returnMath.max(a[idx](x),query(l,mid,x,leftson));}// Otherwise, recursively call query// on the right childelse{returnMath.max(a[idx](x),query(mid,r,x,rightson));}}// This code is contributed by Tapesh(tapeshdua420)
Time Complexity: O(NLogN), for the construction of the tree and O(LogN) for each Query Auxiliary Space: O(N)
Advantages of Li Chao tree:
Dynamic Convex Hull: The Li Chao tree is a dynamic data structure that can handle line segments and can be used to maintain the convex hull of a set of lines in real-time.
Fast Queries: Li Chao tree has a logarithmic time complexity for querying the minimum value on a given line segment, making it efficient for large datasets.
Space-efficient: Li Chao tree uses a balanced binary tree structure, which is space-efficient.
Disadvantages of the Li Chao tree:
Complexity: The construction of the Li Chao tree takes O(n log n) time and O(n) space, which can be computationally expensive for large datasets.
Limited to lines: Li Chao tree can only handle line segments and not other types of geometric shapes.
Limited to 2D: Li Chao tree is limited to 2-dimensional space and cannot be used for 3D or higher-dimensional problems.
Limited to linear functions: Li Chao tree is limited to linear functions and cannot be used for non-linear functions.