AOJ 2343 Matrix Operation

さすがにそろそろICPC対策しないとなあと思い、Marathon Matchのレーティングを犠牲に練習を始めます。

ところでこういう実装は苦手です…

#include<stdio.h>
#include<algorithm>
#include<map>
using namespace std;
int row[41000];
int col[41000];
map<pair<int,int>,int> m;
char str[10];
int dir;
int n;
pair<int,int> conv(int a,int b){
	int ret1=0;
	int ret2=0;
	switch(dir&3){
		case 0:
			ret1=a;
			ret2=b;
			break;
		case 1:
			ret1=b;
			ret2=n-a+1;
			break;
		case 2:
			ret1=n-a+1;
			ret2=n-b+1;
			break;
		case 3:
			ret1=n-b+1;
			ret2=a;
			break;
	}
	if(dir&4)swap(ret1,ret2);
	return make_pair(row[ret1],col[ret2]);
}
int main(){
	int a,b,c,d,e,f,g;
	int q;
	scanf("%d%d%d%d%d%d%d%d%d",&n,&q,&a,&b,&c,&d,&e,&f,&g);
	long long h=314159265;
	long long mod=1000000007;
	for(int i=1;i<=n;i++)row[i]=i;
	for(int i=1;i<=n;i++)col[i]=i;
	while(q--){
		scanf("%s",str);
		if(str[0]=='W'){
			int R,C,V;
			scanf("%d%d%d",&R,&C,&V);
			pair<int,int> at=conv(R,C);
			m[at]=V;
		}
		if(str[0]=='C'){
			int r1,c1,r2,c2;
			scanf("%d%d%d%d",&r1,&c1,&r2,&c2);
			pair<int,int> at1=conv(r1,c1);
			pair<int,int> at2=conv(r2,c2);
			if(m.count(at1))m[at2]=m[at1];
			else m[at2]=((long long)at1.first*a+(long long)at1.second*b)%c;
		}
		if(str[0]=='S'){
			int r1,r2;
			scanf("%d%d",&r1,&r2);
			if((dir/4+(str[1]=='R')+dir%4)&1){
				if((dir+1+dir/4*1+(str[1]!='R')*2)%4<2)swap(row[r1],row[r2]);
				else swap(row[n-r1+1],row[n-r2+1]);
			}else{
				if((dir+dir/4*3+(str[1]=='R')*2)%4<2)swap(col[r1],col[r2]);
				else swap(col[n-r1+1],col[n-r2+1]);
			}
		}
		if(str[0]=='R'){
			if(str[1]=='L'){
				dir=(dir&4)+((dir%4+1)&3);
			}
			if(str[1]=='R'){
				dir=(dir&4)+((dir%4+3)&3);
			}
			if(str[1]=='H'){
				dir=((dir&4)?0:4)+(dir%4)^1;
			}
			if(str[1]=='V'){
				dir=((dir&4)?0:4)+(dir%4)^3;
			}
		}
	}/*
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			pair<int,int> at=conv(i,j);
			int ans;
			if(m.count(at))ans=m[at];
			else ans=((long long)at.first*a+(long long)at.second*b)%c;
			printf("%d ",ans);
		}
		printf("\n");
	}*/
	for(int i=d;i<=e;i++){
		for(int j=f;j<=g;j++){
			pair<int,int> at=conv(i,j);
			if(m.count(at))h=(h*31+m[at])%mod;
			else h=(h*31+((long long)at.first*a+(long long)at.second*b)%c)%mod;
		}
	}
	printf("%lld\n",h);
}