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

算法模板(Java版)_前缀和与差分

@ZZHow(ZZHow1024)

差分是前缀和的逆运算。

前缀和

前缀和作用:快速求出 [l, r] 区间的和。

一维前缀和

  • 例题:AcWing 795. 前缀和
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[] a = new int[n + 1];int[] s = new int[n + 1];for (int i = 1; i <= n; i++)a[i] = scanner.nextInt(); // 原数组for (int i = 1; i <= n; i++)s[i] = s[i - 1] + a[i]; // 差分数组while (m-- != 0) {int l = scanner.nextInt();int r = scanner.nextInt();System.out.println(s[r] - s[l - 1]); // 求 [l, r] 的和}}
}

二维前缀和

  • 例题:AcWing 796. 子矩阵的和
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[n + 10][m + 10];int[][] s = new int[n + 10][m + 10];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++)s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j]; // 前缀和矩阵while (q-- != 0) {int x1 = scanner.nextInt();int y1 = scanner.nextInt();int x2 = scanner.nextInt();int y2 = scanner.nextInt();System.out.println(s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1]); // 求子矩阵的和}}
}

差分

快速的把 [l, r] 区间中的所有数都加上 C。

一维差分

  • 例题:AcWing 797. 差分
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[] a = new int[n + 10];int[] b = new int[n + 10];for (int i = 1; i <= n; i++)a[i] = scanner.nextInt(); // 原数组for (int i = 1; i <= n; i++)insert(b, i, i, a[i]); // 差分数组while (m-- != 0) {int l = scanner.nextInt();int r = scanner.nextInt();int c = scanner.nextInt();insert(b, l, r, c); // [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] + " ");}public static void insert(int[] b, int l, int r, int c) {b[l] += c;b[r + 1] -= c;}
}

二维差分

  • 例题:AcWing 798. 差分矩阵
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[n + 10][m + 10];int[][] b = new int[n + 10][m + 10];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(b, i, j, i, j, a[i][j]); // 差分矩阵while (q-- != 0) {int x1 = scanner.nextInt();int y1 = scanner.nextInt();int x2 = scanner.nextInt();int y2 = scanner.nextInt();int c = scanner.nextInt();insert(b, x1, y1, x2, y2, c); // 在 (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();}}public static void insert(int[][] b, 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;}
}
http://www.xdnf.cn/news/19660.html

相关文章:

  • win10虚拟机报错打不开和ubuntu空间不足
  • 深度学习中的数据增强实战:基于PyTorch的图像分类任务优化
  • 【音视频】WebRTC-NetEQ 分析
  • flutter踩坑插件:Swift架构不兼容
  • 深度学习篇---Pytorch常用优化器
  • 网络安全A模块专项练习任务十解析
  • 数据结构:单链表的应用(力扣算法题)第三章
  • ANTD-TABLE表格字段明细展示
  • (MySQL)分布式锁
  • k8s知识点汇总2
  • 【 HarmonyOS 】错误描述:The certificate has expired! 鸿蒙证书过期如何解决?
  • K8S-etcd数据库的相关操作
  • 吴恩达机器学习补充:决策树和随机森林
  • 中越跨境物流管理系统的设计与实现(原创)
  • DiffusionGPT-LLM驱动的文本生成图像系统
  • 【高等数学】第十章 重积分——第五节 含参变量的积分
  • 焦耳热技术助力顶刊研究:薄层质子交换膜实现高效水电解制氢
  • 【STM32】在链接脚本中指定DMA Buffer的地址
  • 智慧班牌系统基于Java+Vue技术栈构建,实现教育信息化综合管理。
  • shell脚本编辑(小白基础学习)
  • 从拿起简历(resume)重新找工作开始聊起
  • 【算法】算法题核心类别与通用解题思路
  • git基础命令
  • React中纯 localStorage 与 Context + useReducer + localStorage对比
  • HTML应用指南:利用GET请求获取MSN财经股价数据并可视化
  • IDEA Spring属性注解依赖注入的警告 Field injection is not recommended 异常解决方案
  • 【0426】insert into 内核实现之 找到 buffe, 插入 tuple (2)
  • YOLO 目标检测:YOLOv4数据增强、CIoU Loss、网络结构、CSP、SPPNet、FPN和PAN
  • 模型量化(Model Quantization) 和低精度计算(Low-Precision Computing)
  • 程序员与杀毒软件:一场不必要的“战争”?程序员用什么杀毒软件?-优雅草卓伊凡