atcoder#ARC153B. [ARC153B] Grid Rotations

[ARC153B] Grid Rotations

Score : 500500 points

Problem Statement

We have a grid with HH rows from top to bottom and WW columns from left and right. Initially, the square at the ii-th row from the top and jj-th column from the left has a lowercase English letter Ai,jA_{i,j}.

Let us perform QQ operations on this grid. In the ii-th operation, we are given integers aia_i and bib_i such that 1aiH11\leq a_i \leq H-1 and 1biW11\leq b_i\leq W-1, and do the following.

  • Let R1R_1, R2R_2, R3R_3, and R4R_4 be rectangular regions within the grid defined as follows:- R1R_1 is the intersection of the top aia_i rows and leftmost bib_i columns;
    • R2R_2 is the intersection of the top aia_i rows and rightmost WbiW-b_i columns;
    • R3R_3 is the intersection of the bottom HaiH-a_i rows and leftmost bib_i columns;
    • R4R_4 is the intersection of the bottom HaiH-a_i rows and rightmost WbiW-b_i columns.
  • R1R_1 is the intersection of the top aia_i rows and leftmost bib_i columns;
  • R2R_2 is the intersection of the top aia_i rows and rightmost WbiW-b_i columns;
  • R3R_3 is the intersection of the bottom HaiH-a_i rows and leftmost bib_i columns;
  • R4R_4 is the intersection of the bottom HaiH-a_i rows and rightmost WbiW-b_i columns.
  • Rotate 180180 degrees each of R1R_1, R2R_2, R3R_3, and R4R_4.

Here, a 180180-degree rotation of a rectangular region RR within the grid moves the character on the square at the ii-th from the top and jj-th column from the left in RR to the square at the ii-th from the bottom and jj-th column from the right in RR. See also the figures for the samples.

Print the grid after all QQ operations.

Constraints

  • 2H,W2\leq H, W, and HW5×105HW \leq 5\times 10^5.
  • Ai,jA_{i,j} is a lowercase English letter.
  • 1Q2×1051\leq Q\leq 2\times 10^5
  • 1aiH11\leq a_i\leq H - 1
  • 1biW11\leq b_i\leq W - 1

Input

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

HH WW

A1,1A1,WA_{1,1}\cdots A_{1, W}

\vdots

AH,1AH,WA_{H,1}\cdots A_{H, W}

QQ

a1a_1 b1b_1

\vdots

aQa_Q bQb_Q

Output

Print the grid after the operations in the following format, where Bi,jB_{i,j} is the character on the square (i,j)(i,j) on the final grid.

B1,1B1,WB_{1,1}\cdots B_{1, W}

\vdots

BH,1BH,WB_{H,1}\cdots B_{H, W}

4 5
abcde
fghij
klmno
pqrst
1
3 3
mlkon
hgfji
cbaed
rqpts

The grid will change as follows.

3 7
atcoder
regular
contest
2
1 1
2 5
testcon
oderatc
ularreg

The grid will change as follows.

2 2
ac
wa
3
1 1
1 1
1 1
ac
wa

The grid will change as follows.