【优化&大师级代码欣赏】Codeforces Round #137 (Div. 2) / 222B Cosmic Tables (矩阵)
2013-08-19 16:09
726 查看
B. Cosmic Tables
http://codeforces.com/problemset/problem/222/B
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
The Free Meteor Association (FMA) has got a problem: as meteors are moving, the Universal Cosmic Descriptive Humorous Program (UCDHP) needs to add a special module that would analyze this movement.
UCDHP stores some secret information about meteors as an n × m table with integers in its cells. The order of meteors in the Universe is changing.
That's why the main UCDHP module receives the following queries:
The query to swap two table rows;
The query to swap two table columns;
The query to obtain a secret number in a particular table cell.
As the main UCDHP module is critical, writing the functional of working with the table has been commissioned to you.
Input
The first line contains three space-separated integers n, m and k (1 ≤ n, m ≤ 1000, 1 ≤ k ≤ 500000)
— the number of table columns and rows and the number of queries, correspondingly.
Next n lines contain m space-separated numbers each
— the initial state of the table. Each number p in the table is an integer and satisfies the inequality 0 ≤ p ≤ 106.
Next k lines contain queries in the format "si xi yi",
where si is one
of the characters "с", "r" or "g",
and xi, yi are
two integers.
If si = "c",
then the current query is the query to swap columns with indexes xi and yi (1 ≤ x, y ≤ m, x ≠ y);
If si = "r",
then the current query is the query to swap rows with indexes xi and yi (1 ≤ x, y ≤ n, x ≠ y);
If si = "g",
then the current query is the query to obtain the number that located in the xi-th
row and in the yi-th
column (1 ≤ x ≤ n, 1 ≤ y ≤ m).
The table rows are considered to be indexed from top to bottom from 1 to n, and the table columns — from left to right from 1 to m.
Output
For each query to obtain a number (si =
"g") print the required number. Print the answers to the queries in the order of the queries in the input.
Sample test(s)
input
output
input
output
Note
Let's see how the table changes in the second test case.
After the first operation is fulfilled, the table looks like that:
2 1 4
1 3 5
After the second operation is fulfilled, the table looks like that:
1 3 5
2 1 4
So the answer to the third query (the number located in the first row and in the third column) will be 5.
简单的矩阵维护查询。
学到个小技巧~
字符、空格和数字混合读取时推荐用字符串读取单个字符。
完整代码:(我写的,大师在后面)
大师A:
大师B:
大师C:
大师D:
大师E:极简主义
大师F:神一般的内存优化
大师G:
http://codeforces.com/problemset/problem/222/B
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
The Free Meteor Association (FMA) has got a problem: as meteors are moving, the Universal Cosmic Descriptive Humorous Program (UCDHP) needs to add a special module that would analyze this movement.
UCDHP stores some secret information about meteors as an n × m table with integers in its cells. The order of meteors in the Universe is changing.
That's why the main UCDHP module receives the following queries:
The query to swap two table rows;
The query to swap two table columns;
The query to obtain a secret number in a particular table cell.
As the main UCDHP module is critical, writing the functional of working with the table has been commissioned to you.
Input
The first line contains three space-separated integers n, m and k (1 ≤ n, m ≤ 1000, 1 ≤ k ≤ 500000)
— the number of table columns and rows and the number of queries, correspondingly.
Next n lines contain m space-separated numbers each
— the initial state of the table. Each number p in the table is an integer and satisfies the inequality 0 ≤ p ≤ 106.
Next k lines contain queries in the format "si xi yi",
where si is one
of the characters "с", "r" or "g",
and xi, yi are
two integers.
If si = "c",
then the current query is the query to swap columns with indexes xi and yi (1 ≤ x, y ≤ m, x ≠ y);
If si = "r",
then the current query is the query to swap rows with indexes xi and yi (1 ≤ x, y ≤ n, x ≠ y);
If si = "g",
then the current query is the query to obtain the number that located in the xi-th
row and in the yi-th
column (1 ≤ x ≤ n, 1 ≤ y ≤ m).
The table rows are considered to be indexed from top to bottom from 1 to n, and the table columns — from left to right from 1 to m.
Output
For each query to obtain a number (si =
"g") print the required number. Print the answers to the queries in the order of the queries in the input.
Sample test(s)
input
3 3 5 1 2 3 4 5 6 7 8 9 g 3 2 r 3 2 c 2 3 g 2 2 g 3 2
output
8 9 6
input
2 3 3 1 2 4 3 1 5 c 2 1 r 1 2 g 1 3
output
5
Note
Let's see how the table changes in the second test case.
After the first operation is fulfilled, the table looks like that:
2 1 4
1 3 5
After the second operation is fulfilled, the table looks like that:
1 3 5
2 1 4
So the answer to the third query (the number located in the first row and in the third column) will be 5.
简单的矩阵维护查询。
学到个小技巧~
字符、空格和数字混合读取时推荐用字符串读取单个字符。
完整代码:(我写的,大师在后面)
/*872ms,3900KB*/ #include<cstdio> #include<cstring> int mat[1005][1005], row[1005], col[1005]; int main() { int n, m, k; char s[2]; int a, b, temp; memset(mat, 0, sizeof(mat)); scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) scanf("%d", &mat[i][j]); for (int i = 1; i <= n; ++i) row[i] = i; for (int i = 1; i <= m; ++i) col[i] = i; while (k--) { ///混合读取时推荐用char数组(而不是用char)读取单个字符 scanf("%s%d%d", s, &a, &b); if (s[0] == 'c') { temp = col[a]; col[a] = col[b]; col[b] = temp; } else if (s[0] == 'r') { temp = row[a]; row[a] = row[b]; row[b] = temp; } else printf("%d\n", mat[row[a]][col[b]]); } return 0; }
大师A:
/*最快!250ms,5400KB*/
// -------------------- Khai bao thu vien --------------------
#include <set>
#include <bitset>
#include <queue>
#include <deque>
#include <set>
#include <bitset>
#include <queue>
#include <deque>
#include <stack>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <cassert>
#include <vector>
#include <list>
#include <map>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
using namespace std;
// -------------------- Khai bao cac container --------------------
typedef vector <int> vi;
typedef vector <vi> vvi;
typedef pair <int, int> ii;
typedef vector <ii> vii;
typedef vector <string> vs;
typedef long long int64; //NOTES:int64
typedef unsigned long long uint64; //NOTES:uint64
typedef unsigned uint;
const double pi = acos(-1.0); //NOTES:pi
const double eps = 1e-11; //NOTES:eps
const int MAXI = 0x7fffffff;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
const char dz[] = "SENW";
// -------------------- Dinh nghia lai cac phep toan --------------------
#define FORN(i,a,b) for (int i=(a),_b=(b); i<_b; i++)
#define RFORN(i,b,a) for (int i=(b)-1,_a=(a); i>=_a; i--)
#define RESET(a,b) memset((a),(b),sizeof(a))
#define SC(x) scanf("%d",&x)
#define SC2(x,y) scanf("%d %d",&x,&y)
#define PR(x) printf("%d ",x)
#define PR2(x,y) printf("%d %d ",x,y)
#define PRS(x) printf("%s",x)
#define END printf("\n")
#define EXIT(x) {PRS(x);return 0;}
#define DBG(x) {cerr << "--> " << #x << " = " << x << endl;}
#define fi first
#define se second
#define PB push_back
#define ALL(x) (x).begin(),(x).end()
#define MP(X,Y) make_pair(X,Y)
#define foreach(i, c) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define two(x) (1 << (x))
#define two64(x) (((int64)(1)) << (x))
#define contain(s, x) (((s) & two(x)) != 0)
#define contain64(s,x) (((s) & two64(x)) != 0)
// =========================== Bat dau template ===========================
// -------------------- Cac thao tac nhap - xuat, debug, v.v... --------------------
void startDBG(){
PRS("\n\n------------------- Start Debug -------------------\n\n");
}
void endDBG(){
PRS("\n\n------------------- End Debug -------------------\n");
}
int in() {
int x = 0, c;
for (; (uint)((c = getchar()) - '0') >= 10; ) { if (c == '-') return -in(); if (!~c) throw ~0; }
do { x = (x << 3) + (x << 1) + (c - '0'); } while ((uint)((c = getchar()) - '0') < 10);
return x;
}
int64 in64() {
int64 x = 0, c;
for (; (uint)((c = getchar()) - '0') >= 10; ) { if (c == '-') return -in(); if (!~c) throw ~0; }
do { x = (x << 3) + (x << 1) + (c - '0'); } while ((uint)((c = getchar()) - '0') < 10);
return x;
}
void showVi(vi A){
FORN (i, 0, A.size()) cout << A[i] << " ";
}
void showVii(vii A){
FORN (i, 0, A.size()) cout << A[i].fi << " " << A[i].se << endl;
}
void showVs(vs A){
FORN (i, 0, A.size()) cout << A[i] << endl;
}
void showArray(int A[], int n){
FORN (i, 0, n) cout << A[i] << " ";
}
int MUOI(int x){
int re = 1;
FORN (i, 0, x) re *= 10;
return re + x;
}
vi getVi(int SOLUONG){
vi REVI;
FORN (i, 0, SOLUONG){
int GETA;
SC(GETA);
REVI.PB(GETA);
}
return REVI;
}
vs getVs(int SOLUONG){
vs REVS;
FORN (i, 0, SOLUONG){
string GETS;
cin >> GETS;
REVS.PB(GETS);
}
return REVS;
}
vii getVii(int SOLUONG){
vii REVII;
FORN (i, 0, SOLUONG){
int GETA, GETB;
SC2(GETA, GETB);
REVII.PB(MP(GETA, GETB));
}
return REVII;
}
template<class T> inline T checkMod(T n, T m) {
return (n % m + m) % m;
}
template<class T> inline T multiplyMod(T a, T b, T m) {
return (T) ((((int64) (a)*(int64) (b) % (int64) (m))+(int64) (m)) % (int64) (m));
}
template<class T> inline T powerMod(T p, int e, T m)
{
if (e == 0)return 1 % m;
else if (e % 2 == 0) {
T t = powerMod(p, e / 2, m);
return multiplyMod(t, t, m);
} else return multiplyMod(powerMod(p, e - 1, m), p, m);
}
template<class T> inline bool isPrimeNumber(T n)
{
if (n <= 1)return false;
for (T i = 2; i * i <= n; i++) if (n % i == 0) return false;
return true;
}
template<class T> inline T sqr(T x) {
return x*x;
}
template<class T> inline T lowbit(T n) {
return (n^(n - 1))&n;
}
template<class T> inline int countbit(T n) {
return (n == 0) ? 0 : (1 + countbit(n & (n - 1)));
}
// -------------------- Cac ham ve toan --------------------
int64 C(int m,int n)
{
if (n > m) return 0;
int64 re = 1;
for(int i = 1; i <= n; i++) re = re * (m - i + 1) / i;
return re;
}
template<class T> inline T gcd(T a, T b)
{
if (a < 0)return gcd(-a, b);
if (b < 0)return gcd(a, -b);
return (b == 0) ? a : gcd(b, a % b);
}
template<class T> inline T lcm(T a, T b)
{
if (a < 0)return lcm(-a, b);
if (b < 0)return lcm(a, -b);
return a * (b / gcd(a, b));
}
template<class T> inline T euclide(T a, T b, T &x, T &y)
{
if (a < 0) {
T d = euclide(-a, b, x, y);
x = -x;
return d;
}
if (b < 0) {
T d = euclide(a, -b, x, y);
y = -y;
return d;
}
if (b == 0) {
x = 1;
y = 0;
return a;
} else {
T d = euclide(b, a % b, x, y);
T t = x;
x = y;
y = t - (a / b) * y;
return d;
}
}
template<class T> inline vector<pair<T, int> > factorize(T n)
{
vector<pair<T, int> > R;
for (T i = 2; n > 1;) {
if (n % i == 0) {
int C = 0;
for (; n % i == 0; C++, n /= i);
R.push_back(make_pair(i, C));
}
i++;
if (i > n / i) i = n;
}
if (n > 1) R.push_back(make_pair(n, 1));
return R;
}
template<class T> inline T eularFunction(T n)
{
vector<pair<T, int> > R = factorize(n);
T r = n;
for (int i = 0; i < R.size(); i++)r = r / R[i].first * (R[i].first - 1);
return r;
}
// -------------------- Cac ham ve ma tran --------------------
const int MaxMatrixSize = 40;
template<class T> inline void showMatrix(int n, T A[MaxMatrixSize][MaxMatrixSize])
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)cout << A[i][j];
cout << endl;
}
}
template<class T> inline void identityMatrix(int n, T A[MaxMatrixSize][MaxMatrixSize])
{
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) A[i][j] = (i == j) ? 1 : 0;
}
template<class T> inline void addMatrix(int n, T C[MaxMatrixSize][MaxMatrixSize], T A[MaxMatrixSize][MaxMatrixSize], T B[MaxMatrixSize][MaxMatrixSize])
{
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) C[i][j] = A[i][j] + B[i][j];
}
template<class T> inline void subMatrix(int n, T C[MaxMatrixSize][MaxMatrixSize], T A[MaxMatrixSize][MaxMatrixSize], T B[MaxMatrixSize][MaxMatrixSize])
{
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) C[i][j] = A[i][j] - B[i][j];
}
template<class T> inline void mulMatrix(int n, T C[MaxMatrixSize][MaxMatrixSize], T _A[MaxMatrixSize][MaxMatrixSize], T _B[MaxMatrixSize][MaxMatrixSize])
{
T A[MaxMatrixSize][MaxMatrixSize], B[MaxMatrixSize][MaxMatrixSize];
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) A[i][j] = _A[i][j], B[i][j] = _B[i][j], C[i][j] = 0;
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) C[i][j] += A[i][k] * B[k][j];
}
template<class T> inline void addModMatrix(int n, T m, T C[MaxMatrixSize][MaxMatrixSize], T A[MaxMatrixSize][MaxMatrixSize], T B[MaxMatrixSize][MaxMatrixSize])
{
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) C[i][j] = checkMod(A[i][j] + B[i][j], m);
}
template<class T> inline void subModMatrix(int n, T m, T C[MaxMatrixSize][MaxMatrixSize], T A[MaxMatrixSize][MaxMatrixSize], T B[MaxMatrixSize][MaxMatrixSize])
{
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) C[i][j] = checkMod(A[i][j] - B[i][j], m);
}
template<class T> inline void mulModMatrix(int n, T m, T C[MaxMatrixSize][MaxMatrixSize], T _A[MaxMatrixSize][MaxMatrixSize], T _B[MaxMatrixSize][MaxMatrixSize])
{
T A[MaxMatrixSize][MaxMatrixSize], B[MaxMatrixSize][MaxMatrixSize];
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) A[i][j] = _A[i][j], B[i][j] = _B[i][j], C[i][j] = 0;
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) C[i][j] = (C[i][j] + multiplyMod(A[i][k], B[k][j], m)) % m;
}
// -------------------- Cac ham ve hinh hoc --------------------
double dist(ii a, ii b) {
return sqrt(sqr((double)(a.fi) - (double)(b.fi)) + sqr((double)(a.se) - (double)(b.se)));
}
double distR(ii a, ii b) {
return sqr((double)(a.fi) - (double)(b.fi)) + sqr((double)(a.se) - (double)(b.se));
}
int cross(ii c, ii a, ii b) {
return (a.fi - c.fi)*(b.se - c.se)-(b.fi - c.fi)*(a.se - c.se);
}
int crossOper(ii c, ii a, ii b)
{
double t = (a.fi - c.fi)*(b.se - c.se)-(b.fi - c.fi)*(a.se - c.se);
if (fabs(t) <= eps) return 0;
return (t < 0) ? -1 : 1;
}
bool isIntersect(ii a, ii b, ii c, ii d)
{
return crossOper(a, b, c) * crossOper(a, b, d) < 0 && crossOper(c, d, a) * crossOper(c, d, b) < 0;
}
bool isMiddle(double s, double m, double t) {
return fabs(s - m) <= eps | fabs(t - m) <= eps | (s < m) != (t < m);
}
// -------------------- Cac ham ve chuyen doi giua cac kieu --------------------
bool isUpperCase(char c) {
return c >= 'A' && c <= 'Z';
}
bool isLowerCase(char c) {
return c >= 'a' && c <= 'z';
}
bool isLetter(char c) {
return c >= 'A' && c <= 'Z' | c >= 'a' && c <= 'z';
}
bool isDigit(char c) {
return c >= '0' && c <= '9';
}
char toLowerCase(char c) {
return (isUpperCase(c)) ? (c + 32) : c;
}
char toUpperCase(char c) {
return (isLowerCase(c)) ? (c - 32) : c;
}
template<class T> string toString(T n) {
ostringstream ost;
ost << n;
ost.flush();
return ost.str();
}
int toInt(string s) {
int r = 0;
istringstream sin(s);
sin >> r;
return r;
}
int64 toInt64(string s) {
int64 r = 0;
istringstream sin(s);
sin >> r;
return r;
}
double toDouble(string s) {
double r = 0;
istringstream sin(s);
sin >> r;
return r;
}
template<class T> void stoa(string s, int &n, T A[]) {
n = 0;
istringstream sin(s);
for (T v; sin >> v; A[n++] = v);
}
template<class T> void atos(int n, T A[], string &s) {
ostringstream sout;
for (int i = 0; i < n; i++) {
if (i > 0)sout << ' ';
sout << A[i];
}
s = sout.str();
}
template<class T> void atov(int n, T A[], vector<T> &vi) {
vi.clear();
for (int i = 0; i < n; i++) vi.push_back(A[i]);
}
template<class T> void vtoa(vector<T> vi, int &n, T A[]) {
n = vi.size();
for (int i = 0; i < n; i++)A[i] = vi[i];
}
template<class T> void stov(string s, vector<T> &vi) {
vi.clear();
istringstream sin(s);
for (T v; sin >> v; vi.push_back(v));
}
template<class T> void vtos(vector<T> vi, string &s) {
ostringstream sout;
for (int i = 0; i < vi.size(); i++) {
if (i > 0)sout << ' ';
sout << vi[i];
}
s = sout.str();
}
// -------------------- Cac ham ve phan so --------------------
template<class T> struct Fraction {
T a, b;
Fraction(T a = 0, T b = 1);
string toString();
};
template<class T> Fraction<T>::Fraction(T a, T b) {
T d = gcd(a, b);
a /= d;
b /= d;
if (b < 0) a = -a, b = -b;
this->a = a;
this->b = b;
}
template<class T> string Fraction<T>::toString() {
ostringstream sout;
sout << a << "/" << b;
return sout.str();
}
template<class T> Fraction<T> operator+(Fraction<T> p, Fraction<T> q) {
return Fraction<T > (p.a * q.b + q.a * p.b, p.b * q.b);
}
template<class T> Fraction<T> operator-(Fraction<T> p, Fraction<T> q) {
return Fraction<T > (p.a * q.b - q.a * p.b, p.b * q.b);
}
template<class T> Fraction<T> operator*(Fraction<T> p, Fraction<T> q) {
return Fraction<T > (p.a * q.a, p.b * q.b);
}
template<class T> Fraction<T> operator/(Fraction<T> p, Fraction<T> q) {
return Fraction<T > (p.a * q.b, p.b * q.a);
}
// =========================== Ket thuc template ===========================
#define N 1005
int n = in(), m = in(), k = in(), x, y;
int A
, vtCot
, vtHang
;
char q;
int main(){
//freopen ("input.txt", "r", stdin);
//freopen ("output.txt", "w", stdout);
FORN (i, 0, n)
FORN (j, 0, m)
A[i][j] = in();
FORN (i, 0, n) vtHang[i] = i;
FORN (i, 0, m) vtCot[i] = i;
FORN (i, 0, k){
q = getchar(); x = in() - 1; y = in() - 1;
if (q == 'r') swap(vtHang[x], vtHang[y]);
if (q == 'c') swap(vtCot[x], vtCot[y]);
if (q == 'g')
printf("%d\n", A[vtHang[x]][vtCot[y]]);
}
}
大师B:
/*最快!250ms,5400KB*/ #include <set> #include <bitset> #include <queue> #include <deque> #include <stack> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cstdlib> #include <cmath> #include <ctime> #include <cstring> #include <string> #include <cassert> #include <vector> #include <list> #include <map> #include <algorithm> #include <functional> #include <numeric> #include <utility> // DEFINE #define FOR(i,a,b) for (int i=(a),_b=(b); i<_b; i++) #define RFOR(i,b,a) for (int i=(b)-1,_a=(a); i>=_a; i--) #define RESET(a,b) memset((a),(b),sizeof(a)) #define fi first #define se second #define foreach(i, c) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) // using namespace std; typedef vector <int> vi; typedef vector <vi> vvi; typedef pair <int, int> ii; typedef vector <ii> vii; typedef vector <string> vs; typedef long long int64; //NOTES:int64 typedef unsigned long long uint64; //NOTES:uint64 typedef unsigned uint; const double pi = acos(-1.0); //NOTES:pi const double eps = 1e-11; //NOTES:eps const int MAXI = 0x7fffffff; int in() { int x = 0, c; for (; (uint)((c = getchar()) - '0') >= 10; ) { if (c == '-') return -in(); if (!~c) throw ~0; } do { x = (x << 3) + (x << 1) + (c - '0'); } while ((uint)((c = getchar()) - '0') < 10); return x; } // CODE int main(int argc, char *argv[]) { long col, row, k; cin >> row >> col; k = in(); int r[row], c[col], a[row][col]; FOR(i,0,row) FOR(j,0,col) a[i][j] = in(); FOR(i,0,row) r[i] = i; FOR(i,0,col) c[i] = i; FOR(i,0,k) { char s; int x, y; s = getchar(); x = in() - 1; y = in() - 1; if(s == 'g') cout << a[r[x]][c[y]] << endl; if(s == 'r') swap(r[x],r[y]); if(s == 'c') swap(c[x],c[y]); } return 0; }
大师C:
/*270ms,5500KB*/ #include <sstream> #include <iomanip> #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <algorithm> #include <vector> #include <set> #include <map> #include <stack> #include <queue> #include <string> #include <deque> #include <complex> #define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++) #define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--) #define REP(i,a) for(int i=0,_a=(a); i<_a; i++) #define ll long long #define F first #define S second #define PB push_back #define MP make_pair #define DEBUG(x) cout << #x << " = "; cout << x << endl; #define PR(a,n) cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; #define PR0(a,n) cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; using namespace std; //Buffer reading int INP,AM; #define BUFSIZE (1<<12) char BUF[BUFSIZE+1], *inp=BUF; #define GETCHAR(INP) { \ if(!*inp) { \ if (feof(stdin)) memset(BUF, 0, sizeof BUF); else fread(BUF,1,BUFSIZE,stdin); \ inp=BUF; \ } \ INP=*inp++; \ } #define DIG(a) (((a)>='0')&&((a)<='9')) #define GN(j) { \ AM=0;\ GETCHAR(INP); while(!DIG(INP) && INP!='-') GETCHAR(INP);\ if (INP=='-') {AM=1;GETCHAR(INP);} \ j=INP-'0'; GETCHAR(INP); \ while(DIG(INP)){j=10*j+(INP-'0');GETCHAR(INP);} \ if (AM) j=-j;\ } //End of buffer reading const long double PI = acos((long double) -1.0); int col[1011], row[1011], a[1011][1011], m, n, q; int main() { GN(m); GN(n); GN(q); FOR(i,1,m) FOR(j,1,n) GN(a[i][j]); FOR(i,1,m) row[i] = i; FOR(j,1,n) col[j] = j; int i, j; while (q--) { char c = '#'; while (c < 'a' || c > 'z') GETCHAR(c); if (c == 'g') { GN(i); GN(j); printf("%d\n", a[row[i]][col[j]]); } else if (c == 'r') { GN(i); GN(j); swap(row[i], row[j]); } else { GN(i); GN(j); swap(col[i], col[j]); } } return 0; }
大师D:
/*270ms,5400KB*/ #include <stdio.h> #include <iostream> #include <map> #include <bitset> #include <vector> #include <algorithm> #include <queue> #include <cstring> #include <cstdlib> #include <cmath> #include <set> #include <ctime> using namespace std; # define f(i,a,b) for(typeof( b ) i=a;i<b;i++) # define TT "\t" # define Clear(x) memset(x,0,sizeof(x)) # define fill(x,a) memset(x,a,sizeof(x)) # define pb push_back # define mp make_pair # define X first # define Y second # define inf 2147000000 # define linf 9223372036854770000LL # define sqr(x) ((x)*(x)) typedef unsigned long long ull; typedef long long ll; typedef pair<int, int> ii; typedef vector < pair<int, int > > vii; typedef vector<int> vi; template <class T> inline void ri(T &i) { bool minus = false; char c; for (c = getchar(); (c < '0' || c > '9') && (c != '-'); c = getchar()); if (c == '-') { minus = true; c = '0'; } for (i = 0; c >= '0' && c <= '9'; c = getchar()) i = (i << 3) + (i << 1) + (c - 48); if (minus)i = (~i) + 1; } int a[1002][1002]; int col[1002]; int row[1002]; int main() { int n, m, k; ri(n); ri(m); ri(k); f(i, 0, n) { f(j, 0, m) { ri(a[i][j]); } } f(i, 0, n) { row[i] = i; } f(i, 0, m)col[i] = i; int x, y; char c; f(j, 0, k) { c = getchar(); ri(x); ri(y); x--; y--; if (c == 'c') { swap(col[x], col[y]); } else if (c == 'r') { swap(row[x], row[y]); } else { x = row[x]; y = col[y]; printf("%d\n", a[x][y]); } } return 0; }
大师E:极简主义
/*270ms,5400KB*/ #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; int GetNumber() { char c;while(c=getchar(),c<'0'||c>'9'); int X=c-48;while(c=getchar(),c>='0'&&c<='9')X=X*10+c-48; return X; } int main() { int M=GetNumber(),N=GetNumber(),Query=GetNumber(); static int A[1000][1000]; for(int i=0;i<M;i++) for(int j=0;j<N;j++) A[i][j]=GetNumber(); static int X[1000],Y[1000]; for(int i=0;i<M;i++)X[i]=i; for(int i=0;i<N;i++)Y[i]=i; while(Query--) { char c;while(c=getchar(),c<'a'||c>'z'); int a=GetNumber()-1,b=GetNumber()-1; if(c=='r')swap(X[a],X[b]); if(c=='c')swap(Y[a],Y[b]); if(c=='g')printf("%d\n",A[X[a]][Y[b]]); } return 0; }
大师F:神一般的内存优化
/*280ms,3900KB*/ #include<bits/stdc++.h> using namespace std; typedef unsigned int uint; char p; void inp(int &n) { for (; ((p < '0') | (p > '9')) & (p != EOF); p = getchar()); for (n = 0; p >= '0' && p <= '9'; n = n * 10 + p - 48, p = getchar()); } int inp() { int n = 0; for (; (uint)((p = getchar()) - '0') > 9;) { if (p == '-') return -inp(); //if (!~c) throw ~0; } for (n = p - '0'; (uint)((p = getchar()) - '0') < 10; n = n * 10 + p - '0'); return n; } void inp(char &c) { for (p = getchar(); (p <= 32) & (p != EOF); p = getchar()); c = p; } int main() { int n, m, k, i, j; //inp(n),inp(m),inp(k); n = inp(), m = inp(), k = inp(); int x , y[m], mapa [m]; char c; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) //inp(mapa[i][j]); mapa[i][j] = inp(); x[i] = i; } for (j = 0; j < m; j++) y[j] = j; while (k--) { inp(c); //inp(i),inp(j); i = inp(), j = inp(); if (c == 'r') swap(x[i - 1], x[j - 1]); else if (c == 'c') swap(y[i - 1], y[j - 1]); else printf("%d\n", mapa[x[i - 1]][y[j - 1]]); } return 0; }
大师G:
/*310ms,5500KB*/
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;
#define Rep(i,n) for(__typeof(n) i = 0; i < (n); ++i)
#define Repd(i,n) for(__typeof(n) i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(__typeof(b) i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(__typeof(a) i = (a); i >= (b); --i)
#define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define ms(a,x) memset(a, x, sizeof(a))
#define nl puts("")
template<class T> void upmin(T &t, T f)
{
if (t > f) t = f;
}
template<class T> void upmax(T &t, T f)
{
if (t < f) t = f;
}
template<class F, class T> T convert(F a, int p = -1)
{
stringstream ss;
if (p >= 0) ss << fixed << setprecision(p);
ss << a;
T r;
ss >> r;
return r;
}
template<class T> void db(T a, int p = -1)
{
if (p >= 0) cout << fixed << setprecision(p);
cout << a << " ";
}
template<class T> T gcd(T a, T b)
{
T r;
while (b != 0)
{
r = a % b;
a = b;
b = r;
}
return a;
}
template<class T> T lcm(T a, T b)
{
return a / gcd(a, b) * b;
}
template<class T> T sqr(T x)
{
return x * x;
}
template<class T> T cube(T x)
{
return x * x * x;
}
template<class T> struct Triple
{
T x, y, z;
Triple() {} Triple(T _x, T _y, T _z) : x(_x), y(_y), z(_z) {}
};
template<class T> Triple<T> euclid(T a, T b)
{
if (b == 0) return Triple<T>(1, 0, a);
Triple<T> r = euclid(b, a % b);
return Triple<T>(r.y, r.x - a / b * r.y, r.z);
}
template<class T> int getbit(T s, int i)
{
return (s >> i) & 1;
}
template<class T> T onbit(T s, int i)
{
return s | (T(1) << i);
}
template<class T> T offbit(T s, int i)
{
return s & (~(T(1) << i));
}
template<class T> int cntbit(T s)
{
return s == 0 ? 0 : cntbit(s >> 1) + (s & 1);
}
const int bfsz = 1 << 16;
char bf[bfsz + 5];
int rsz = 0;
int ptr = 0;
char gc()
{
if (rsz <= 0)
{
ptr = 0;
rsz = (int) fread(bf, 1, bfsz, stdin);
if (rsz <= 0) return EOF;
} --rsz;
return bf[ptr++];
}
void ga(char &c)
{
c = EOF;
while (!isalpha(c)) c = gc();
}
int gs(char s[])
{
int l = 0;
char c = gc();
while (isspace(c)) c = gc();
while (c != EOF && !isspace(c))
{
s[l++] = c;
c = gc();
}
s[l] = '\0';
return l;
}
template<class T> bool gi(T &v)
{
v = 0;
char c = gc();
while (c != EOF && c != '-' && !isdigit(c)) c = gc();
if (c == EOF) return false;
bool neg = c == '-';
if (neg) c = gc();
while (isdigit(c))
{
v = v * 10 + c - '0';
c = gc();
}
if (neg) v = -v;
return true;
}
const double PI = 2 * acos(0);
const string months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int dr[] = { -1, 0, +1, 0};
const int dc[] = {0, +1, 0, -1};
const int inf = (int)1e9 + 5;
const ll linf = (ll)1e17 + 5;
const double eps = 1e-9;
#define maxn 1005
int n, m, k, a[maxn][maxn], row[maxn], col[maxn];
int main()
{
// freopen("in.txt", "r", stdin);
gi(n);
gi(m);
gi(k);
For(i, 1, n) For(j, 1, m) gi(a[i][j]);
For(i, 1, n) row[i] = i;
For(j, 1, m) col[j] = j;
char op;
int x, y;
while (k--)
{
ga(op);
gi(x);
gi(y);
if (op == 'c') swap(col[x], col[y]);
else if (op == 'r') swap(row[x], row[y]);
else printf("%d\n", a[row[x]][col[y]]);
}
return 0;
}
相关文章推荐
- 一笔画游戏路径搜索代码<未优化>
- Educational Codeforces Round 40 (Rated for Div. 2) F. Runner's Problem 前缀和求区间覆盖 矩阵快速幂优化DP 离散化
- Codeforces Round #291 (Div. 2) E. Darth Vader and Tree(矩阵优化DP)
- Codeforces Round #341 (Div. 2) E. Wet Shark and Blocks(矩阵优化DP)
- 伪数组代码&&矩阵相乘
- 【矩阵乘法】【快速幂】【递推】斐波那契数列&&矩乘优化递推模板
- Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo 矩阵快速幂优化dp
- <代码收藏>矩阵相乘
- Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations 矩阵快速幂优化dp
- JS使用&&和||来优化代码
- JavaScript 代码性能优化总结 && 一个最简单的web服务器
- hdu 2276 Kiki & Little Kiki 2(矩阵优化递推)
- 这句代码什么意思?img, div, input { behavior: url("iepngfix.htc") }
- BestCoder Round #68 (div.1) B 矩阵乘法优化DP
- Code Review&编程习惯&代码质量优化
- 关于小话C语言集合贴,C 语言常见问题集,c语言-优化C代码常用的几招,高效程序的秘密_hacker's delight读书笔记
- 【.Net码农】Asp.Net异常:"由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值"的解决方法
- Codeforces Round #426 (Div. 2) C. The Meaningless Game & A, B题代码及错误总结
- foj 1692 矩阵快速幂&&循环矩阵优化
- Codeforces Round #341 (Div. 2) E. Wet Shark and Blocks(矩阵优化DP)★