【优化&大师级代码欣赏】Codeforces Round #137 (Div. 2) / 222B Cosmic Tables (矩阵)

Kylin-Desktop-V10-SP1-General-Release-2303-X86_64-海光版(hygon c86)安装与配置指南 本文提供了在国产海光C86处理器平台上安装与配置Kylin Desktop V10 SP1操作系统的详细指南。内容涵盖从下载专用镜像、制作启动盘、BIOS关键设置、磁盘分区方案,到系统安装后的驱动优化、必备软件安装及常见故障排除,旨在帮助用户顺利完成hygon c86平台的系统部署与优化。 阅读详情

B. Cosmic Tables
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[N][N], vtCot[N], vtHang[N];
    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[n], y[m], mapa[n][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;
}



微信小程序URL Link全解析:从生成原理到实战应用(附Java代码) 本文深入解析微信小程序URL Link的生成原理与实战应用。URL Link是微信官方提供的智能路由协议,通过中间页实现外部链接安全跳转至小程序指定页面。文章详细阐述了其权限模型、环境版本差异及跳转机制,并提供了包含令牌管理、异常处理和性能优化的健壮Java实现方案,助力开发者构建高可用的链接生成服务。 阅读详情

相关推荐

手机相机基础技术解析:从硬件到算法的全面指南

手机相机的成像过程可简化为的链路,其核心硬件包括 镜头、CMOS传感器、ISP(图像信号处理器) 以及配套的软件算法。

weixin_44239603的博客 2457

Codeforces Round #137 (Div. 2)——B

B. Cosmic Tables 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

Learn as if you were to live forever 439

千兆网口电路设计中的EMC防护与TVS选型实战

本文深入探讨了千兆网口电路设计中面临的EMC挑战,并提供了TVS二极管选型的实战指南。文章分析了ESD、浪涌和EFT等主要威胁,重点阐述了如何根据工作电压、钳位电压和关键的低结电容等参数为PHY芯片选择匹配的TVS器件,以确保在严苛电磁环境下实现可靠的信号完整性与防护。

m3n5b7v8c9x的博客 536

Cosmic Tables

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. UC...

weixin_43244265的博客 174

【ROS2】Arduino系列之机器人底盘实现

介绍ros中提供的一个已经封装了的模块: ros_arduino_bridge,该模块由下位机驱动和上位机控制两部分组成,通过该模块可以更为快捷、方便的实现自己的机器人平台

guilin 1489

OpenCV实战:高效检测与识别多类型条码与二维码

本文详细介绍了如何利用OpenCV的BarcodeDetector和QRCodeDetector模块,高效实现EAN-13条形码与常见二维码的检测与识别。内容涵盖从环境搭建、单码识别到处理多二维码的完整流程,并分享了提升识别率的图像预处理与性能优化等实战技巧,帮助开发者一站式解决视觉项目中的条码识别需求。

weixin_29094539的博客 503

【CodeForces】222B - Cosmic Tables(思维)

点击打开题目 B. Cosmic Tables time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output The Free Meteor Association

wyg1997 784

Arduino粉尘传感器DIY:打造智能PM2.5监测仪

本文详细介绍了如何利用Arduino和粉尘传感器DIY一个智能PM2.5监测仪。从硬件选购、接线步骤到代码编写,提供了完整的制作指南,帮助读者以低成本打造透明可靠的空气质量监测设备,并探讨了数据解读与智能升级方案。

i3j4k5的博客 396

平常水题 - CodeForces - 222B

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.

dreams___的博客 600

使用 MATLAB/Simulink + Deep Learning Toolbox,构建一个基于LSTM的电网负荷预测模

本文介绍了一个基于Simulink和深度学习的智能电网负荷预测与调度建模方法。通过LSTM神经网络构建负荷预测模型,将历史负荷、天气、日期等特征作为输入,预测未来24小时负荷。然后将训练好的模型集成到Simulink仿真系统中,结合发电单元模型和经济调度逻辑,实现预测驱动的电力调度优化。实验结果表明,该方法预测误差在2.5%-4.0%之间,相比传统调度方式可降低15%-25%的运行成本。文章详细展示了从数据准备、特征工程、模型训练到系统集成的完整流程,为智能电网中AI技术的应用提供了实用参考。

amy_mhd的博客 174

Agentic RL: 大模型后训练 SFT、PPO和GRPO

本文介绍了从LLM训练到Agentic RL的发展过程。传统LLM训练包括预训练和后训练两个阶段,其中后训练通过监督微调、奖励建模和强化学习等技术优化模型表现。针对智能体在复杂任务中的不足,提出了Agentic RL新范式,将LLM嵌入强化学习框架,通过多步交互、工具使用和环境反馈来优化长期目标。相比传统单轮对话优化(PBRFT),Agentic RL具有状态包含历史上下文、行动类型多样、动态状态转移等特点,更适合解决需要多步推理和规划的任务。这种新方法为开发更强大的智能体系统提供了可能。

Cachel Wood的博客 705

记第一次打codeforces

昨天第一次打cf,虽然我的账号申请很长时间了,但是暑假一直在玩多校,然后被多校玩了一把,真爽。说实话,我现在真的很喜欢这种比赛,发自内心,有点困了,昨天打了一小时,看着ac了两道就关机了,其实有一道后来被hack了 然后因为我读题失误,造成了严重的掉分。3,4天一次的cf只要不是10:50的我以后必参加,我要开始上分了,上钻石,上大师。(梦想还是要有的) 今天做了一下昨天的题,我认为,我最多做

但求心安的博客 4376

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

0和5(思维-水题)

点击打开题目 I - 0和5  51Nod - 1433  题解: 各位加起来是9的倍数,因为是整除90,所以末尾至少有一个0; 代码: #include #include using namespace std; int main(){ int n; while(cin>>n){ int zore=0; int five=0;

Manton's blog 482

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

三个重要的同余式——威尔逊定理、费马小定理、欧拉定理 + 求幂大法的证明

一、威尔逊定理 若p为质数,则 p|(p-1)!+1 亦:(p-1)! ≡ p-1 ≡ -1(mod p) 例题: HDU 2973 YAPTCHA (威尔逊定理及其逆定理) 解题报告见http://blog.csdn.net/synapse7/article/details/18728157 二、费马小定理 假如p是质数,且gcd(a,p)=1,那么 a^(

AC,∑ndless 1万+

使用bitset实现二进制和十进制的相互转换

#include #include #include #include #include #include using namespace std; const int Size = 32; const double log_2 = log(2.0); char str[Size]; int main(void) { int num; char *endstr; while (~scan

AC,∑ndless 9684

动态规划解最长公共子序列(LCS)问题 (附可打印LCS完整代码)

动态规划法 经常会遇到复杂问题不能简单地分解成几个子问题,而会分解出一系列的子问题。简单地采用把大问题分解成子问题,并综合子问题的解导出大问题的解的方法,问题求解耗时会按问题规模呈幂级数增加。 为了节约重复求相同子问题的时间,引入一个数组,不管它们是否对最终解有用,把所有子问题的解存于该数组中,这就是动态规划法所采用的基本方法。 【问题】 求两字符序列的最长公共字

AC,∑ndless 9137

全排列的编码与解码——康托展开 (附完整代码)

一、康托展开:全排列到一个自然数的双射 X=an*(n-1)!+an-1*(n-2)!+...+ai*(i-1)!+...+a2*1!+a1*0! ai为整数,并且0  适用范围:没有重复元素的全排列 二、全排列的编码: {1,2,3,4,...,n}的排列总共有n!种,将它们从小到大排序,怎样知道其中一种排列是有序序列中的第几个? 如 {1,2,3} 按从小到大排列一共6个:

AC,∑ndless 8528

中国剩余定理算法详解 + POJ 1006 Biorhythms 生理周期

此题相当于解方程组 x+d≡p(mod 23) x+d≡e(mod 28) x+d≡i(mod 33) 由于23,28,33两两互素,所以M=23*28*33=21252,M1=924,M2=759,M3=644. 则有924y1≡1(mod 23),由924/23=(40,5,1,3)计算得y1=6(mod 23) 则有759y2≡1(mod 28),由759/28=(27,9,2,1)计算得y2=19(mod 28) 则有644y3≡1(mod 33),由644/33=(19,1,1,16)计算得y3=

AC,∑ndless 8346

最长递增子序列(LIS)的O(NlogN)打印算法

题目: 求一个一维数组arr[n]中的最长递增子序列的长度,如在序列1,5,8,3,6,7中,最长递增子序列长度为4 (即1,3,6,7)。 方法一:一般的DP方法(O(N^2)) 像LCS一样,从后向前分析,很容易想到,第i个元素之前的最长递增子序列的长度要么是1(单独成一个序列),要么就是第i-1个元素之前的最长递增子序列加1,这样得到状态方程:

AC,∑ndless 7453

最短路模板:使用priority_queue实现的Dijkstra算法

#include #include #include #include #include #include using namespace std; const int mx = 10005; typedef pair P; ///first是最短距离,second是顶点编号 struct edge { int to, cost; }; vector G[mx]; int disTo[mx]

AC,∑ndless 5007

Manacher 算法详解:O(n) 复杂度求最长回文子串

先预处理下:在每个字符的两边都插入一个特殊的符号,比如abba变成#a#b#b#a#,aba变成#a#b#a#(因为Manacher算法只能处理奇数长度的字符串)。同时,为了避免数组越界,在字符串开头添加另一特殊符号,比如$#a#b#a#。 以字符串3212343219为例,处理后变成S[] = "$#3#2#1#2#3#4#3#2#1#9#"。 然后用一个数组Len[i]来记录以字符S[i

AC,∑ndless 4611

扩展欧几里得算法&同余方程&模m乘法逆元详解

首先介绍扩展欧几里得定理: 对于两个不全为0的整数a,b,必存在一组解x,y,使得ax+by=gcd(a,b) 换句话说,形如ax+by的最小正整数等于gcd(a,b)。 实现代码如下:(一般题目都要用64位) (复杂度:O(log max(a,b)))

AC,∑ndless 4452

UVa 11827 Maximum GCD (gcd&读入技巧)

11827 - Maximum GCD Time limit: 1.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2927 Given the N integers, you have to f

AC,∑ndless 3391

O(NloglogN)素数筛法与O(N)素数筛法的对比测试

#include #include #include using namespace std; const int mx = 1000000 + 1; ///在(1,mx)的范围内寻找素数 const int sqrt_mx = (int)sqrt((double)mx); bool vis[mx]; int prime[mx / 10]; ///在mx>65000时建议写成 int pr

AC,∑ndless 3146

类型转换与字符串赋值、分割、合并、复制、比较、查询、翻转详解 (完整代码)

一、类型转换 char数组转int、__int64或double: #include #include #include #include char str[100]; int main(void) { char *stopstr; gets(str); if (strchr(str, '.') == NULL) { if (strlen(str) < 10) print

AC,∑ndless 3121

用getchar()和putchar()加速IO(含整型快速IO和浮点型快速IO)

****转载请注明http://blog.csdn.net/synapse7/article/details/19096049**** 概述:使用getchar()和putchar(),以及math.h头文件中的一些函数,基本实现了以下函数 scanf("%u",&x) scanf("%d",&x) scanf("%lf",&x) printf("%u",x) printf("%d",x

AC,∑ndless 2939

动态规划(DP)——入门篇(11.24更新)

零、先修课程 首先,在开始理解DP的思想前,你需要 1. 完成HDU里面的递推求解专题练习(For Beginner)那7道题(这些题很简单,题解请在博客中搜索),这对你理解DP有很大的帮助。 2. 对递归搜索(比如深度优先搜索,DFS)有一定了解。 一、递归与记忆化搜索 我们从POJ 3176入手来学习这一思想。(题目很短,请快速读完) 从上往下看,最大和值无非是往左走和往右

AC,∑ndless 2861
上一篇: Codeforces Round #136 (Div. 2) / 221B Little Elephant and Numbers (数论)
下一篇: Codeforces Round #137 (Div. 2) / 222A Shooshuns and Sequence (模拟)
synapse7
博客等级 码龄14年 432粉丝 701原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址