[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 ai, bi, ci, di, hi, wi, 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 n, m 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 ai, bi, ci, di, hi, wi (1 ≤ ai, ci, hi ≤ n, 1 ≤ bi, di, wi ≤ m).
Output
Print n lines containing m integers each — the resulting matrix.
Example
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
4 4 3 3
4 4 3 3
2 2 1 1
2 2 1 1
4 2 1
1 1
1 1
2 2
2 2
1 1 4 1 1 2
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 }