CodeForces 367E Working routine 十字链表

E. Working routine
time limit per test
2.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasiliy finally got to work, where there is a huge amount of tasks waiting for him. Vasiliy is given a matrix consisting of n rows and mcolumns and q tasks. Each task is to swap two submatrices of the given matrix.

For each task Vasiliy knows six integers aibicidihiwi, where ai is the index of the row where the top-left corner of the first rectangle is located, bi is the index of its column, ci is the index of the row of the top-left corner of the second rectangle, di is the index of its column,hi is the height of the rectangle and wi is its width.

It's guaranteed that two rectangles in one query do not overlap and do not touch, that is, no cell belongs to both rectangles, and no two cells belonging to different rectangles share a side. However, rectangles are allowed to share an angle.

Vasiliy wants to know how the matrix will look like after all tasks are performed.

Input

The first line of the input contains three integers nm and q (2 ≤ n, m ≤ 10001 ≤ q ≤ 10 000) — the number of rows and columns in matrix, and the number of tasks Vasiliy has to perform.

Then follow n lines containing m integers vi, j (1 ≤ vi, j ≤ 109) each — initial values of the cells of the matrix.

Each of the following q lines contains six integers aibicidihiwi (1 ≤ ai, ci, hi ≤ n1 ≤ bi, di, wi ≤ m).

Output

Print n lines containing m integers each — the resulting matrix.

Examples
input
4 4 2
1 1 2 2
1 1 2 2
3 3 4 4
3 3 4 4
1 1 3 3 2 2
3 1 1 3 2 2
output
4 4 3 3
4 4 3 3
2 2 1 1
2 2 1 1
input
4 2 1
1 1
1 1
2 2
2 2
1 1 4 1 1 2
output
2 2
1 1
2 2
1 1

题意:给你一个矩阵,要你进行q次操作,问操作以后的矩阵长啥样。操作为:选择两个子矩阵交换,保证两个子矩阵没有边的接触。

题解:弄个十字链表,每次暴力修改边界就可以了。

//************************************************************************//
//*Author : Handsome How                                                 *//
//************************************************************************//
//#pragma comment(linker, "/STA    CK:1024000000,1024000000")
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <ctime>
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) __typeof(v) r = (v)
#endif
#define foreach(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define fur(i,a,b) for(int i=(a);i<=(b);i++)
#define furr(i,a,b) for(int i=(a);i>=(b);i--)
#define cl(a) memset((a),0,sizeof(a))
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#ifdef HandsomeHow
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define dbg(x) cout << #x << " = " << x << endl
#else
#define debug(...)
#define dbg(x)
#endif
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;
const int inf=0x3f3f3f3f;
const double eps=1e-8;
const int mod=1000000007;
const double pi=acos(-1);
inline void gn(long long&x){
    int sg=1;char c;while(((c=getchar())<'0'||c>'9')&&c!='-');c=='-'?(sg=-1,x=0):(x=c-'0');
    while((c=getchar())>='0'&&c<='9')x=x*10+c-'0';x*=sg;
}
inline void gn(int&x){long long t;gn(t);x=t;}
inline void gn(unsigned long long&x){long long t;gn(t);x=t;}
ll gcd(ll a,ll b){return a? gcd(b%a,a):b;}
ll powmod(ll a,ll x,ll mod){ll t=1ll;while(x){if(x&1)t=t*a%mod;a=a*a%mod;x>>=1;}return t;}
// (づ°ω°)づe★
//-----------------------------------------------------------------
struct Node{
	int v;
	Node *l,*r,*up,*down;
}node[1111][1111];
int main(){
#ifdef HandsomeHow
    freopen("data.in","r",stdin);
    //freopen("data.out","w",stdout);
    time_t beginttt = clock();
#endif
	int n,m,q;
	gn(n);gn(m);gn(q);
	fur(i,1,n) node[i][0].r = &node[i][1];
	fur(i,1,n) node[i][m+1].l = &node[i][m];
	fur(i,1,m) node[0][i].down = &node[1][i];
	fur(i,1,m) node[n+1][i].up = &node[n][i];
	fur(i,1,n)fur(j,1,m){
		gn(node[i][j].v);
		node[i][j].l = &node[i][j-1];
		node[i][j].r = &node[i][j+1];
		node[i][j].up = &node[i-1][j];
		node[i][j].down = &node[i+1][j];
	}
	int a,b,c,d,h,w;
	while(q--){
		gn(a);gn(b);gn(c);gn(d);gn(h);gn(w);
		Node *p1 = &node[a][0], *p2 = &node[c][0];
		fur(i,1,b) p1 = p1->r;
		fur(i,1,d) p2 = p2->r;
		fur(i,1,w){
			swap(p1->up,p2->up);
			swap(p1->up->down,p2->up->down);
			p1 = p1->r;
			p2 = p2->r;
		}
		p1 = p1->l;
		p2 = p2->l;
		
		fur(i,1,h){
			swap(p1->r,p2->r);
			swap(p1->r->l,p2->r->l);
			p1 = p1->down;
			p2 = p2->down;
		}
		p1 = p1->up;
		p2 = p2->up;
		
		fur(i,1,w){
			swap(p1->down,p2->down);
			swap(p1->down->up,p2->down->up);
			p1 = p1->l;
			p2 = p2->l;
		}
		p1 = p1->r;
		p2 = p2->r;
		
		
		fur(i,1,h){
			swap(p1->l,p2->l);
			swap(p1->l->r,p2->l->r);
			p1 = p1->up;
			p2 = p2->up;
		}

	}
	fur(i,1,n){
		Node *p = node[i][0].r;
		fur(j,1,m){
			printf("%d ",p->v);
			p = p->r;
		}
		printf("\n");
	}
#ifdef HandsomeHow
	time_t endttt = clock();
    debug("time: %d\n",(int)(endttt - beginttt));
#endif
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值