#P7190. Luka's Traffic Light Journey
Luka's Traffic Light Journey
Luka's Traffic Light Journey
Luka is driving his truck along a road from position \(0\) to \(l\) with \(n\) traffic lights along the way. Each traffic light has a repeating cycle where it stays red for \(r\) seconds and then green for \(g\) seconds. When Luka starts his journey, all lights begin in the red phase at the very start of their cycle.
Luka drives at a speed of 1 unit per second. If he encounters a traffic light that is still red upon arrival, he must wait until it turns green (note that if he arrives exactly when the light switches from red to green, he does not have to wait).
Your task is to calculate how long it will take for Luka to reach the end of the road at position \(l\).
Note: The behavior at each traffic light is as follows:
- Let \(t\) be the current time when Luka arrives at a traffic light located at \(x\).
- If \(t \bmod (r+g) < r\), then the light is red and Luka must wait \(r - (t \bmod (r+g))\) seconds.
- Otherwise, if \(t \bmod (r+g) \geq r\), the light is green and Luka can continue immediately.
inputFormat
The input will start with two integers \(n\) and \(l\) separated by a space, where \(n\) is the number of traffic lights and \(l\) is the length of the road.
This is followed by \(n\) lines, each containing three integers \(x\), \(r\), and \(g\). Here, \(x\) represents the position of the traffic light on the road (\(0 < x < l\)), \(r\) is the duration in seconds that the light remains red, and \(g\) is the duration in seconds that the light remains green.
outputFormat
Output the total time in seconds that Luka requires to reach the end of the road at position \(l\).
sample
1 10
3 2 2
10
</p>