This page is still under construction.

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

Super Paintball

Interview

Time limit1sMemory limit128 MB

Summary
Given up to 100000 opponent positions on an N by N grid, count cells from which a shot along its row, column, or either diagonal would pass through every opponent.
Level

Medium6 of 10

Topics
Brute force, Implementation, Array, Math
Solved
No attempts yet

Problem

Bessie is playing a paintball game on a square field. The field is divided into an N×NN \times N grid of unit cells (1≤N≤1001 \le N \le 100). There are KK opponents (1≤K≤100,0001 \le K \le 100{,}000); opponent ii stands in the cell at row RiR_i and column CiC_i (1≤Ri≤N1 \le R_i \le N, 1≤Ci≤N1 \le C_i \le N). Several opponents may stand in the same cell.

Bessie's paintball gun fires in any of eight directions: up, down, left, right, and the four diagonals (up-left, up-right, down-left, down-right). She hits an opponent if that opponent lies in the same row, the same column, or on one of the two diagonals through her cell. She can also hit an opponent that shares her own cell.

Bessie will stand in exactly one cell. Count how many of the N×NN \times N cells she may choose so that, from that cell, she can hit every one of the KK opponents.

Input

  • Line 1: two space-separated integers NN and KK.
  • Lines 2 to K+1K+1: line i+1i+1 contains two space-separated integers RiR_i and CiC_i, the row and column of opponent ii.

Output

  • Line 1: a single integer — the number of distinct cells Bessie may occupy so that she can hit every opponent.

Hint

Consider a field with 44 rows and 44 columns and opponents at (2,1)(2,1), (2,3)(2,3), and (4,1)(4,1) (C marks an opponent):

. . . .
C . C .
. . . .
C . . .

From each of the cells (2,1)(2,1), (2,3)(2,3), (3,2)(3,2), (4,1)(4,1), and (4,3)(4,3) Bessie can hit all three opponents, so the answer is 55. Below, B marks a valid cell for Bessie and * marks a cell that is valid and also shared with an opponent:

. . . .        . . . .
B . B .        * . * .
. B . .   =>   . B . .
B . B .        * . B .

Examples3

  1. Example 1

    Input
    4 3
    2 1
    2 3
    4 1
    
    Expected output
    5
    
  2. Example 2

    Input
    1 1
    1 1
    
    Expected output
    1
    
  3. Example 3

    Input
    3 1
    1 1
    
    Expected output
    7