problems / 248

The Orchard Ledger

medium fenwick-tree matrix prefix-sums 1000 ms · 256 MB

The Orchard Ledger

Old Mabel keeps her orchard's records on a single sheet of squared paper. The orchard is a rectangle of R rows and C columns of plots, drawn one character per plot:

  • T — a tree stands on this plot;
  • . — the plot is empty.

Rows are numbered 1 to R from top to bottom and columns 1 to C from left to right, so the plot in row r and column c is written (r, c). Nothing lies outside the drawn rectangle.

Through the season the ledger collects a long list of entries, processed in order:

  • PLANT r c — a tree is planted on plot (r, c). If a tree already stands there, nothing changes.
  • CUT r c — the tree on plot (r, c) is cut down, leaving the plot empty. If the plot is already empty, nothing changes.
  • COUNT r1 c1 r2 c2 — a buyer asks how many trees currently stand in the rectangle with opposite corners (r1, c1) and (r2, c2), both corners included. The corners may be given in any order: the rectangle covers every row between r1 and r2 and every column between c1 and c2, whichever of each pair is smaller.

Answer every COUNT.

Input

The first line contains two integers R and C (1 ≤ R, C ≤ 1000).

Each of the next R lines contains a string of exactly C characters, each T or . — the orchard at the start of the season.

The next line contains an integer q (1 ≤ q ≤ 2·10^5) — the number of ledger entries.

Each of the next q lines contains one entry: PLANT r c, CUT r c (1 ≤ r ≤ R, 1 ≤ c ≤ C) or COUNT r1 c1 r2 c2 (1 ≤ r1, r2 ≤ R, 1 ≤ c1, c2 ≤ C). At least one entry is a COUNT.

Output

For every COUNT entry, print one line with the number of trees in the requested rectangle.

Notes

In the first example the orchard starts as

T..T
.TT.
T...

The whole orchard holds 5 trees. The rectangle with corners (2, 3) and (1, 2) covers rows 1–2 and columns 2–3, which hold the trees at (2, 2) and (2, 3): 2 trees. Planting at (1, 2) adds a tree; planting at (2, 2) changes nothing because a tree already stands there; cutting (3, 4) changes nothing because that plot is empty. The same rectangle now holds 3 trees. After (2, 3) is cut, the left three columns of rows 1–3 hold the trees at (1, 1), (1, 2), (2, 2) and (3, 1): 4 trees.

In the second example the orchard is a single empty plot.

Example 1

Input

3 4
T..T
.TT.
T...
8
COUNT 1 1 3 4
COUNT 2 3 1 2
PLANT 1 2
PLANT 2 2
CUT 3 4
COUNT 1 2 2 3
CUT 2 3
COUNT 3 3 1 1

Output

5
2
3
4

Example 2

Input

1 1
.
6
COUNT 1 1 1 1
CUT 1 1
COUNT 1 1 1 1
PLANT 1 1
PLANT 1 1
COUNT 1 1 1 1

Output

0
0
1

Hints · reveal one at a time if you're stuck

Hint 1

If the orchard never changed, 2D prefix sums would answer every COUNT in O(1). What does a single PLANT or CUT do to those prefix sums, and why is rebuilding them too slow?

Hint 2

You need a structure that supports "add ±1 at one cell" and "sum of a top-left rectangle" both in polylogarithmic time. A Fenwick tree does this in one dimension; nest it to get two dimensions, then combine four prefix sums by inclusion–exclusion. Keep the picture itself too, so a redundant PLANT or CUT changes nothing.

report an issue Prefer your own editor? sooks prep 248 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.