CodeForces 706E Working routine
十字链表。
开一个十字链表,矩阵中每一格作为一个节点,记录五个量:
$s[i].L$:$i$节点左边的节点编号
$s[i].R$:$i$节点右边的节点编号
$s[i].U$:$i$节点上面的节点编号
$s[i].D$:$i$节点下面的节点编号
$s[i].V$:$i$节点存储的值
每次操作,只要把四个边上的那些边拆掉,重新连上新的边就可以了。时间复杂度:$O(qn)$。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
} const int maxn=;
int n,m,k,sz;
struct X { int u,d,l,r,v; }s[maxn*maxn];
int a[maxn*maxn],id[maxn][maxn]; int main()
{
scanf("%d%d%d",&n,&m,&k); for(int i=;i<=n;i++) id[i][]=sz++; for(int j=;j<=m;j++) id[][j]=sz++;
for(int i=;i<=n;i++) for(int j=;j<=m;j++) id[i][j]=sz++;
for(int i=;i<=n;i++) id[i][m+]=sz++; for(int j=;j<=m;j++) id[n+][j]=sz++; for(int i=;i<=n+;i++)
{
for(int j=;j<=m+;j++)
{
s[id[i][j]].l=-; s[id[i][j]].r=-; s[id[i][j]].u=-; s[id[i][j]].d=-; if(j->=) s[id[i][j]].l=id[i][j-]; if(j+<=m+) s[id[i][j]].r=id[i][j+];
if(i->=) s[id[i][j]].u=id[i-][j]; if(i+<=n+) s[id[i][j]].d=id[i+][j];
}
} for(int i=;i<=n;i++) for(int j=;j<=m;j++) scanf("%d",&s[id[i][j]].v); for(int i=;i<=k;i++)
{
int r1,c1,r2,c2,h,w;
scanf("%d%d%d%d%d%d",&r1,&c1,&r2,&c2,&h,&w); int p1,p2,t1,t2,t3,t4,P1,P2; p1=r1,p2=r2;
for(int j=;j<=c1;j++) p1=s[p1].r; for(int j=;j<=c2;j++) p2=s[p2].r; P1=p1,P2=p2;
for(int j=;j<=w-;j++) P1=s[P1].r; for(int j=;j<=w-;j++) P2=s[P2].r;
for(int j=;j<=h-;j++) P1=s[P1].d; for(int j=;j<=h-;j++) P2=s[P2].d; t1=p1,t2=p2; t3=s[p1].l; t4=s[p2].l;
for(int j=;j<=h;j++) swap(s[t1].l,s[t2].l), t1=s[t1].d, t2=s[t2].d;
for(int j=;j<=h;j++) swap(s[t3].r,s[t4].r), t3=s[t3].d, t4=s[t4].d; t1=p1,t2=p2; t3=s[p1].u; t4=s[p2].u;
for(int j=;j<=w;j++) swap(s[t1].u,s[t2].u), t1=s[t1].r, t2=s[t2].r;
for(int j=;j<=w;j++) swap(s[t3].d,s[t4].d), t3=s[t3].r, t4=s[t4].r; p1=P1,p2=P2; t1=p1,t2=p2; t3=s[p1].d; t4=s[p2].d;
for(int j=;j<=w;j++) swap(s[t1].d,s[t2].d), t1=s[t1].l, t2=s[t2].l;
for(int j=;j<=w;j++) swap(s[t3].u,s[t4].u), t3=s[t3].l, t4=s[t4].l; t1=p1,t2=p2; t3=s[p1].r; t4=s[p2].r;
for(int j=;j<=h;j++) swap(s[t1].r,s[t2].r), t1=s[t1].u, t2=s[t2].u;
for(int j=;j<=h;j++) swap(s[t3].l,s[t4].l), t3=s[t3].u, t4=s[t4].u; }
for(int i=;i<=n;i++)
{
int p=i;
for(int j=;j<=m;j++)
p=s[p].r, printf("%d ",s[p].v);
printf("\n");
} return ;
}
CodeForces 706E Working routine的更多相关文章
- 【链表】【模拟】Codeforces 706E Working routine
题目链接: http://codeforces.com/problemset/problem/706/E 题目大意: 给一个N*M的矩阵,Q个操作,每次把两个同样大小的子矩阵交换,子矩阵左上角坐标分别 ...
- [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 w ...
- Working routine CodeForces - 706E (链表)
大意: 给定矩阵, q个操作, 每次选两个子矩阵交换, 最后输出交换后的矩阵 双向十字链表模拟就行了 const int N = 1500; int n, m, q; struct _ { int v ...
- Magolor的数据结构作业
\(CodeForces 706E ~Working routine\) 给出一个矩阵,每次操作交换两个子矩阵,求最后状态. 使用链表存储,每次交换后,影响到的之后矩阵边缘的指针,暴力修改. \(~~ ...
- 十字链表 Codeforces Round #367 E Working routine
// 十字链表 Codeforces Round #367 E Working routine // 题意:给你一个矩阵,q次询问,每次交换两个子矩阵,问最后的矩阵 // 思路:暴力肯定不行.我们可以 ...
- codeforces B. Routine Problem 解题报告
题目链接:http://codeforces.com/problemset/problem/337/B 看到这个题目,觉得特别有意思,因为有熟悉的图片(看过的一部电影).接着让我很意外的是,在纸上比划 ...
- Codeforces Round #196 (Div. 2) B. Routine Problem
screen 尺寸为a:b video 尺寸为 c:d 如果a == c 则 面积比为 cd/ab=ad/cb (ad < cb) 如果b == d 则 面积比为 cd/ab=cb/ad (c ...
- Codeforces gym 100685 F. Flood bfs
F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Desc ...
- Codeforces Round #367 (Div. 2) (A,B,C,D,E)
Codeforces Round 367 Div. 2 点击打开链接 A. Beru-taxi (1s, 256MB) 题目大意:在平面上 \(n\) 个点 \((x_i,y_i)\) 上有出租车,每 ...
随机推荐
- knockoutJS 快速上手
翻译:Knockout 快速上手 - 3: knockoutJS 快速上手 许多时候,学会一种技术的有效方式就是使用它解决实际中的问题.在这一节,我们将学习使用 Knockout 来创建一个常见的应用 ...
- vijos1053 用spfa判断是否存在负环
MARK 用spfa判断是否存在负环 判断是否存在负环的方法有很多, 其中用spfa判断的方法是:如果存在一个点入栈两次,那么就存在负环. 细节想想确实是这样,按理来说是不存在入栈两次的如果边权值为正 ...
- 在 go/golang语言中使用 google Protocol Buffer
怎么在go语言中实用google protocol Buffer呢? 现在的潮流趋势就是一键搞定,跟ubuntu安装软件一样 go get code.google.com/p/goprotobuf/{ ...
- C/C++基础知识总结——类与对象
1. 面向对象程序设计的特点 1.1 抽象 1.2 封装 1.3 继承 1.4 多态 (1) 分为:强制多态.重载多态.类型参数化多态.包含多态 (2) 强制多态:类型转换 重载多态: 类型参数化多态 ...
- Redis系统学习 二、数据结构
一.字符串 1.在Redis里,字符串是最基本的数据结构.当你在思索着关键字-值对时,你就是在死锁着字符串数据结构.不要被名字给搞混了. 常见实例: set users:leto " ...
- 学会Func
学会Func 前言 首先你要会最基本的委托的使用,如果不会,看起来可能会有难度.. 不过第一个例子将帮你复习一下委托delegate 接下来通过几个例子就会学会怎么灵活使用Func了 委托回顾(d ...
- enode框架step by step之Staged event-driven architecture思想的运用
enode框架step by step之Staged event-driven architecture思想的运用 enode框架系列step by step文章系列索引: 分享一个基于DDD以及事件 ...
- vsftp FTP服务器 server settings , and add different users
建议阅读知识:http://linux.vbird.org/linux_basic/0210filepermission.php 这是关于档案权限,用户,组等的问题.介绍的很有意思. 1. Inst ...
- Arduino 各种模块篇 RGB LED灯
示例代码: 类似与这样的led,共阴rgb led,通过调节不同的亮度,组合成不同的颜色. 示例代码: /* 作者:极客工坊 时间:2012年12月18日 IDE版本号:1.0.1 发布地址:www. ...
- PHP之-json转数组,支持多层嵌套json
function json_to_array($str) { if (is_string($str)) $str = json_decode($str); $arr=array(); foreach( ...