This page is still under construction.

Parts of this page are still being built. What you see may change.

Inlay Cutters

Time limit1sMemory limit128 MB

Summary
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 M×NM \times N 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 MM, NN, and KK, separated by spaces. MM and NN (1≤M,N≤50)(1 \le M, N \le 50) are the dimensions of the plate, and KK (0≤K≤296)(0 \le K \le 296) is the number of cuts. Each of the next KK lines describes one cut. The ii-th cut is given by four integers Xi,1X_{i,1}, Yi,1Y_{i,1}, Xi,2X_{i,2}, Yi,2Y_{i,2}: the start point (Xi,1,Yi,1)(X_{i,1}, Y_{i,1}) and end point (Xi,2,Yi,2)(X_{i,2}, Y_{i,2}) of the cut. Both points lie on the plate's border, are different from each other, and the cut passes through the plate. The XX coordinate ranges from 00 to MM and the YY coordinate ranges from 00 to NN. All cuts are distinct.

Output

Print a single integer: the number of triangles produced by the cuts.

Examples2

  1. Example 1

    Input
    4 4 4
    1 4 1 0
    0 4 4 0
    0 0 4 4
    0 2 4 2
    
    Expected output
    6
    
  2. Example 2

    Input
    7 4 6
    6 0 7 1
    1 4 1 0
    0 4 4 0
    0 0 4 4
    0 2 7 2
    7 0 3 4
    
    Expected output
    8