Lampice
Time limit3sMemory limit1024 MB
Count axis-aligned rectangles with integer corners on an n by m terrace where, for each colour, both lamps are inside or both are outside.
- Level
Hard8 of 10
- Topics
- Prefix sum, Dynamic programming, Math
- Solved
- No attempts yet
Problem
Christmas is coming! Teo has already decided to decorate his terrace.
Teo has a big rectangular terrace. It is meters long and meters wide. Instead of hanging Christmas lights on the edges of the terrace, he will put them on the floor.
Teo has lamps, two for each of colours. He will put each lamp at some position , where is the distance from the left side of the terrace and is the distance from the bottom side.
Proud of how he decorated the terrace, he decided to take the rest of his day off. Soon he got bored and returned to the terrace. He started counting nice rectangles on the terrace. A rectangle is nice if for each colour, both lamps are either inside or outside of the rectangle. A lamp located on the edge of the rectangle counts as inside.

The left rectangle is not nice. One blue lamp is inside the rectangle and one is outside. The right rectangle is nice. The red and blue lamps are inside, and the yellow lamps are outside.
Teo has realized that counting nice rectangles is not an easy job. He wants to know how many nice rectangles there are whose corners have integer distances from the bottom and left sides of the terrace. All rectangles considered are parallel to the sides of the terrace. Your task is to count the nice rectangles.
Input
The first line contains three integers , , (, , ), the length and the width of the terrace, and the number of lamp colours.
The next lines contain four numbers , , , (, ), the positions of the first and the second lamp of the -th colour.
Output
In a single line, output the number of nice rectangles.
Hint
Clarification of the first example: the image shows all nice rectangles from the first example.
