Matrix
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 271 Accepted Submission(s): 126
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)
;
indicating the number of test cases. For each test case:
The first line contains three integers n
, m
and q
.
The following n
lines describe the matrix M.(1≤Mi,j≤10,000)
for all (1≤i≤n,1≤j≤m)
.
The following q
lines contains three integers a(1≤a≤4)
, x
and y
.
after all q
operations.
2
Recommand to use scanf and printf
对于交换行、交换列的操作,分别记录当前状态下每一行、每一列是原始数组的哪一行、哪一列即可。
对每一行、每一列加一个数的操作,也可以两个数组分别记录。注意当交换行、列的同时,也要交换增量数组。
输出时通过索引找到原矩阵中的值,再加上行、列的增量
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#define LL __int64
#define pi acos(-1.0)
#define mod 1
#define maxn 10000
using namespace std;
int t;
int mp[][] ;
int n,m,q;
int a,x,y;
int l[],h[];
int ladd[],hadd[];
int main()
{
scanf("%d",&t);
for(int i=;i<=t;i++)
{
scanf("%d %d %d",&n,&m,&q);
for(int j=;j<=n;j++)
for(int k=;k<=m;k++)
scanf("%d",&mp[j][k]);
for(int j=;j<=n;j++)
{
h[j]=j;hadd[j]=j;
}
for(int j=;j<=m;j++)
{
l[j]=j; ladd[j]=;
}
memset(hadd,,sizeof(hadd));
memset(ladd,,sizeof(ladd));
int t;
for(int j=;j<=q;j++)
{
scanf("%d %d %d",&a,&x,&y);
if(a==)
{
t=h[y];
h[y]=h[x];
h[x]=t;
}
else
if(a==)
{
t=l[y];
l[y]=l[x];
l[x]=t;
}
else
if(a==)
{
hadd[h[x]]+=y;
}
else
ladd[l[x]]+=y;
}
for(int j=;j<=n;j++)
{
printf("%d",mp[h[j]][l[]]+hadd[h[j]]+ladd[l[]]);
for(int k=;k<=m;k++)
{
printf(" %d",mp[h[j]][l[k]]+hadd[h[j]]+ladd[l[k]]);
}
printf("\n");
}
}
return ;
}