Inlay Cutters
Time limit1sMemory limit128 MB
Count all 45-degree right isosceles triangles formed by grid-aligned cuts and diagonals on an M by N plate after K straight cuts.
- Level
Hard8 of 10
- Topics
- Geometry, Implementation, Brute force, Combinatorics
- Solved
- No attempts yet
Problem

A factory cuts a rectangular granite plate into pieces using a special machine that can cut in four different directions: vertically, horizontally, and along the two diagonals at 45° to the sides of the plate. Every cut is a straight line whose start and end points both lie on the sides of the plate.
The factory has been asked to produce inlay tiles, each of which is a 45° right isosceles triangle. To save delivery time, all such triangles are to be collected from plates that have already been cut. Given information about every cut that was performed, compute the number of triangles (of any size) produced by the cuts.
A side of a triangle may lie on a cut or on the outer edge of the plate.
Input
The first line contains three integers , , and , separated by spaces. and are the dimensions of the plate, and is the number of cuts. Each of the next lines describes one cut. The -th cut is given by four integers , , , : the start point and end point of the cut. Both points lie on the plate's border, are different from each other, and the cut passes through the plate. The coordinate ranges from to and the coordinate ranges from to . All cuts are distinct.
Output
Print a single integer: the number of triangles produced by the cuts.