Caterpillar Walk
InterviewTime limit1sMemory limit1024 MB
Given non-overlapping buildings on the x-axis, find the total distance Lulu crawls along roofs and walls from x=0 to x=100.
- Level
Easy3 of 10
- Topics
- Implementation, Array
- Solved
- No attempts yet
Problem
Consider a city where each building is a rectangle with its base on the positive x-axis, its two walls parallel to the positive y-axis, and its roof parallel to the x-axis. Two buildings may touch each other, so they can stand right next to each other. However, no two buildings may share any area, meaning they do not intersect or overlap.
Lulu, our beloved caterpillar, lives at and works at . Every morning, Lulu crawls from home to the office. Without any buildings, the trip is a distance of 100. The buildings force Lulu to crawl up and down the walls, which adds to the distance. Lulu starts at home and ends at the office, and is always crawling on the x-axis, up the side of a wall, across the top of a building, or down the side of a wall.
Given the description of all the buildings, compute the total distance Lulu travels.
Input
The first line contains an integer (), the number of buildings. Each of the next lines describes one building with three integers: (), (), and (). Here is the x-coordinate of the lower-left corner, is the width, and is the height. It is guaranteed that , so no building extends beyond .
Output
Print the total distance Lulu travels.
Hint
The picture below shows the last input example.
