Deforestation
Time limit2sMemory limit2048 MB
Given N trees on a number line and K intervals that each must keep at least t trees, find the maximum number of trees that can be cut down.
- Level
Medium6 of 10
- Topics
- Greedy, Sorting, Binary search, Union-find
- Solved
- No attempts yet
Problem
Farmer John is expanding his farm! He has identified the perfect location in the Red-Black Forest, which consists of trees () on a number line, with the -th tree at position ().
Environmental protection laws restrict which trees Farmer John can cut down to make space for his farm. There are restrictions () specifying that there must always be at least trees () in the line segment , including the endpoints (). It is guaranteed that the Red-Black Forest initially satisfies these restrictions.
Farmer John wants to make his farm as big as possible. Please help him compute the maximum number of trees he can cut down while still satisfying all the restrictions.
Input
Each input consists of () independent test cases. It is guaranteed that the sums of all and of all within an input each do not exceed .
The first line of input contains . Each test case is then formatted as follows:
The first line contains integers and .
The next line contains the integers .
Each of the next lines contains three space-separated integers: , and .
Output
For each test case, output a single line with an integer denoting the maximum number of trees Farmer John can cut down.
Hint
For the first test case, Farmer John can cut down the first trees, leaving trees at to satisfy the restriction.
For the second test case, the additional restriction does not affect which trees Farmer John can cut down, so he can cut down the same trees and satisfy both restrictions.
For the third test case, Farmer John can only cut down at most trees because there are initially trees but the second restriction requires him to leave at least trees uncut.