C - Urban Planning and Road Development Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 366

問題文

高橋君は、ある都市の建物と道路の関係を調査しています。

都市は縦 H 行、横 W 列のマス目で表されます。上から i 行目(1 \leq i \leq H)、左から j 列目(1 \leq j \leq W)のマスを (i, j) と表記します。各マスは次のいずれかの種類を持ちます。

  • B :建物
  • R :道路
  • E :空き地

初期状態において、マス (i, j) の種類は文字列 S_ij 文字目で与えられます。

ある建物のマスが、上下左右に隣り合うマスのうち少なくとも 1 つが道路のマスであるとき、その建物のマスは 道路に面している といいます。斜めに隣り合っている場合は、道路に面しているとはみなしません。

これから Q 回の道路工事が順に行われます。工事の効果は累積します(一度道路に変わったマスは、以降の工事を通じて道路のままです)。

k 回目(1 \leq k \leq Q)の工事では、行 U_k から行 D_k まで、かつ列 L_k から列 R_k までの長方形領域内にあるすべての空き地のマスが道路に変わります。すでに道路であるマスや建物のマスは変化しません。工事範囲外のマスも変化しません。

各工事の長方形領域内に建物のマスは含まれないことが保証されます。

各工事の直後に、道路に面している建物のマスの総数を求めてください。1 つの建物のマスが複数の道路マスと隣り合っていても、1 つとして数えます。

制約

  • 1 \leq H, W \leq 10^6
  • 1 \leq Q \leq 10^5
  • H W \leq 2 \times 10^6
  • H + Q \leq 2 \times 10^5
  • S_i1 \leq i \leq H)は長さ W の文字列であり、各文字は BRE のいずれかである
  • 1 \leq U_k \leq D_k \leq H1 \leq k \leq Q
  • 1 \leq L_k \leq R_k \leq W1 \leq k \leq Q
  • \displaystyle \sum_{k=1}^{Q} \left( 2(D_k-U_k+1) + 2(R_k-L_k+1) \right) \leq 5 \times 10^6
  • 各工事範囲の長方形領域内に B であるマスは含まれない
  • 入力はすべて整数および文字列である

入力

H W Q
S_1
S_2
\vdots
S_H
U_1 D_1 L_1 R_1
U_2 D_2 L_2 R_2
\vdots
U_Q D_Q L_Q R_Q
  • 1 行目には、都市の行数を表す整数 H、列数を表す整数 W、道路工事の回数を表す整数 Q がスペース区切りで与えられる。
  • 続く H 行では、都市の初期状態を表す文字列 S_i1 \leq i \leq H)が与えられる。S_ij 文字目がマス (i, j) の初期状態を表す。
  • 続く Q 行では、道路工事の情報が与えられる。k 回目の工事を表す行には、工事範囲の上端の行番号 U_k、下端の行番号 D_k、左端の列番号 L_k、右端の列番号 R_k がスペース区切りで与えられる。工事範囲は、行 U_k 以上 D_k 以下かつ列 L_k 以上 R_k 以下の長方形領域である。

出力

A_1
A_2
\vdots
A_Q

Q 行出力せよ。k 行目には、k 回目の工事の直後における、道路に面している建物のマスの総数 A_k を出力してください。


入力例 1

4 5 3
EEEEE
EBERE
EEEEE
REBEE
1 1 1 3
3 4 4 5
2 3 3 3

出力例 1

1
2
2

入力例 2

3 3 4
BEB
EEE
BEB
2 2 2 2
1 3 2 2
2 2 1 3
1 1 2 2

出力例 2

0
4
4
4

入力例 3

8 10 7
EEREEEEEER
EEBEEEEEEE
EEEEERBEEE
EEEEEEEEEE
BEEEEEEEER
EEEEREEEEE
EEEEBEEEEE
REEEEEEEEE
1 1 1 5
4 4 1 10
2 3 4 6
5 6 2 4
6 8 6 8
1 3 8 10
7 8 1 3

出力例 3

3
4
4
4
4
4
4

入力例 4

12 15 10
EEEEREEEEEEEEEE
EEEEBEEEREEBEEE
EEEEEEEEEEEEEEE
EEBEEEEEEEEEEER
EREEEEEEEBEEEEE
EEEEEEEEEEEEERE
EEEEEEBEEEEEEBE
EEEEEEEEEEEEEEE
BEEEEEEBEEEEEER
EEEEREEEEEEEEEE
EEEBEEEEEEBEEEE
REEEEEEEEEEEEER
1 1 1 15
2 8 1 1
3 6 4 8
2 4 13 15
5 6 11 15
8 10 2 4
10 12 12 15
6 8 8 13
3 3 1 15
12 12 1 15

出力例 4

3
4
6
6
7
8
9
10
10
10

入力例 5

1 1 1
E
1 1 1 1

出力例 5

0

Score : 366 pts

Problem Statement

Takahashi is investigating the relationship between buildings and roads in a certain city.

The city is represented as a grid with H rows and W columns. The cell at the i-th row from the top (1 \leq i \leq H) and the j-th column from the left (1 \leq j \leq W) is denoted as (i, j). Each cell has one of the following types:

  • B : Building
  • R : Road
  • E : Empty lot

In the initial state, the type of cell (i, j) is given by the j-th character of the string S_i.

A building cell is said to be facing a road if at least one of its vertically or horizontally adjacent cells is a road cell. Diagonally adjacent cells are not considered as facing a road.

From now on, Q road construction operations will be performed in order. The effects of construction are cumulative (once a cell becomes a road, it remains a road throughout all subsequent operations).

In the k-th operation (1 \leq k \leq Q), all empty lot cells within the rectangular region from row U_k to row D_k and from column L_k to column R_k are converted to road cells. Cells that are already roads or buildings do not change. Cells outside the construction area also do not change.

It is guaranteed that no building cells are contained within the rectangular region of each construction operation.

After each construction operation, determine the total number of building cells that are facing a road. Even if a single building cell is adjacent to multiple road cells, it is counted as one.

Constraints

  • 1 \leq H, W \leq 10^6
  • 1 \leq Q \leq 10^5
  • H W \leq 2 \times 10^6
  • H + Q \leq 2 \times 10^5
  • S_i (1 \leq i \leq H) is a string of length W, where each character is one of B, R, or E
  • 1 \leq U_k \leq D_k \leq H (1 \leq k \leq Q)
  • 1 \leq L_k \leq R_k \leq W (1 \leq k \leq Q)
  • \displaystyle \sum_{k=1}^{Q} \left( 2(D_k-U_k+1) + 2(R_k-L_k+1) \right) \leq 5 \times 10^6
  • No cell with B is contained within the rectangular region of each construction operation
  • All inputs are integers and strings

Input

H W Q
S_1
S_2
\vdots
S_H
U_1 D_1 L_1 R_1
U_2 D_2 L_2 R_2
\vdots
U_Q D_Q L_Q R_Q
  • The first line contains integers H representing the number of rows, W representing the number of columns, and Q representing the number of road construction operations, separated by spaces.
  • The following H lines give the strings S_i (1 \leq i \leq H) representing the initial state of the city. The j-th character of S_i represents the initial state of cell (i, j).
  • The following Q lines give the information of the road construction operations. The line representing the k-th operation contains the top row number U_k, bottom row number D_k, left column number L_k, and right column number R_k of the construction area, separated by spaces. The construction area is the rectangular region with rows from U_k to D_k (inclusive) and columns from L_k to R_k (inclusive).

Output

A_1
A_2
\vdots
A_Q

Output Q lines. On the k-th line, output A_k, the total number of building cells that are facing a road immediately after the k-th construction operation.


Sample Input 1

4 5 3
EEEEE
EBERE
EEEEE
REBEE
1 1 1 3
3 4 4 5
2 3 3 3

Sample Output 1

1
2
2

Sample Input 2

3 3 4
BEB
EEE
BEB
2 2 2 2
1 3 2 2
2 2 1 3
1 1 2 2

Sample Output 2

0
4
4
4

Sample Input 3

8 10 7
EEREEEEEER
EEBEEEEEEE
EEEEERBEEE
EEEEEEEEEE
BEEEEEEEER
EEEEREEEEE
EEEEBEEEEE
REEEEEEEEE
1 1 1 5
4 4 1 10
2 3 4 6
5 6 2 4
6 8 6 8
1 3 8 10
7 8 1 3

Sample Output 3

3
4
4
4
4
4
4

Sample Input 4

12 15 10
EEEEREEEEEEEEEE
EEEEBEEEREEBEEE
EEEEEEEEEEEEEEE
EEBEEEEEEEEEEER
EREEEEEEEBEEEEE
EEEEEEEEEEEEERE
EEEEEEBEEEEEEBE
EEEEEEEEEEEEEEE
BEEEEEEBEEEEEER
EEEEREEEEEEEEEE
EEEBEEEEEEBEEEE
REEEEEEEEEEEEER
1 1 1 15
2 8 1 1
3 6 4 8
2 4 13 15
5 6 11 15
8 10 2 4
10 12 12 15
6 8 8 13
3 3 1 15
12 12 1 15

Sample Output 4

3
4
6
6
7
8
9
10
10
10

Sample Input 5

1 1 1
E
1 1 1 1

Sample Output 5

0