loj#P2310. 「APIO2017」斑斓之地

「APIO2017」斑斓之地

Description

A long long time ago, in the Dreamtime, Australia was a flat grid of RR rows and CC columns and each grid cell was land. The rows were numbered 11 to RR from North to South, and the columns were numbered 11 to CC from West to East. The cell in row rr and column cc was denoted as (r,(r, c)c). One day, the great rainbow serpent rose from the earth at (sr,(s_r, sc)s_c), and moved across Australia, creating rivers wherever it slid. The serpent made MM consecutive moves, each time moving to the cell directly North (N), South (S), East (E) or West (W), turning the cell into river. The cell (sr,(s_r, sc)s_c) was also turned into river.

Now, millions of years later, you would like to purchase a rectangular block of cells to commemorate the creation of the rivers by the rainbow serpent. You will choose a different colour for each land cell inside your rectangular block. You would like to use as many different colours as possible but insist that every pair of adjacent land cells inside your block share the same colour. Two cells are adjacent if they share an edge. You will not choose a colour for any land cells outside your block, nor will you choose a colour for the river cells inside your block.

Given the moves made by the rainbow serpent, you would like to determine the maximum number of different colours you can choose for land cells in each of QQ rectangular blocks of cells.

Input

Line 1: four integers R,R, C,C, MM and QQ;
Line 2: two integers srs_r and scs_c;
Line 3: a string SS consisting of MM characters, each N, S, E or W (this line should be left blank if M=0M = 0);
Lines 4 to QQ+3: four integers ara_r, aca_c, brb_r and bcb_c.

Output

For every request, output the maximum number of different colours you can choose for land cells in the rectangular block.

6 4 9 4
3 3
NWESSWEWS
2 3 2 3
3 2 4 4
5 3 6 4
1 2 5 3
0
2
1
3

Limits And Hints

For all subtasks, 0M1050 ≤ M ≤ 10^5 and R,C,Q1R, C, Q ≥ 1.

Subtask Points RR CC QQ
1 11 R50R\le 50 C50C\le 50 Q1000Q\le 1000
2 12 R=2R=2 C2×105C\le 2\times 10^5 Q105Q\le 10^5
3 24 R2×105R\le 2\times 10^5 Q=1Q=1
4 27 R1000R\le 1000 C1000C\le 1000 Q105Q\le 10^5
5 26 R2×105R\le 2\times 10^5 C2×105C\le 2\times 10^5