You are given a grid of
The grid initially consists of a single row containing the integers from
X 0
- Cut the grid into left and right halves and put the left half on top of the right half.X 1
- Cut the grid into left and right halves and put the right half on top of the left half.Y 0
- Cut the grid into top and bottom halves and put the top half on the left of the bottom half.Y 1
- Cut the grid into top and bottom halves and put the bottom half on the left of the top half.Q x
- Determine the current row and column number of the value .
For all of the cut operations, it is guaranteed that there will be at least two rows/columns in the direction which must be cut in half. Rows and columns are renumbered starting at
Constraints
Subtask [1/2 points]
Subtask [1/2 points]
No additional constraints.
Input Specification
The first line contains two space-separated integers,
The next
X 0
- Cut the grid into left and right halves and put the left half on top of the right half.X 1
- Cut the grid into left and right halves and put the right half on top of the left half.Y 0
- Cut the grid into top and bottom halves and put the top half on the left of the bottom half.Y 1
- Cut the grid into top and bottom halves and put the bottom half on the left of the top half.Q x
- Determine the current row and column number of the value .
For all of the cut operations, it is guarenteed that there will be at least two rows/columns in the direction which must be cut in half.
Output Specification
For every query operation (type Q
), output a line containing two space-separated integers, the current row and column number of the queried value.
Sample Input
8 9
Q 3
X 0
X 1
Q 1
Y 0
Q 8
Q 7
Y 1
Q 4
Sample Output
1 3
3 1
2 2
2 1
1 6
Explanation for Sample
The grid initially looks like this:
1 2 3 4 5 6 7 8
The first operation, Q 3
, queries the current position of the value 1 3
.
The second operation, X 0
, cuts the grid into left and right halves and puts the left half on top of the right half:
1 2 3 4
5 6 7 8
The third operation, X 1
, cuts the grid into left and right halves and puts the right half on top of the left half:
3 4
7 8
1 2
5 6
The fourth operation, Q 1
, queries the current position of the value 3 1
.
The fifth operation, Y 0
, cuts the grid into top and bottom halves and puts the top half on the left of the bottom half:
3 4 1 2
7 8 5 6
The sixth operation, Q 8
, queries the current position of the value 2 2
.
The seventh operation, Q 7
, queries the current position of the vallue 2 1
.
The eighth operation, Y 1
, cuts the grid into top and bottom halves and puts the bottom half on the left of the top half:
7 8 5 6 3 4 1 2
The ninth operation, Q 4
, queries the current position of the value 1 6
.
Comments
I think I speak for ALL of us when I say this problem is frigging tuff.