#abc370d. D - Cross Explosion

D - Cross Explosion

Score : 400400 points

问题陈述

有一个 HHWW 列的网格。用 (i,j)(i, j) 表示从顶部数第 ii 行,从左边数第 jj 列的单元格。 最初,每个单元格中都有一堵墙。 在按照给定顺序处理了 QQ 个查询后,找出剩余的墙的数量。

在第 qq 个查询中,你将得到两个整数 RqR_qCqC_q。 你在 (Rq,Cq)(R_q, C_q) 放置一个炸弹来摧毁墙。结果,会发生以下过程。

  • 如果 (Rq,Cq)(R_q, C_q) 处有墙,摧毁那堵墙并结束过程。
  • 如果 (Rq,Cq)(R_q, C_q) 处没有墙,摧毁从 (Rq,Cq)(R_q, C_q) 向上、向下、向左和向右看时首次出现的墙。更准确地说,同时发生以下四个过程:
    • 如果存在一个 i<Rqi < R_q 使得 (i,Cq)(i, C_q) 处有墙,并且对于所有 i<k<Rqi < k < R_q(k,Cq)(k, C_q) 处没有墙,摧毁 (i,Cq)(i, C_q) 处的墙。
    • 如果存在一个 i>Rqi > R_q 使得 (i,Cq)(i, C_q) 处有墙,并且对于所有 Rq<k<iR_q < k < i(k,Cq)(k, C_q) 处没有墙,摧毁 (i,Cq)(i, C_q) 处的墙。
    • 如果存在一个 j<Cqj < C_q 使得 (Rq,j)(R_q, j) 处有墙,并且对于所有 j<k<Cqj < k < C_q(Rq,k)(R_q, k) 处没有墙,摧毁 (Rq,j)(R_q, j) 处的墙。
    • 如果存在一个 j>Cqj > C_q 使得 (Rq,j)(R_q, j) 处有墙,并且对于所有 Cq<k<jC_q < k < j(Rq,k)(R_q, k) 处没有墙,摧毁 (Rq,j)(R_q, j) 处的墙。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

There is a grid with HH rows and WW columns. Let (i,j)(i, j) denote the cell at the ii-th row from the top and jj-th column from the left.
Initially, there is one wall in each cell.
After processing QQ queries explained below in the order they are given, find the number of remaining walls.

In the qq-th query, you are given two integers RqR_q and CqC_q.
You place a bomb at (Rq,Cq)(R_q, C_q) to destroy walls. As a result, the following process occurs.

  • If there is a wall at (Rq,Cq)(R_q, C_q), destroy that wall and end the process.
  • If there is no wall at (Rq,Cq)(R_q, C_q), destroy the first walls that appear when looking up, down, left, and right from (Rq,Cq)(R_q, C_q). More precisely, the following four processes occur simultaneously:
    • If there exists an i<Rqi \lt R_q such that a wall exists at (i,Cq)(i, C_q) and no wall exists at (k,Cq)(k, C_q) for all i<k<Rqi \lt k \lt R_q, destroy the wall at (i,Cq)(i, C_q).
    • If there exists an i>Rqi \gt R_q such that a wall exists at (i,Cq)(i, C_q) and no wall exists at (k,Cq)(k, C_q) for all Rq<k<iR_q \lt k \lt i, destroy the wall at (i,Cq)(i, C_q).
    • If there exists a j<Cqj \lt C_q such that a wall exists at (Rq,j)(R_q, j) and no wall exists at (Rq,k)(R_q, k) for all j<k<Cqj \lt k \lt C_q, destroy the wall at (Rq,j)(R_q, j).
    • If there exists a j>Cqj \gt C_q such that a wall exists at (Rq,j)(R_q, j) and no wall exists at (Rq,k)(R_q, k) for all Cq<k<jC_q \lt k \lt j, destroy the wall at (Rq,j)(R_q, j).

Constraints

  • 1H,W1 \leq H, W
  • H×W4×105H \times W \leq 4 \times 10^5
  • 1Q2×1051 \leq Q \leq 2 \times 10^5
  • 1RqH1 \leq R_q \leq H
  • 1CqW1 \leq C_q \leq W
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

HH WW QQ

R1R_1 C1C_1

R2R_2 C2C_2

\vdots

RQR_Q CQC_Q

Output

Print the number of remaining walls after processing all queries.

Sample Input 1

2 4 3
1 2
1 2
1 3

Sample Output 1

2

The process of handling the queries can be explained as follows:

  • In the 1st query, (R1,C1)=(1,2)(R_1, C_1) = (1, 2). There is a wall at (1,2)(1, 2), so the wall at (1,2)(1, 2) is destroyed.
  • In the 2nd query, (R2,C2)=(1,2)(R_2, C_2) = (1, 2). There is no wall at (1,2)(1, 2), so the walls at (2,2),(1,1),(1,3)(2,2),(1,1),(1,3), which are the first walls that appear when looking up, down, left, and right from (1,2)(1, 2), are destroyed.
  • In the 3rd query, (R3,C3)=(1,3)(R_3, C_3) = (1, 3). There is no wall at (1,3)(1, 3), so the walls at (2,3),(1,4)(2,3),(1,4), which are the first walls that appear when looking up, down, left, and right from (1,3)(1, 3), are destroyed.

After processing all queries, there are two remaining walls, at (2,1)(2, 1) and (2,4)(2, 4).

Sample Input 2

5 5 5
3 3
3 3
3 2
2 2
1 2

Sample Output 2

10

Sample Input 3

4 3 10
2 2
4 1
1 1
4 2
2 1
3 1
1 3
1 2
4 3
4 2

Sample Output 3

2