[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
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
4 4 3 34 4 3 32 2 1 12 2 1 1
Input
4 2 11 11 12 22 21 1 4 1 1 2
Output
2 21 12 21 1

这道题目续了我一晚上。

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

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

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

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

然后就换用了十字链表。

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

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

code:

 #include<bits/stdc++.h>
 using namespace std;
 ;
 ];}a[N*N];
 int n,m,q;
 inline int read() {
     ; char ch=getchar();
     ') ch=getchar();
     +ch-',ch=getchar();
     return x;
 }
 )+y;}
 void reach(int &p,int x,int y) {
     ; i<x; i++) p=a[p].dir[];
     ; i<y; i++) p=a[p].dir[];
 }
 void alter(int p1,int p2,int l1,int l2,int d) {
     ; i<l1; i++) {
         p1=a[p1].dir[d],p2=a[p2].dir[d];
         swap(a[p1].dir[-d],a[p2].dir[-d]);
     }
     d=-d;
     ; i<l2; i++) {
         p1=a[p1].dir[d],p2=a[p2].dir[d];
         swap(a[p1].dir[-d],a[p2].dir[-d]);
     }
 }
 int main() {
     cin>>n>>m>>q;
     ; i<=n; i++)
         ; j<=m; j++) a[id(i,j)].v=read();
     ; i<=n; i++)
         ; j<=m; j++) {
             a[id(i,j)].dir[]=id(i,j+);
             a[id(i,j)].dir[]=id(i+,j);
         }
     for (int sx,sy,sp,tx,ty,tp,lx,ly; q; q--) {
         sx=read(),sy=read(),sp=;
         tx=read(),ty=read(),tp=;
         lx=read(),ly=read();
         reach(sp,sx,sy);
         reach(tp,tx,ty);
         alter(sp,tp,lx,ly,);
         alter(sp,tp,ly,lx,);
     }
     ,p=,p0; i<=n; i++,puts("")) {
         p=a[p].dir[],p0=p;
         ; j<=m; j++) {
             p0=a[p0].dir[];
             printf("%d ",a[p0].v);
         }
     }
     ;
 }

[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. java框架之SpringMVC(1)-入门&整合MyBatis

    前言 SpringMVC简介 SpringMVC 是一个类似于 Struts2 表现层的框架,属于 SpringFramework 的后续产品. 学习SpringMVC的原因 SpringMVC 与 ...

  2. input type = file 上传图片转为base64

    项目背景是做图片识别,接口需要上传图片格式为base64格式的,react项目的相关代码: let reader = new FileReader();reader.readAsDataURL(e.t ...

  3. 51nod1432贪心

    n个人,已知每个人体重.独木舟承重固定,每只独木舟最多坐两个人,可以坐一个人或者两个人.显然要求总重量不超过独木舟承重,假设每个人体重也不超过独木舟承重,问最少需要几只独木舟? Input 第一行包含 ...

  4. Java字节码浅析(二)

    英文原文链接,译文链接,原文作者:James Bloom,译者:有孚 条件语句 像if-else, switch这样的流程控制的条件语句,是通过用一条指令来进行两个值的比较,然后根据结果跳转到另一条字 ...

  5. 剑指offer(48)不用加减乘除做加法

    题目描述 写一个函数,求两个整数之和,要求在函数体内不得使用+.-.*./四则运算符号. 题目分析 不用加减乘除做加法,我第一时间想到的就是用位运算,毕竟计算机是二进制的,所有的操作都是以位运算为基础 ...

  6. NOI 2017 整数(线段树)

    题意 https://loj.ac/problem/2302 思路 拆分成每个二进制位的加减来考虑,维护那个整数的二进制位.不难发现,进位就是找右边第一个 \(0\) 的位置,并将其赋值为 \(1\) ...

  7. 拦截导弹nlogn解法

    题目 题目描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某天,雷达捕捉到敌国 ...

  8. ES6 常用总结(前端开发js技术进阶提升总结)

    一.变量声明const和let 在ES6之前,我们都是用var关键字声明变量.无论声明在何处,都会被视为声明在函数的最顶部(不在函数的最顶部就在全局作用域的最顶部).这就是函数变量提升例如: 不用关心 ...

  9. _luckdraw

    该表可以控制进行抽奖.10连抽: `comment` 备注 `itemId` 物品ID `chance`几率 `itemCount` 数量

  10. 【依赖注入】Unity和Autofac

    全面理解ASP.NET Core依赖注入:https://www.cnblogs.com/jesse2013/p/di-in-aspnetcore.html MSDN:https://docs.mic ...