forked from cacophonix/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ACS.cpp
62 lines (56 loc) · 940 Bytes
/
ACS.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
USER: zobayer
TASK: ACS
ALGO: simulation
*/
#include <cstdio>
#include <algorithm>
using namespace std;
#define MAXR 1234
#define MAXC 5678
int R[MAXR+1], C[MAXC+1], cR[MAXR+1], cC[MAXC+1];
inline int m(int x, int y)
{
return (R[x]-1)*MAXC + C[y];
}
inline void getxy(int w, int &x, int &y)
{
int p = (w+MAXC-1) / MAXC;
int q = (w-1) % MAXC + 1;
x = cR[p];
y = cC[q];
}
int main()
{
int i, x, y, w;
char s[5];
for(i=1; i<=1234; i++) R[i] = cR[i] = i;
for(i=1; i<=5678; i++) C[i] = cC[i] = i;
while(scanf("%s", s)==1)
{
switch(s[0])
{
case 'R':
scanf("%d %d", &x, &y);
swap(R[x], R[y]);
cR[R[x]] = x;
cR[R[y]] = y;
break;
case 'C':
scanf("%d %d", &x, &y);
swap(C[x], C[y]);
cC[C[x]] = x;
cC[C[y]] = y;
break;
case 'Q':
scanf("%d %d", &x, &y);
printf("%d\n", m(x, y));
break;
case 'W':
scanf("%d", &w);
getxy(w, x, y);
printf("%d %d\n", x, y);
}
}
return 0;
}