輸入一個n行m列的整數矩陣,再輸入q個操作,每個操作包含五個整數x1, y1, x2, y2, c,其中(x1, y1)和(x2, y2)表示一個子矩陣的左上角坐標和右下角坐標,
每個操作都要將選中的子矩陣中的每個元素的值加上c,
請你將進行完所有操作后的矩陣輸出,
輸入格式
第一行包含整數n,m,q,
接下來n行,每行包含m個整數,表示整數矩陣,
接下來q行,每行包含5個整數x1, y1, x2, y2, c,表示一個操作,
輸出格式
共 n 行,每行 m 個整數,表示所有操作進行完畢后的最終矩陣,
資料范圍
1≤n,m≤10001≤n,m≤1000,
1≤q≤1000001≤q≤100000,
1≤x1≤x2≤n1≤x1≤x2≤n,
1≤y1≤y2≤m1≤y1≤y2≤m,
?1000≤c≤1000?1000≤c≤1000,
?1000≤矩陣內元素的值≤1000?1000≤矩陣內元素的值≤1000
輸入樣例:
3 4 3
1 2 2 1
3 2 2 1
1 1 1 1
1 1 2 2 1
1 3 2 3 2
3 1 3 4 1
輸出樣例:
2 3 4 1
4 3 4 1
2 2 2 2
有些步驟可以合并
代碼:
import java.util.Scanner; public class Main { static final int max=1005; static int a[][]=new int[max][max]; static int b[][]=new int[max][max]; public static void insert(int x1,int y1,int x2,int y2,int c){ b[x1][y1]+=c; b[x2+1][y1]-=c; b[x1][y2+1]-=c; b[x2+1][y2+1]+=c; } public static void main(String[] args) { Scanner scan=new Scanner(System.in); int n=scan.nextInt(); int m=scan.nextInt(); int t=scan.nextInt(); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) a[i][j]=scan.nextInt(); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) insert(i,j,i,j,a[i][j]);//差分陣列初始化 while(t-->0){ //矩陣更新操作 int x1=scan.nextInt(); int y1=scan.nextInt(); int x2=scan.nextInt(); int y2=scan.nextInt(); int c=scan.nextInt(); insert(x1,y1,x2,y2,c); } for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) b[i][j]+=b[i-1][j]+b[i][j-1]-b[i-1][j-1];//求差分陣列的前綴和 for(int i=1;i<=n;i++){ //輸出 for(int j=1;j<=m;j++) System.out.print(b[i][j]+" "); System.out.println(); } } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/107647.html
標籤:其他
下一篇:PAT乙級1011
