#P3776. [APIO2017] 斑斓之地

[APIO2017] 斑斓之地

Description

In the distant Golden Age, the land of Australia was rectangular and could be divided into a grid with RR rows and CC columns. Rows are numbered 11 to RR from north to south, and columns are numbered 11 to CC from west to east. The cell (r,c)(r, c) denotes the land in row rr and column cc. One day, the great Rainbow Serpent started at (sr,sc)(s_r, s_c) and moved across Australia. The serpent made MM consecutive moves, each time moving one cell to the north (N), south (S), east (E), or west (W). Every cell it passed through (including the start and end cells) turned into river. It is guaranteed that the Rainbow Serpent never leaves the R×CR \times C rectangular land at any time.

Millions of years later, you want to buy a rectangular area to commemorate the great Rainbow Serpent. You want to color every non-river cell inside the purchased rectangle, with the requirement that adjacent cells must have the same color. Two cells are adjacent if and only if they share a common side. Cells outside your purchased area do not need to be colored.

You are given the MM movement directions of the Rainbow Serpent and QQ candidate rectangular regions to purchase. For each region, determine the maximum number of different colors you can use.

Input Format

The first line contains four integers RR, CC, MM, and QQ.

The second line contains two integers srs_r and scs_c.

The third line contains a string SS of MM characters, each being one of N, S, E, or W (if M=0M=0, this line is left blank).

Lines 44 through Q+3Q+3: each line contains four integers ara_r, aca_c, brb_r, and bcb_c, describing the land you intend to purchase, whose top-left corner is (ar,ac)(a_r, a_c) and bottom-right corner is (br,bc)(b_r, b_c).

Output Format

For each query, output the maximum number of different colors that the land can be painted with.

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

Hint

Sample Explanation

The sample corresponds to the figure below, where blue cells represent rivers.

Constraints

For all testdata, 0M1050 \le M \le 10^5, and R,C,Q1R, C, Q \ge 1. For every purchased rectangle, 1arbrR1 \le a_r \le b_r \le R and 1acbcC1 \le a_c \le b_c \le C.

The detailed subtask scores and additional conditions are as follows.

Subtask ID Score RR CC QQ
11 1111 R50R \le 50 C50C \le 50 Q1000Q \le 1000
22 1212 R=2R = 2 C2×105C \le 2 \times 10^5 Q105Q \le 10^5
33 2424 R2×105R \le 2 \times 10^5 Q=1Q = 1
44 2727 R1000R \le 1000 C1000C \le 1000 Q105Q \le 10^5
55 2626 R2×105R \le 2 \times 10^5 C2×105C \le 2 \times 10^5

Translated by ChatGPT 5