Codeforces Round #367 (Div. 2) E. Working routine (十字链表)

传送门:E. 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 andm 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 ≤ 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个操作,每次操作需要交换矩阵的两个子矩形,问:经过q次操作后,最终矩阵长什么样子。

思路:

1000×1000 的矩阵啊, n2 操作肯定是要T的。考虑链表将整个矩阵串在一起,如下图:



发现对于任意需要我们交换的矩阵,其实我们只需要改变这两个矩阵的周围一圈元素的指针指向即可完成两个矩阵的交换。单次操作复杂度就降到了O(n + m)了。另外我们只需要知道元素的右边下边是什么,并不用关心上方,左方是什么,所以只用存储两个方向即可。


代码:

#include <bits/stdc++.h>
using  namespace  std;

template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}

const int N=1111;
struct P{
  int val;
  int dir[2];  // 0: right; 1: down
}p[N*N];
int m;

int change(int i,int j){
  return i*m+j;
}

inline void Goto(int& pos,int x,int y){  //到指定点左上角
  for(int i=1; i<x; i++)pos=p[pos].dir[1];
  for(int i=1; i<y; i++)pos=p[pos].dir[0];
}

inline void shift(int p1, int p2,int h,int w,int s){
  for(int i=0; i<h; i++){
    p1=p[p1].dir[s];
    p2=p[p2].dir[s];
    swap(p[p1].dir[1-s], p[p2].dir[1-s]);
  }
  s=1-s;
  for(int i=0; i<w; i++){
    p1=p[p1].dir[s];
    p2=p[p2].dir[s];
    swap(p[p1].dir[1-s], p[p2].dir[1-s]);
  }
}

int  main(){
  int n,q; read(n);read(m);read(q);
  for(int i=1; i<=n ;i++)
    for(int j=1; j<= m; j++)
      read(p[change(i, j)].val);
  for(int i=0; i<=n ;i++){
    for(int j=0; j<=m ; j++){
      p[change(i, j)].dir[0]=change(i, j+1);
      p[change(i, j)].dir[1]=change(i+1, j);
    }
  }
  int x1,x2,y1,y2,h,w;
  while(q--){
    read(x1);read(y1);read(x2);read(y2);read(h);read(w);
    int p1=0,p2=0;
    Goto(p1, x1, y1);
    Goto(p2, x2, y2);
    shift(p1, p2, h, w, 1);
    shift(p1, p2, w, h, 0);
  }
  int pos=0;
  for(int i=1; i<=n; i++){
    pos=p[pos].dir[1];
    int tmp=pos;
    for(int j=1; j<=m; j++){
      tmp=p[tmp].dir[0];
      printf("%d ",p[tmp].val);
    }
    puts("");
  }
  return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值