Oil
InterviewTime limit10sMemory limit512 MB
Given up to 2000 horizontal segments at distinct points, find the maximum total length of segments intersected by a single non-horizontal line from the origin.
- Level
Medium7 of 10
- Topics
- Geometry, Sorting, Brute force, Binary search
- Solved
- No attempts yet
Problem
An oil company drills one well to bring oil up from deposits buried in the earth. A newly found deposit is rarely a single body. It usually splits into many parts that lie in layers.
The company simplifies the situation to a 2-dimensional model. Each deposit is a horizontal segment parallel to the surface, and the well is drilled from the surface along a straight line. The well takes oil from every deposit it touches on the way down, and touching a deposit at an endpoint of its segment still empties that whole deposit. The oil held by a deposit equals the width of the deposit, so the yield of a well is the sum of the widths of the deposits it touches.
The well goes downward from the surface, so it is never horizontal.
Find the largest amount of oil one well can extract.

Figure 1: oil layers buried in the earth. The picture matches the first example input.
Input
The first line contains an integer (), the number of deposits. Each of the next lines contains three integers , , and describing one deposit: the deposit is the segment whose endpoints are and , where is the depth below the surface. The values satisfy and . No two deposits share a point. Note that may be larger than , and a deposit with has width 0.
Output
Print one integer, the maximum amount of oil that one well can extract.