In the primary school maths class, Rock gave you this problem:
- Count the number of rectangles in an $n \times m$ grid. (vertical by horizontal) Here, squares are regarded as rectangles.
As a 華夏盃 contestant, you instakilled this problem and started yawning. Rock doesn't like your attitude and decided to increase the difficulty of the problem:
- Now $k$ 2-by-2 squares in the grid are merged.
But again, as a 華夏盃 contestant, you still find this problem easy. Exert the superiority of a 華夏盃 contestant by solving this problem.
For example, the following image shows the scenario of $n=m=3$ and $(x_1, y_1)=(1,2)$
Input
The first line of the input contains three integers: $n, m, k$.
The following $k$ lines of the input each contains 2 integers: $x_i, y_i$,
representing the coordinates of the top left corner of the $i$-th merged 2-by-2 square.
Output
Output a single integer: the number of squares in the grid.
Constraints
For all cases, $2 \le n, m \le 10^9, 4 \le n \times m \le 10^9, 0 \le k \le 10, 1 \le x_i \lt n, 1 \le y_i \lt m$
Also, the merged squares do not overlap, i.e. $|x_i - x_j| \ge 2 \text{ or } |y_i-y_j| \ge 2$ for all $1 \le i, j \le k, i \neq j$
Subtask 1 (13%): $k=0, 2 \le n, m \le 40$
Subtask 2 (19%): $2 \le n, m \le 40$
Subtask 3 (15%): $k=0$
Subtask 4 (17%): $k=1$
Subtask 5 (27%): $k=2$
Subtask 6 (9%): No additional constraints
Sample Test Cases
Input | Output | |
---|---|---|
3 3 1 1 2 |
15 | |
This case is describe in the task statement. | ||
4 2 2 1 1 3 1 |
3 | |
4 4 1 2 2 |
52 | |
4 7 2 1 2 2 6 |
127 |
Scoring: Per Subtask
Authored by s17r28
Appeared in 2023 Wah Yan Interschool Olympiad in Informatics 🤯🥷⚡🧠🏆