菜鸟的博客

纵有疾风起,人生不言弃。

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

11.24周五补交

复制代码
import java.util.Scanner;
public class Main{
    static final int N = 100010;
    static int[] a = new int[N];
    static int[] b = new int[N];
    public static void insert(int l, int r, int c){
        b[l]+=c;
        b[r+1]-=c;
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int m = scanner.nextInt();

        for(int i = 1;i<=n;i++) a[i] = scanner.nextInt();
        for(int i = 1;i<=n;i++) insert(i,i,a[i]);

        while(m!=0){
            m--;
            int l = scanner.nextInt();
            int r = scanner.nextInt();
            int c = scanner.nextInt();
            insert(l,r,c);
        }

        for(int i = 1;i<=n;i++) b[i]+=b[i-1];
        for(int i = 1;i<=n;i++) System.out.print(b[i]+" ");
        System.out.println();
    }
}

差分矩阵
import java.util.Scanner;
public class Main{
   static int N = 1010;
   static int a[][] = new int[N][N];
   static int b[][] = new int[N][N];

   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 scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int q = scanner.nextInt();
        for(int i = 1;i<=n;i++){
            for(int j = 1;j<=m;j++){
                a[i][j] = scanner.nextInt();
            }
        }
        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){
            q--;
            int x1 = scanner.nextInt();
            int y1 = scanner.nextInt();
            int x2 = scanner.nextInt();
            int y2 = scanner.nextInt();
            int c = scanner.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();
        }
    }
}
复制代码

 

posted on   hhmzd233  阅读(3)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示