Crazy Dreamoon
Time limit1sMemory limit512 MB
Given up to 2000 line segments in a 2000 by 2000 grid, count the grid squares whose interior is crossed by at least one segment.
- Level
Hard8 of 10
- Topics
- Geometry, Math, Implementation, Sorting
- Solved
- No attempts yet
Problem
Dreamoon likes algorithm competitions very much. When he feels crazy because he cannot figure out any solution for any problem in a competition, he often draws many meaningless straight line segments on his calculation paper.
Dreamoon's calculation paper is special. It can be imagined as the plane with a Cartesian coordinate system where the coordinates range over . The grid lines are all lines of the form or for every integer between and , inclusive. So the grid contains squares.
Now Dreamoon wonders how many grid squares are crossed by at least one of the lines he drew. Help Dreamoon find the answer. Touching an edge of a grid square is not considered as crossing this square.
Input
The first line of input contains an integer denoting the number of lines Dreamoon drew. The -th line of the following lines contains four integers , denoting that the -th segment Dreamoon drew is a straight line segment between points and .
Output
Output one integer on a single line: how many grid squares are crossed by at least one of the line segments which Dreamoon drew.
Constraints
- the lengths of all line segments in input are non-zero