codeforces 367 E. Working routine

本文介绍了一种处理矩阵中子矩阵交换的高效算法。通过构建四个方向的链表,算法能在不改变子矩阵内部元素相对位置的情况下,快速完成两个不相交子矩阵的位置互换。该方法适用于解决特定类型的问题,如在大型数据集上进行复杂操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 ≤ 1000, 1 ≤ 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 ≤ n, 1 ≤ bi, di, wi ≤ m).

Output

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

Examples

input

Copy

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

Copy

4 4 3 3
4 4 3 3
2 2 1 1
2 2 1 1

input

Copy

4 2 1
1 1
1 1
2 2
2 2
1 1 4 1 1 2

output

Copy

2 2
1 1
2 2
1 1

题意:给一个矩阵,有q次操作,每次交换两个不相交的子矩阵

 

可以发现,每次交换时子矩阵内部位置关系是不变的,所以只要交换两个子矩阵的外部位置就行了,每个点开一个四个方向的链表,存每个方向的点,每次交换只要交换链表即可,具体看代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxm = 1005;
struct node
{
	int x, y;
	node() {};
	node(int x, int y) :x(x), y(y) {};
}U[maxm][maxm], D[maxm][maxm], L[maxm][maxm], R[maxm][maxm];
int f[maxm][maxm];
int main()
{
	int n, i, j, k, sum, m, Q;
	int a, b, c, d, h, w;
	node u, v, p, q;
	scanf("%d%d%d", &n, &m, &Q);
	for (i = 1;i <= n;i++)
		for (j = 1;j <= m;j++)
			scanf("%d", &f[i][j]);
	for (i = 0;i <= n + 1;i++)
	{
		for (j = 0;j <= m + 1;j++)
		{
			U[i][j] = node(i - 1, j), D[i][j] = node(i + 1, j);
			L[i][j] = node(i, j - 1), R[i][j] = node(i, j + 1);
		}
	}
	while (Q--)
	{
		scanf("%d%d%d%d%d%d", &a, &b, &c, &d, &h, &w);
		u.x = a, u.y = 0, v.x = c, v.y = 0;
		for (i = 1;i <= b;i++) u = R[u.x][u.y];
		for (i = 1;i <= d;i++) v = R[v.x][v.y];
		for (i = 1;i <= w;i++)
		{
			p = U[u.x][u.y], q = U[v.x][v.y];
			U[u.x][u.y] = q, U[v.x][v.y] = p;
			D[p.x][p.y] = v, D[q.x][q.y] = u;
			if (i < w) u = R[u.x][u.y], v = R[v.x][v.y];
		}
		for (i = 1;i <= h;i++)
		{
			p = R[u.x][u.y], q = R[v.x][v.y];
			R[u.x][u.y] = q, R[v.x][v.y] = p;
			L[p.x][p.y] = v, L[q.x][q.y] = u;
			if (i < h) u = D[u.x][u.y], v = D[v.x][v.y];
		}
		for (i = 1;i <= w;i++)
		{
			p = D[u.x][u.y], q = D[v.x][v.y];
			D[u.x][u.y] = q, D[v.x][v.y] = p;
			U[p.x][p.y] = v, U[q.x][q.y] = u;
			if (i < w) u = L[u.x][u.y], v = L[v.x][v.y];
		}
		for (i = 1;i <= h;i++)
		{
			p = L[u.x][u.y], q = L[v.x][v.y];
			L[u.x][u.y] = q, L[v.x][v.y] = p;
			R[p.x][p.y] = v, R[q.x][q.y] = u;
			if (i < h) u = U[u.x][u.y], v = U[v.x][v.y];
		}
	}
	for (i = 1;i <= n;i++)
	{
		p.x = i, p.y = 0;
		for (j = 1;j <= m;j++)
		{
			p = R[p.x][p.y];
			printf("%d%c", f[p.x][p.y], j == m ? '\n' : ' ');
		}
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值