当前位置: 首页 > news >正文

18440二维差分

18440二维差分

⭐️难度:中等
📖
在这里插入图片描述
📚


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int q = scanner.nextInt();
        int[][] a = new int[1010][1010];
        int[][] b = new int[1010][1010];

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                a[i][j] = scanner.nextInt();
                b[i][j] = a[i][j] + a[i-1][j-1] - a[i-1][j] - a[i][j-1]; // 求差分数组
            }
        }

        // 子矩阵求和
        for (int i = 0; i < q; i++) {
            int x1 = scanner.nextInt();
            int y1 = scanner.nextInt();
            int x2 = scanner.nextInt();
            int y2 = scanner.nextInt();
            int d = scanner.nextInt();
            
            b[x1][y1] += d;
            b[x1][y2+1] -= d;
            b[x2+1][y1] -= d;
            b[x2+1][y2+1] += d;
        }
        
        // 差分数组求前缀和还原原数组
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                b[i][j] = b[i][j] - b[i-1][j-1] + b[i-1][j] + b[i][j-1]; // 直接用b数组求前缀和
                System.out.print(b[i][j]+" ");
            }
            System.out.println();
        }
    }
}

🐱笔记
在这里插入图片描述

背公式,用公式。

相关文章:

  • 【开源-鸿蒙土拨鼠大理石系统】鸿蒙 HarmonyOS Next App+微信小程序+云平台
  • 微信小程序读取写入NFC文本,以及NFC直接启动小程序指定页面
  • AIP-155 请求标识
  • 标签使用笔记
  • sqli-lab
  • 【SpringBoot】数据访问技术spring Data、 JDBC、MyBatis、JSR-303校验
  • 使用通义万相Wan2.1进行视频生成
  • 钉钉宜搭智能车辆管理系统:AIoT与低代码融合的数字化解决方案
  • 基于Three.js的3D赛车游戏开发实战详解
  • Vue 3 路由管理实战:构建多页面博客导航 - 掌握 Vue Router 实现 SPA 页面跳转
  • nio多线程版本
  • 【Linux网络编程】五种IO模型 多路转接(select)
  • 第39天:安全开发-JavaEE应用SpringBoot框架Actuator监控泄漏Swagger自动化
  • java八股文之框架
  • (转)Java单例模式(1)
  • Unity 运用正则表达式保留字符串中的中文英文字母和数字
  • 深入理解 QVectorQString:Qt 中动态数组的强大力量
  • 《机器学习数学基础》补充资料:LU分解
  • linux 命令getent 的使用指南
  • 第五章 数据库完整性
  • 做网站用php转html/百度指数 移民
  • 网站采集跟直接复制有什么区别/网站上做推广
  • 移动网站优化 sousuoyouhua.com/东莞seo排名公司
  • 上海专业网站建站品牌/公众号怎么做文章推广
  • 网站站点地图设计/百度seo优化推广
  • 给别人做网站的公司/最新的网络营销的案例