HDU5671-Matrix 区间加减
来源:互联网 发布:a类网络ip地址 编辑:程序博客网 时间:2024/06/03 13:44
Problem Description
There is a matrix M that has n rows and m columns (1≤n≤1000,1≤m≤1000) .Then we perform q(1≤q≤100,000) operations:
1 x y: Swap row x and row y(1≤x,y≤n) ;
2 x y: Swap column x and column y(1≤x,y≤m) ;
3 x y: Add y to all elements in row x(1≤x≤n,1≤y≤10,000) ;
4 x y: Add y to all elements in column x(1≤x≤m,1≤y≤10,000) ;
1 x y: Swap row x and row y
2 x y: Swap column x and column y
3 x y: Add y to all elements in row x
4 x y: Add y to all elements in column x
Input
There are multiple test cases. The first line of input contains an integerT(1≤T≤20) indicating the number of test cases. For each test case:
The first line contains three integersn ,m and q .
The followingn lines describe the matrix M.(1≤Mi,j≤10,000) for all (1≤i≤n,1≤j≤m) .
The followingq lines contains three integers a(1≤a≤4) ,x and y .
The first line contains three integers
The following
The following
Output
For each test case, output the matrix M after all q operations.
Sample Input
23 4 21 2 3 42 3 4 53 4 5 61 1 23 1 102 2 21 1010 11 1 22 1 2
Sample Output
12 13 14 151 2 3 43 4 5 61 1010 1
Hint
Recommand to use scanf and printf
这是一题bestcoder的B题,题目本身是挺简单的,只要观察到一个性质:
当我们进行 行交换的时候,每个元素所对应的列是不变的;
当我们进行 列交换的时候,每个元素所对应的行是不变的。
那么,我们先考虑这个问题的简化版本:
一开始矩阵所有数值都为0,对行列进行题目中的操作,
就可以开两个数组,分别记录每一行 每一列的变化量。如果是交换的话,直接swap就可以。(具体的自己试一下就知道了)
但是题目中有个问题,就是矩阵每个元素是有初始值的。那么怎么办呢?
从我们已经解决的问题出发,思考 如何转化成我们已经解决的问题
我的方法是开了rowindex数组,记录目前第I行是原来(一开始)的第几行。
列同理,开了colindex数组。
最后把原来的数值加上去就可以了。
注意每行最后不能有空格,不然会PE
代码:
#include<bits/stdc++.h>using namespace std;const int maxn=1010;int m,n,q;int row[maxn],col[maxn],rowindex[maxn],colindex[maxn];int backup[maxn][maxn],num[maxn][maxn];void init(void){ memset(row,0,sizeof(row)); memset(col,0,sizeof(col)); for(int i=1;i<=n;i++) rowindex[i]=i; for(int i=1;i<=m;i++) colindex[i]=i; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%d",&backup[i][j]);}int main(void){ int T,a,x,y; cin>>T; while(T--) { cin>>n>>m>>q; init(); while(q--) { scanf("%d%d%d",&a,&x,&y); if(a==1) { swap(row[x],row[y]); swap(rowindex[x],rowindex[y]); } if(a==2) { swap(col[x],col[y]); swap(colindex[x],colindex[y]); } if(a==3) row[x]+=y; if(a==4) col[x]+=y; } for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { num[i][j]=backup[rowindex[i]][colindex[j]]+row[i]+col[j]; if(j!=1) printf(" %d",num[i][j]); else printf("%d",num[i][j]); } printf("\n"); } } return 0;}
阅读全文
0 0
- HDU5671-Matrix 区间加减
- hdu5671 Matrix
- hdu5671
- Hdu5671 Matrix 矩阵、行列交换+行列加值
- 线段树模板 区间加减 区间修改
- 树链剖分&树状数组区间加减区间求和模板
- POJ 3468(线段树,区间加减 询问区间和)
- HDU5671矩阵行列交换
- hdu5671 Conturbatio(思维)
- hdu5828 Rikka with Sequence (线段树:区间开根+区间求和+区间加减)
- poj 3468 线段树对一个区间加减和求和
- 1455 - Kingdom(并查集+线段树区间加减)
- A Simple Problem with Integers 【线段树】-区间加减求和
- HDU 1166(线段树,单点加减 询问区间和)
- 几道题(hdu5671 && hdu5672 && hdu5673)
- POJ 3468 A Simple Problem with Integers(线段树功能:区间加减区间求和)
- POJ 3468 A Simple Problem with Integers(线段树|区间加减&&区间求和)
- 洛谷 3373(线段树,区间加减及乘法 询问区间和)
- My Map
- vue全家桶系列之网易云音乐(移动版)
- 写一个递归函数DigitSum(n),输入一个非负整数,返回组成它的数字之和。
- echart2使用入门
- 如何使用markdown
- HDU5671-Matrix 区间加减
- numpy添加新的维度:newaxis
- Linux命令基础34-如何在一个终端一次运行多个命令
- 图像处理行业专业级类库OpenCV
- uCOS-ii 如何获取最高优先级的Task
- 413. Arithmetic Slices Add to List
- oracle创建数据库和删除数据库
- 100亿数据找出最大的1000个数字
- h.264/AVC的结构 NAL结构