[cf div 2 706E] Working routine

[cf div 2 706E] Working routine

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 m columns 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 ≤ n1 ≤ bi, di, wi ≤ m).

Output

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

Example

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

这道题目续了我一晚上。

原本就想用链表,然后写炸了。

然后换了双向链表和树状数组,发现都wa on 4。

然后就发现了这样的做法有共同的漏洞,就是我的做法当一个位置上的权值改变再改变,我就没办法了。

怎么说呢?就是不支持动态维护(不然要O(n*m))。

然后就换用了十字链表。

我们只要维护矩阵中每一个点的右边和下边就可以了。然后0,n+1这种边界也要连边。

这样,维护一个矩形直接就把边界的信息修改一发。复杂度是O(q*(n+m))。

code:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N=2005;
 4 struct Mate {int v,dir[2];}a[N*N];
 5 int n,m,q;
 6 inline int read() {
 7     int x=0; char ch=getchar();
 8     while (ch<'0'||ch>'9') ch=getchar();
 9     while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
10     return x;
11 }
12 int id(int x,int y) {return x*(m+1)+y;}
13 void reach(int &p,int x,int y) {
14     for (int i=1; i<x; i++) p=a[p].dir[1];
15     for (int i=1; i<y; i++) p=a[p].dir[0];
16 }
17 void alter(int p1,int p2,int l1,int l2,int d) {
18     for (int i=0; i<l1; i++) {
19         p1=a[p1].dir[d],p2=a[p2].dir[d];
20         swap(a[p1].dir[1-d],a[p2].dir[1-d]);
21     }
22     d=1-d;
23     for (int i=0; i<l2; i++) {
24         p1=a[p1].dir[d],p2=a[p2].dir[d];
25         swap(a[p1].dir[1-d],a[p2].dir[1-d]);
26     }
27 }
28 int main() {
29     cin>>n>>m>>q;
30     for (int i=1; i<=n; i++)
31         for (int j=1; j<=m; j++) a[id(i,j)].v=read();
32     for (int i=0; i<=n; i++)
33         for (int j=0; j<=m; j++) {
34             a[id(i,j)].dir[0]=id(i,j+1);
35             a[id(i,j)].dir[1]=id(i+1,j);
36         }
37     for (int sx,sy,sp,tx,ty,tp,lx,ly; q; q--) {
38         sx=read(),sy=read(),sp=0;
39         tx=read(),ty=read(),tp=0;
40         lx=read(),ly=read();
41         reach(sp,sx,sy);
42         reach(tp,tx,ty);
43         alter(sp,tp,lx,ly,1);
44         alter(sp,tp,ly,lx,0);
45     }
46     for (int i=1,p=0,p0; i<=n; i++,puts("")) {
47         p=a[p].dir[1],p0=p;
48         for (int j=1; j<=m; j++) {
49             p0=a[p0].dir[0];
50             printf("%d ",a[p0].v);
51         }
52     }
53     return 0;
54 }
View Code

 

转载于:https://www.cnblogs.com/whc200305/p/7726227.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值