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

#DSU0000084. Phá tường theo bốn hướng (Cross Explosion)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Phá tường theo bốn hướng (Cross Explosion)

Cross Explosion

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

Consider an HH by WW grid with rows numbered from top to bottom and columns from left to right. Initially, every cell contains exactly one wall.

There are QQ bomb placements, processed in the given order. When a bomb is placed at (R,C)(R,C):

  • If this cell still contains a wall, destroy this wall only.
  • Otherwise, destroy the closest remaining wall in each of the four cardinal directions, looking along the same row or column. A direction with no remaining wall destroys nothing.

When the bomb lands in an empty cell, the four targets are determined simultaneously using the grid state immediately before the explosion. Determine the number of remaining walls after all queries.

Input

The first line contains H,W,QH,W,Q. Each of the next QQ lines contains Ri,CiR_i,C_i.

Output

Print the number of walls remaining.

Subtasks

  • Subtask 1 (20 points): 1≤H,W≤91\le H,W\le 9, HW≤81HW\le 81, 1≤Q≤601\le Q\le 60, 1≤Ri≤H1\le R_i\le H, 1≤Ci≤W1\le C_i\le W.
  • Subtask 2 (30 points): 1≤H,W1\le H,W, HW≤3000HW\le 3000, 1≤Q≤30001\le Q\le 3000, 1≤Ri≤H1\le R_i\le H, 1≤Ci≤W1\le C_i\le W.
  • Subtask 3 (50 points): 1≤H,W1\le H,W, HW≤4⋅105HW\le 4\cdot10^5, 1≤Q≤2⋅1051\le Q\le 2\cdot10^5, 1≤Ri≤H1\le R_i\le H, 1≤Ci≤W1\le C_i\le W.

Example

Input

2 4 3
1 2
1 2
1 3

Output

2

Explanation

The first bomb removes (1,2)(1,2). The second removes (1,1)(1,1), (1,3)(1,3) and (2,2)(2,2). The last bomb removes (1,4)(1,4) and (2,3)(2,3). Two walls remain.