Segments Removal
Time limit4sMemory limit2048 MB
Remove segments on points 1 to x to maximize total score, where each removal gains weight times its exclusively covered points and pays its penalty.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Segment tree, Intervals
- Solved
- No attempts yet
Problem
Consider points on a coordinate axis. You are given a collection of segments that start and end in these points. Each segment has weight and penalty associated with it.
Initially, you have a score of . You can make moves. In each move, you select a segment from the given collection, remove it from the collection, and your score decreases by the penalty associated with the segment. In return, your score increases by the weight of the segment multiplied by the number of points with integer coordinates such that, at this moment of time, this segment is the only segment in the collection that covers these points. A segment is considered to cover its endpoints.
The total score is the sum of scores for the moves you make. Find the maximum total score you can achieve.
Input
The first line contains an integer (), the number of test cases. The test cases follow.
The first line of each test case contains two integers: the number of segments () and the maximum coordinate ().
Next lines contain the description of segments. Each line contains four integers: the start and end of the segment (), its weight () and penalty ().
The sum of over all test cases does not exceed . The sum of over all test cases does not exceed .
Output
For each test case, print a line containing one integer: the maximum possible score you can achieve.