LCC '22 Contest 5 J5 - Knight Fork

View as PDF

Submit solution

Points: 7 (partial)
Time limit: 4.0s
Memory limit: 256M

Author:
Problem type

Fred Lever just learned that knights in Chess are very good at forking, which is when they attack multiple pieces simultaneously. To practice this skill, he uses a N by N board with M rooks on it. For simplicity, the top-left cell is denoted as (1, 1) and the bottom-right as (N, N). Every rook is placed on a distinct cell (r_i, r_j) on the board. Fred wants to know how many cells he can place a knight on the board that forks two or more rooks, without the knight being attacked (or on the same cell of a rook).

Remember, knights in Chess may move from a cell (x, y) to cells (x \pm 1, y \pm 2) or cells (x \pm 2, y \pm 1) and rooks may move from a cell (x, y) to cells (x+k, y) or cells (x, y+k) where k is a nonzero integer. However, all pieces must stay on the board.

.....
.....
.R.R.
.....
..K..

The above figure is an example of a knight (K) forking two rooks (R) on a 5 by 5 board. Note that the knight can also fork the rooks if it were placed at (1, 3).

Constraints

For all subtasks,

2 \le N \le 10^9

2 \le M < \min(10^5, N^2)

1 \le r_i, r_j \le N

all (r_i, r_j) are pairwise distinct.

Subtask 1 [25%]

2 \le N \le 2 \times 10^3

Subtask 2 [75%]

No additional constraints.

Input Specification

The first line will contain two integers N and M, space-separated.

The next M lines will contain two integers r_i and r_j, denoting the location of a rook on the board.

Sample Input 1

5 2
3 2
3 4

Sample Output 1

2

Explanation for Sample Output 1

This is the example in the problem statement.

Sample Input 2

6 4
4 3
4 5
4 1
3 4

Sample Output 2

3

Explanation for Sample Output 2

......
.K...K
...R..
R.R.R.
......
.K....

The above figure shows where knights can be placed to fork two or more rooks. Note that (6, 4) is not a valid cell as the knight would be attacked by the rook on (3, 4).


Comments

There are no comments at the moment.