【Codeforces Round 367 (Div 2) E】【十字链表 边框维护】Working routine nm矩形交换q次子矩形的最终矩形

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


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 1010, M = N*N, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int n, m, q;
int v[N][N];
struct node
{
	int v;
	int r, b;
}a[N * N];
int O(int y, int x)
{
	return y*(m + 1) + x;
}
void print()
{
	int p = 0;
	for (int i = 1; i <= n; ++i)
	{
		p = a[p].b;
		int q = p;
		for (int j = 1; j <= m; ++j)
		{
			q = a[q].r;
			printf("%d ", a[q].v);
		}puts("");
	}
}
int main()
{
	while (~scanf("%d%d%d", &n, &m, &q))
	{
		for (int i = 1; i <= n; ++i)
		{
			for (int j = 1; j <= m; ++j)
			{
				scanf("%d", &a[O(i,j)].v);
			}
		}
		for (int i = 0; i <= n; ++i)
		{
			for (int j = 0; j <= m; ++j)
			{
				a[O(i, j)].r = O(i, j + 1);
				a[O(i, j)].b = O(i + 1, j);
			}
		}
		while (q--)
		{
			int y1, x1, y2, x2, h, w;
			scanf("%d%d%d%d%d%d", &y1, &x1, &y2, &x2, &h, &w);
			int p1 = 0;
			for (int i = 1; i < y1; ++i)p1 = a[p1].b;
			for (int i = 1; i < x1; ++i)p1 = a[p1].r;
			int p2 = 0;
			for (int i = 1; i < y2; ++i)p2 = a[p2].b;
			for (int i = 1; i < x2; ++i)p2 = a[p2].r;

			int t1 = p1; 
			int t2 = p2;
			for (int i = 1; i <= h; ++i)
			{
				t1 = a[t1].b;
				t2 = a[t2].b;
				swap(a[t1].r, a[t2].r);
			}
			for (int i = 1; i <= w; ++i)
			{
				t1 = a[t1].r;
				t2 = a[t2].r;
				swap(a[t1].b, a[t2].b);
			}
			t1 = p1;
			t2 = p2;
			for (int i = 1; i <= w; ++i)
			{
				t1 = a[t1].r;
				t2 = a[t2].r;
				swap(a[t1].b, a[t2].b);
			}
			for (int i = 1; i <= h; ++i)
			{
				t1 = a[t1].b;
				t2 = a[t2].b;
				swap(a[t1].r, a[t2].r);
			}
		}
		print();
	}
	return 0;
}
/*
【trick&&吐槽】
这道题特别强调了——
It's guaranteed that two rectangles in one query do not overlapand 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.

不overlap很好理解
我这种写法,哪怕touch也不会影响其正确性的。

【题意】
有一个n*m(1000*1000)的矩阵
我们做了q(10000)次操作
每次操作交换两个大小相同的子矩阵
让你输出所有操作完成之后的矩阵

【类型】
十字链表 边框维护

【分析】
这道题很容易想到链表的操作方法,
但是我想的是横向的链表。
然而链表实现的话,无法动态维护下标,我们要如何找到要交换位置的起始位置呢?
我们很容易想到一行行暴力走,一直走到要交换位置的这个方法,
然而其复杂度是可达到O(h * m)的。

这里的解决办法是,引入十字链表
单向的足以,即我们维护每个节点的下一个和右一个节点。
然后找到2个交换矩阵左上角的位置,再暴力更新十字链表即可。


*/


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值