编程语言
首页 > 编程语言> > 差分矩阵java实现

差分矩阵java实现

作者:互联网

 

 

 

import java.util.Scanner;

public class Main {
static int N = 1010;
static int a[][] = new int[N][N];
//b为a的差分数组
static int b[][] = new int[N][N];
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
int m=scan.nextInt();
int q=scan.nextInt();
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
a[i][j]=scan.nextInt();
}
}
//构造b数组
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
insert(i,j,i,j,a[i][j]);
}
}
while(q-->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);
//b数组的前缀和就是a数组
}
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
b[i][j]+=b[i][j-1]+b[i-1][j]-b[i-1][j-1];
System.out.print(b[i][j]+" ");
}
System.out.println();
}
}
//实现查分实现子矩阵全加一个数
public static void insert(int x1,int y1,int x2,int y2,int c) {
b[x2+1][y2+1]+=c;
b[x1][y2+1]-=c;
b[x2+1][y1]-=c;
b[x1][y1]+=c;
}
}

标签:java,scan,int,矩阵,差分,nextInt,static,y1,y2
来源: https://www.cnblogs.com/joker5213/p/16098426.html