Stop & Go
InterviewTime limit2sMemory limit1024 MB
Given N traffic lights on a road of length L, find the time Mr. D reaches point L, stopping at each red light he meets.
- Level
Medium4 of 10
- Topics
- Simulation, Sorting, Implementation
- Solved
- No attempts yet
Problem
Mr. Drive, also known as Mr. D, always drives at exactly the legal speed. If a traffic light turns red from green as he enters a crossing, he stops at once. When a light turns green from red, he starts at once and drives at the legal speed.
His next course is a straight road of length with a legal speed limit of 1 per second. He starts at time 0 from the start point and drives at speed 1 per second. The road has traffic lights numbered 1 through . Light is at distance from the start. At time 0 all lights are green. Light turns red after seconds of green, turns green again after seconds of red, turns red after another seconds of green, and so on.
When Mr. D reaches , if light is green or has just turned green from red, he passes at speed 1 without stopping. If light is red or has just turned red from green, he stops until light turns green.
Compute the time when Mr. D reaches point .
Input
The first line has two integers () and (). Each of the next lines has three integers , , and (, , ). All positions are distinct.
Output
Print the time in seconds when Mr. D reaches point , as a single integer.