#P3776. [APIO2017] 斑斓之地
[APIO2017] 斑斓之地
Description
In the distant Golden Age, the land of Australia was rectangular and could be divided into a grid with rows and columns. Rows are numbered to from north to south, and columns are numbered to from west to east. The cell denotes the land in row and column . One day, the great Rainbow Serpent started at and moved across Australia. The serpent made 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 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 movement directions of the Rainbow Serpent and 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 , , , and .
The second line contains two integers and .
The third line contains a string of characters, each being one of N, S, E, or W (if , this line is left blank).
Lines through : each line contains four integers , , , and , describing the land you intend to purchase, whose top-left corner is and bottom-right corner is .
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, , and . For every purchased rectangle, and .
The detailed subtask scores and additional conditions are as follows.
| Subtask ID | Score | |||
|---|---|---|---|---|
Translated by ChatGPT 5
京公网安备 11011102002149号