Problem 563
Car Pooling
Medium · 20 pts · Array, Prefix Sum, Sorting
Problem
A car with C empty seats drives east along a road and never turns back. Each trip (p, from, to) means p passengers get in at kilometre from and get out at kilometre to. Passengers leaving at a kilometre get out before new passengers get in there. Decide whether the car can complete all trips without ever carrying more than C passengers. Add p at from and subtract p at to in a difference array, then check every running total.
Input
The first line contains N and C. Each of the next N lines contains p, from and to for one trip.
Output
Print YES if all trips can be completed, otherwise NO.
Constraints
1 <= N <= 1000; 1 <= p <= 100; 0 <= from < to <= 1000; 1 <= C <= 100000
Example input
2 4 2 1 5 3 3 7
Example output
NO
Explanation
With capacity 4, the trips (2 passengers from 1 to 5) and (3 passengers from 3 to 7) overlap between kilometres 3 and 5, where the car would hold 5 people. So the answer is NO.