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

Output

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

Example

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

[cf div 2 706E] Working routine的更多相关文章

  1. 【链表】【模拟】Codeforces 706E Working routine

    题目链接: http://codeforces.com/problemset/problem/706/E 题目大意: 给一个N*M的矩阵,Q个操作,每次把两个同样大小的子矩阵交换,子矩阵左上角坐标分别 ...

  2. CodeForces 706E Working routine

    十字链表. 开一个十字链表,矩阵中每一格作为一个节点,记录五个量: $s[i].L$:$i$节点左边的节点编号 $s[i].R$:$i$节点右边的节点编号 $s[i].U$:$i$节点上面的节点编号 ...

  3. Codeforces Round #377 (Div. 2)

    #include <iostream> #include <stdio.h> #include <string.h> using namespace std; in ...

  4. CF 989

    今天晚上闲来无事打了一场CF......div.2,第600名.太弱了. T1看懂题之后发现是水题(废话),6min AC. T2仔细思考之后发现可做,但是由于n=p的特判没确定到底有没有解,WA了一 ...

  5. Codeforces Round #466 (Div. 2) Solution

    从这里开始 题目列表 小结 Problem A Points on the line Problem B Our Tanya is Crying Out Loud Problem C Phone Nu ...

  6. chrome中不可见字符引发的float问题

    起因是刷知乎时碰到这么个问题:https://www.zhihu.com/question/41400503 问题代码如下: <!DOCTYPE html> <html lang=& ...

  7. 基于asp.net的ajax分页

    直接贴代码: <html> <head> <meta http-equiv="Content-Type" content="text/htm ...

  8. Codeforces round 1111

    CF Div 2 537 比赛链接 感觉题目难度OK,五个题都能做,后俩题考察人的翻译水平... 另外,$Claris$太强了... A 直接按照题意模拟,不知道为啥有人会被× 代码: #includ ...

  9. 一款基于jquery的鼠标经过图片列表特效

    今天要给大家推荐一款基于jquery的鼠标经过图片列表特效.当鼠标经过列表图片的时候,图片放大,且有一个半透明的遮罩层随之移动.效果图如下: 在线预览   源码下载 实现的代码 html代码: < ...

随机推荐

  1. REST风格的5条关键原则

    REST风格的5条关键原则包括: (1)网络上的所有事物都被抽象为资源. (2)每个资源对应一个唯一的资源标识. (3)通过通用的连接件接口对资源进行操作. (4)对资源的各种操作不会改变资源标识. ...

  2. nginx配置虚拟主机vhost的方法详解

    Nginx vhost配置,可实现基于ip.端口号.servername的虚拟主机,同时可避免直接修改主配置文件.在nginx下配置虚拟主机vhost非常方便.这篇文章主要介绍了nginx配置虚拟主机 ...

  3. 家庭记账本之微信小程序(八)

    寒假总结 寒假充满着腥风血雨,不过在努力下还是完成了寒假的任务,虽说没有出去找活干,毕竟在寒假这段时间不怎么好找,但是我在自己家的店里帮这父母工作了一段时间,也算是颇有收获,在短暂的学习后也算勉强完成 ...

  4. Servlet Analysis

    @WebServlet("/cdiservlet") //url映射 public class NewServlet extends HttpServlet { private M ...

  5. Python记录12:迭代器+生成器+生成式

    '''1. 什么是迭代器 什么是迭代:迭代就是一个重复的过程,但是每一次重复都是基于上一次的结果而进行的 单纯的重复不是迭代: while True: print(1) 迭代的过程 l=['a','b ...

  6. html5 javascript 事件练习1

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  7. HSV to RGB

    HSV构成: Hue : the color type (red, blue, or yellow) Ranges from 0 to 360° Saturation : the intensity ...

  8. 用PHP实现反向代理服务器

    什么是反向代理: 百度百科有云: 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给int ...

  9. jQuery 筛选器2

    jQuery 筛选器2 // 由于$()只能输入字符串$('#li:eq(1)'),可通过.eq()来传入. // 获取this标签中的指定属性 $(this).eq(1) // 获取第一个元素 $( ...

  10. Linux 查看磁盘使用情况

    Linux 查看磁盘使用情况 df 查看当前挂载空间使用情况 语法: df [选项]... [FILE]... 文件-a, --all 包含所有的具有 0 Blocks 的文件系统 文件--block ...