Codeforces Round #137 (Div. 2) B : Cosmic Tables

問題概要

H*W(H,W<1000)の行列がある。以下のQ(<5e4)個のクエリを処理する。

  • i行目とj行目をスワップする。
  • i列目とj列目をスワップする。
  • i行j列の要素を出力する。

解法

実際に全ての要素を置換せずにindexの示す先だけをスワップすればよい。

acceptされたコード

#include <cstdio>
#include <algorithm>
using namespace std;

const int MAX_N = 1000;
int H, W, Q;
int mat[MAX_N][MAX_N];
int rows[MAX_N], columns[MAX_N];

void init() {
	scanf("%d%d%d ", &H, &W, &Q);
	for (int i = 0; i < H; ++i) {
		for (int j = 0; j < W; ++j) {
			scanf("%d ", mat[i] + j);
		}
	}
	for (int i = 0; i < H; ++i) {
		rows[i] = i;
	}
	for (int i = 0; i < W; ++i) {
		columns[i] = i;
	}
}

void proc() {
	char ch;
	int y, x;
	scanf(" %c %d%d ", &ch, &y, &x);
	--y; --x;
	if (ch == 'r') {
		swap(rows[y], rows[x]);
	}
	else if (ch == 'c') {
		swap(columns[y], columns[x]);
	}
	else {
		printf("%d\n", mat[rows[y]][columns[x]]);
	}
}

int main() {
	init();
	for (int _ = 0; _ < Q; ++_) {
		proc();
	}
	return 0;
}