Inna and Dima decided to surprise Sereja. They brought a really huge candy matrix, it's big even for Sereja! Let's number the rows of the giant matrix from 1 to n from top to bottom and the columns — from 1 to m, from left to right. We'll represent the cell on the intersection of the i-th row and j-th column as (i, j). Just as is expected, some cells of the giant candy matrix contain candies. Overall the matrix has p candies: the k-th candy is at cell (xk, yk).
The time moved closer to dinner and Inna was already going to eat p of her favourite sweets from the matrix, when suddenly Sereja (for the reason he didn't share with anyone) rotated the matrix x times clockwise by 90 degrees. Then he performed the horizontal rotate of the matrix y times. And then he rotated the matrix z times counterclockwise by 90 degrees. The figure below shows how the rotates of the matrix looks like.
Inna got really upset, but Duma suddenly understood two things: the candies didn't get damaged and he remembered which cells contained Inna's favourite sweets before Sereja's strange actions. Help guys to find the new coordinates in the candy matrix after the transformation Sereja made!
The first line of the input contains fix integers n, m, x, y, z, p (1 ≤ n, m ≤ 109; 0 ≤ x, y, z ≤ 109; 1 ≤ p ≤ 105).
Each of the following p lines contains two integers xk, yk (1 ≤ xk ≤ n; 1 ≤ yk ≤ m) — the initial coordinates of the k-th candy. Two candies can lie on the same cell.
For each of the p candies, print on a single line its space-separated new coordinates.
3 3 3 1 1 9 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3
1 3 1 2 1 1 2 3 2 2 2 1 3 3 3 2 3 1
Just for clarity. Horizontal rotating is like a mirroring of the matrix. For matrix:
QWER REWQ ASDF -> FDSA ZXCV VCXZ
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <queue> #include <algorithm> #include <set> using namespace std; #define MM(a) memset(a,0,sizeof(a)) typedef long long LL; typedef unsigned long long ULL; const int maxn = 1e5+5; const int mod = 1000000007; const double eps = 1e0-7; int xx[maxn], yy[maxn], tmp[maxn]; int main() { int n, m, x, y, z, p; while(~scanf("%d%d%d%d%d%d",&n,&m,&x,&y,&z,&p)) { x %= 4, y%=2, z%=4; ///int ans = x - z; int mm = m, nn = n; for(int i=0; i<p; i++) scanf("%d%d",&xx[i],&yy[i]); for(int i=0; i<p; i++) { m = mm, n = nn; if(x == 1) { tmp[i] = xx[i]; xx[i] = yy[i]; yy[i] = n+1-tmp[i]; swap(m, n); } else if(x == 2) { xx[i] = n+1-xx[i]; yy[i] = m+1-yy[i]; } else if(x == 3) { tmp[i] = xx[i]; xx[i] = m+1-yy[i]; yy[i] = tmp[i]; swap(m, n); } if(y == 1) yy[i] = m+1-yy[i]; if(z == 1) { tmp[i] = xx[i]; xx[i] = m+1-yy[i]; yy[i] = tmp[i]; swap(m, n); } else if(z == 2) { xx[i] = n+1-xx[i]; yy[i] = m+1-yy[i]; } else if(z == 3) { tmp[i] = xx[i]; xx[i] = yy[i]; yy[i] = n+1-tmp[i]; swap(m, n); } } for(int i=0; i<p; i++) cout<<xx[i]<<" "<<yy[i]<<endl; } return 0; }