The Robot Plow
InterviewTime limit1sMemory limit128 MB
Given up to 200 axis-aligned rectangles on a grid at most 240 by 240, count the number of unit squares covered by at least one rectangle.
- Level
Easy3 of 10
- Topics
- Implementation, Simulation, Array, Brute force
- Solved
- No attempts yet
Problem
Farmer John has purchased a new robotic plow to relieve himself from the drudgery of plowing field after field after field. The plow does the job, but with one restriction: it can only plow a perfect rectangle whose sides have integer length.
Because FJ's field has trees and other obstacles, he programs the plow to plow many different rectangles, which may overlap. He is curious how many squares of his field are actually plowed after all of the instructions run. Each instruction describes a rectangle by giving the , coordinates of its lower-left and upper-right corners.
The field is partitioned into squares whose sides are parallel to the and axes. The field is squares wide and squares high (; ). Each of the instructions () consists of four integers , , , (; ), the lower-left and upper-right coordinates of the rectangle to plow. The plow plows every square in the range , inclusive of both endpoint columns and rows.
Consider a field that is 6 squares wide and 4 squares high. As FJ issues a pair of plowing instructions (shown), the field gets plowed as shown by '*' and '#' (already-plowed squares all look the same, but '#' marks the most recently plowed ones):
...... **.... #####.
...... (1,1)(2,4) **.... (1,3)(5,4) #####.
...... **.... **....
...... **.... **....
A total of 14 squares are plowed.
Input
- Line 1: Three space-separated integers: , , and
- Lines 2..I+1: Line contains plowing instruction , described by four integers: , , ,
Output
- Line 1: A single integer, the total number of squares plowed