#P7782. It's All Squares
It's All Squares
It's All Squares
Problem Description
One day when Little Q woke up, he found himself being inside a 2D pixel world. The world is a grid with square cells. Little Q can only walk along the side of these cells, which means he can stay at a point if and only if and , where and are all integers. There is a number written at the center of each cell, number () is written at the point . Little Q had no idea about how to escape from the pixel world, so he started wandering. You will be given queries, each query consists of two integers and a string , denoting the route of Little Q. Initially, Little Q will stand at , then he will do steps of movements one by one. Here is what he will do for each type of movement: · "" : Move from to . · "" : Move from to . · "" : Move from to . · "" : Move from to . It is guaranteed that Little Q will never walk outside of the pixel world, and the route will form a simple polygon. For each query, please tell Little Q how many distinct numbers there are inside the polygon formed by the route. Fortunately, after solving this problem, Little Q woke up on his bed. The pixel world had just been a dream!
Input
The first line of the input contains a single integer (), the number of test cases. For each case, the first line of the input contains three integers and (, ), denoting the size of the pixel world and the number of queries. Each of the following lines contains integers, the -th line contains integers (), denoting the number written in each cell. Each of the following lines contains two integers (, ) and a non-empty string ($S_i\in\{\texttt{L},\texttt{R},\texttt{D},\texttt{U}\}$), denoting each query. It is guaranteed that .
Output
For each query, output a single line containing an integer, the number of distinct numbers inside the polygon.
Sample Input
1
3 3 2
1 2 3
1 1 2
7 8 9
0 0 RRRUUULLLDDD
0 0 RRUULLDD
Sample Output
6
2
Source
2020 Multi-University Training Contest 2