PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000144. Quân xe trên lưới (Rook on Grid)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Quân xe trên lưới (Rook on Grid)

Rook on Grid

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

Consider an H×WH\times W grid, with rows numbered top-to-bottom and columns left-to-right. There are MM distinct blocked cells, and (1,1)(1,1) is guaranteed to be empty.

A rook starts at (1,1)(1,1). In one move, it may travel right or down through one or more consecutive unblocked cells along its current row or column; it cannot jump over or land on an obstacle. Moving left or up is forbidden.

Count the cells reachable in at most two moves, including the starting cell. Count each cell only once, even if there are several paths.

Input

The first line contains H,W,MH,W,M. The next MM lines each contain one obstacle location Xi,YiX_i,Y_i.

Output

Print the number of cells reachable in at most two moves.

Subtasks

  • Subtask 1 (20 points): 1≤H,W≤201\le H,W\le 20; 0≤M≤min⁡(399,HW−1)0\le M\le\min(399,HW-1); 1≤Xi≤H1\le X_i\le H; 1≤Yi≤W1\le Y_i\le W; (Xi,Yi)≠(1,1)(X_i,Y_i)\ne(1,1); các vị trí chướng ngại đôi một khác nhau.
  • Subtask 2 (30 points): 1≤H,W≤20001\le H,W\le 2000; 0≤M≤min⁡(5000,HW−1)0\le M\le\min(5000,HW-1); 1≤Xi≤H1\le X_i\le H; 1≤Yi≤W1\le Y_i\le W; (Xi,Yi)≠(1,1)(X_i,Y_i)\ne(1,1); các vị trí chướng ngại đôi một khác nhau.
  • Subtask 3 (50 points): 1≤H,W≤2000001\le H,W\le 200000; 0≤M≤min⁡(200000,HW−1)0\le M\le\min(200000,HW-1); 1≤Xi≤H1\le X_i\le H; 1≤Yi≤W1\le Y_i\le W; (Xi,Yi)≠(1,1)(X_i,Y_i)\ne(1,1); các vị trí chướng ngại đôi một khác nhau.

Examples

Example 1

Input

4 3 2
2 2
3 3

Output

10

Explanation

The grid has 1212 cells; exactly (2,2)(2,2) and (3,3)(3,3) are blocked. All other 1010 cells can be reached by a right-then-down or down-then-right route of at most two moves.

Example 2

Input

5 4 4
3 2
3 4
4 2
5 2

Output

14

Explanation

Of the 2020 cells, 44 are blocked. The two additional free cells (4,4)(4,4) and (5,4)(5,4) cannot be reached in at most two allowed moves. Therefore the answer is 20−4−2=1420-4-2=14.